001: /**
002: *
003: */package model;
004:
005: import java.io.File;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011:
012: import logging.LoggingPlugin;
013:
014: import org.eclipse.core.resources.IContainer;
015: import org.eclipse.core.resources.ResourcesPlugin;
016: import org.eclipse.core.runtime.ILog;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.emf.common.util.URI;
022: import org.eclipse.emf.ecore.resource.ResourceSet;
023: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
024: import org.openarchitectureware.emf.XmiReader;
025: import org.openarchitectureware.workflow.WorkflowContext;
026: import org.openarchitectureware.workflow.WorkflowContextDefaultImpl;
027: import org.openarchitectureware.workflow.issues.Issues;
028: import org.openarchitectureware.workflow.issues.IssuesImpl;
029: import org.openarchitectureware.workflow.monitor.NullProgressMonitor;
030:
031: /**
032: * @author sh
033: *
034: */
035: public class ModelEditor {
036:
037: // singleton
038: public static final ModelEditor INSTANCE = new ModelEditor();
039:
040: private Issues issues = null;
041:
042: /**
043: * Read the Process Metamodel by a XMI Reader
044: *
045: * @return model the Ecore model
046: * @throws Exception
047: */
048: public List<Map<WorkflowContext, String>> loadModels() {
049: // initialize the issues object which stores errors and warnings
050: // of the load process
051: this .issues = new IssuesImpl();
052:
053: // 1. search for the Meta Model File
054: List<String> metaModelFile = getMetaModelFiles();
055:
056: // 2. search for the model File(s)
057: List<String> modelFiles = getModelFiles();
058:
059: // 3. check if model files and meta model files were found
060: if (metaModelFile == null || modelFiles == null)
061: return null;
062: if (metaModelFile.isEmpty() || modelFiles.isEmpty())
063: return null;
064:
065: // 4. read the object trees from the models
066: return readModels(metaModelFile, modelFiles);
067: }
068:
069: /**
070: * Searches for meta model files in the workspace.
071: * For the process editor these files ends with ".ecore".
072: * Only opened projects are considered.
073: *
074: * @return metaModelFile a list containing all found
075: * meta model files
076: */
077: private List<String> getMetaModelFiles() {
078: List<String> metaModelFile = ModelUtil.getMetaModelFiles();
079: if (metaModelFile == null || metaModelFile.isEmpty()) {
080: String message = "No Meta Model File (*.ecore) has been found !";
081: issues.addError(message);
082: ILog logger = LoggingPlugin.getDefault().getLog();
083: String symName = LoggingPlugin.getDefault().getBundle()
084: .getSymbolicName();
085: logger.log(new Status(IStatus.ERROR, symName, 0, message,
086: null));
087: throw new RuntimeException(message);
088: }
089: return metaModelFile;
090: }
091:
092: /**
093: * Searches for model files in the workspace.
094: * For the process editor these files ends with "concept".
095: * Only opened projects are considered.
096: *
097: * @return modelFiles a list containing all found
098: * model files
099: */
100: private List<String> getModelFiles() {
101: List<String> modelFiles = ModelUtil.getModelFiles();
102: if (modelFiles == null || modelFiles.isEmpty()) {
103: String message = "No Process Model Files to refactor have been found !";
104: issues.addError(message);
105: ILog logger = LoggingPlugin.getDefault().getLog();
106: String symName = LoggingPlugin.getDefault().getBundle()
107: .getSymbolicName();
108: logger.log(new Status(IStatus.ERROR, symName, 0, message,
109: null));
110: throw new RuntimeException(message);
111: }
112: return modelFiles;
113: }
114:
115: /**
116: * This helper method reads the given model files an creates
117: * the object tree for that model. The meta model file is needed
118: * to instanciate the object tree for the given models.
119: *
120: * @param metaModelFile a list containing all meta models
121: * @param modelFiles al list containing all model files to load
122: * @return models a map containing a list consisting of object tree
123: * model file pairs
124: */
125: private List<Map<WorkflowContext, String>> readModels(
126: List<String> metaModelFile, List<String> modelFiles) {
127: XmiReader reader = new XmiReader();
128: reader.setMetaModelFile(metaModelFile.get(0).toString());
129: Map<WorkflowContext, String> contextFileMap = null;
130: WorkflowContext ctx = null;
131: List<Map<WorkflowContext, String>> models = new ArrayList<Map<WorkflowContext, String>>();
132: for (Iterator<String> iterator = modelFiles.iterator(); iterator
133: .hasNext();) {
134: String modelFile = (String) iterator.next();
135: reader.setModelFile(modelFile);
136: try {
137: reader
138: .setMetaModelPackage("newprocess.NewprocessPackage");
139: } catch (RuntimeException e) {
140: //throw new RuntimeException(e);
141: // TODO: has to be fixed because a ClassCast Exception occures
142: }
143: reader.setFirstElementOnly(true);
144: ctx = new WorkflowContextDefaultImpl();
145: reader.invoke(ctx, new NullProgressMonitor(), this .issues);
146:
147: // add the Workflow Context and the concerning ModelFile Path to the Collection
148: contextFileMap = new HashMap<WorkflowContext, String>();
149: contextFileMap.put(ctx, modelFile);
150: models.add(contextFileMap);
151: }
152: return models;
153: }
154:
155: /**
156: * Write the edited Process Metamodel by a XMI Writer
157: *
158: * @param ctx the Workflow Context containing the model
159: */
160: public void writeModel(List<Map<WorkflowContext, String>> binding) {
161: for (Iterator<Map<WorkflowContext, String>> iterator = binding
162: .iterator(); iterator.hasNext();) {
163: Map<WorkflowContext, String> ctxModelMap = iterator.next();
164:
165: for (Iterator<WorkflowContext> contextIter = ctxModelMap
166: .keySet().iterator(); contextIter.hasNext();) {
167: WorkflowContext ctx = (WorkflowContext) contextIter
168: .next();
169: String path = ctxModelMap.get(ctx);
170:
171: IPath filePath = new Path(path);
172: IContainer[] root = ResourcesPlugin.getWorkspace()
173: .getRoot().findContainersForLocation(filePath);
174: for (int i = 0; i < root.length; i++) {
175: IContainer container = root[i];
176: filePath = container.getFullPath();
177: }
178:
179: Writer writer = new Writer();
180: writer.setModelSlot("default");
181: URI url = URI.createPlatformResourceURI(filePath
182: .toString(), true);
183: writer.setUri(url.toString());
184: ResourceSet rs = new ResourceSetImpl();
185: writer.setResourceSet(rs);
186: new StandaloneSetup().setPlatformUri(new File("..")
187: .getAbsolutePath());
188: this .issues = new IssuesImpl();
189: writer.invoke(ctx, new NullProgressMonitor(),
190: this .issues);
191: }
192: }
193: }
194:
195: /**
196: * getter for the issues property.
197: *
198: * @return issues
199: */
200: public Issues getIssues() {
201: return this.issues;
202: }
203: }
|