001: /*
002: * Copyright 2007 Giordano Maestro (giordano.maestro@assetdata.it)
003: *
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License.
007: *
008: * You may obtain a copy of the License at
009: * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
010: * or agreed to in writing, software distributed under the License is
011: * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
012: * KIND, either express or implied. See the License for the specific language
013: * governing permissions and limitations under the License.
014: */
015: package org.romaframework.module.designer.view.domain.radpage;
016:
017: import java.io.File;
018: import java.io.IOException;
019:
020: import org.apache.xmlbeans.XmlObject;
021: import org.apache.xmlbeans.XmlOptions;
022: import org.romaframework.aspect.core.annotation.AnnotationConstants;
023: import org.romaframework.aspect.view.annotation.ViewAction;
024: import org.romaframework.core.config.RomaApplicationContext;
025: import org.romaframework.core.domain.message.Message;
026: import org.romaframework.core.domain.message.MessageOk;
027: import org.romaframework.core.domain.message.MessageResponseListener;
028: import org.romaframework.core.flow.ObjectContext;
029: import org.romaframework.core.schema.SchemaClass;
030: import org.romaframework.core.schema.SchemaConfigurationLoader;
031: import org.romaframework.core.schema.SchemaManager;
032: import org.romaframework.core.schema.SchemaObject;
033: import org.romaframework.core.schema.SchemaReloader;
034: import org.romaframework.core.schema.config.FileSystemSchemaConfiguration;
035: import org.romaframework.core.serializer.RomaSerializationException;
036: import org.romaframework.module.designer.domain.RadClass;
037: import org.romaframework.module.designer.view.domain.SchemaViewInstance;
038: import org.romaframework.xml.config.XmlConfigClassDocument;
039:
040: /**
041: * The page for class, configuration, it is used for the root class
042: * configuration
043: *
044: * @author Giordano Maestro(giordano.maestro@assetdata.it) 09/nov/07
045: *
046: */
047: public class RootClassAspectsPage extends ClassAspectsPage implements
048: MessageResponseListener {
049: public RootClassAspectsPage(RadClass clazz)
050: throws RomaSerializationException {
051: super (clazz);
052: }
053:
054: @Override
055: public void ok() throws Exception {
056: try {
057: partialOk();
058: XmlConfigClassDocument doc = (XmlConfigClassDocument) serializeMyComponent();
059: saveXML(doc);
060: ObjectContext.getInstance().show(
061: new MessageOk("", "", this , "Successful saved"));
062: } catch (Exception e) {
063: e.printStackTrace();
064: ObjectContext.getInstance().show(
065: new MessageOk("", "", this , "File not saved: "
066: + e.getMessage()));
067: }
068: }
069:
070: private void partialOk() throws Exception,
071: RomaSerializationException, IOException {
072: super .ok();
073: }
074:
075: private void saveXML(XmlConfigClassDocument doc) throws Exception {
076: synchronized (SchemaReloader.reloading_mutex) {
077: String name = clazz.getEntity().getSchemaClass()
078: .getSchemaClass().getName();
079: FileSystemSchemaConfiguration schemaConfiguration = RomaApplicationContext
080: .getInstance().getBean(
081: SchemaConfigurationLoader.class)
082: .getXmlFileSystemSchemaConfiguration(name);
083:
084: if (schemaConfiguration != null) {
085: schemaConfiguration.save(doc);
086: } else {
087: newXml(doc, name);
088: }
089: }
090: }
091:
092: private void newXml(XmlConfigClassDocument doc, String name)
093: throws Exception {
094: Class toSave = clazz.getEntity().getSchemaClass()
095: .getSchemaClass().getClazz();
096: String packageFile = toSave.getPackage().toString().replaceAll(
097: "package ", "");
098: packageFile = packageFile.replaceAll("\\.", "/");
099: File fileToSave = new File(toSave.getResource(
100: "/" + packageFile + "/").getFile()
101: + name + ".xml");
102: SchemaClass old = clazz.getEntity().getSchemaClass()
103: .getSchemaClass();
104: doc.save(fileToSave);
105: FileSystemSchemaConfiguration conf = new FileSystemSchemaConfiguration(
106: fileToSave, "/" + packageFile + "/" + name + ".xml");
107: ObjectContext.getInstance().getComponent(SchemaManager.class)
108: .registerClassInfo(old.getName(), old.getClazz(),
109: old.getBaseClass(), conf);
110: ObjectContext.getInstance().getComponent(SchemaReloader.class)
111: .addResourceForReloading(fileToSave, name);
112: doc.save(fileToSave, docOptions());
113: }
114:
115: private XmlOptions docOptions() {
116: XmlOptions options = new XmlOptions();
117: options.setCharacterEncoding("UTF-8");
118: options.setUseDefaultNamespace();
119: options.setSavePrettyPrint();
120: return options;
121: }
122:
123: @Override
124: public void cancel() throws Exception {
125: super .cancel();
126: ObjectContext.getInstance().show(getBackObject());
127: ((SchemaViewInstance) getBackObject()).clean();
128: }
129:
130: @Override
131: protected XmlObject serializeMyComponent()
132: throws RomaSerializationException {
133:
134: XmlConfigClassDocument document = XmlConfigClassDocument.Factory
135: .newInstance();
136: document.addNewClass1().set(super .serializeMyComponent());
137: return document;
138: }
139:
140: @ViewAction(visible=AnnotationConstants.FALSE)
141: public void responseMessage(Message iMessage, Object iResponse) {
142:
143: ObjectContext.getInstance().show(getBackObject(),
144: "screen://body");
145: ((SchemaViewInstance) getBackObject()).clean();
146: }
147:
148: public void preview() {
149: try {
150: partialOk();
151: Class toshowClass = clazz.getEntity().getSchemaClass()
152: .getSchemaClass().getClazz();
153: ObjectContext.getInstance().show(
154: toshowClass.newInstance(),
155: "screen:popup",
156: null,
157: null,
158: (SchemaObject) clazz.getEntity()
159: .getSchemaFeatures());
160: } catch (Exception e) {
161: ObjectContext.getInstance().show(
162: new MessageOk("", "", null,
163: "Cannot preview the object, maybe it has not the default contructor "
164: + e.getMessage()));
165: }
166: }
167: }
|