1 | package de.uka.ipd.sdq.cip.workflow.jobs; |
2 | |
3 | import java.io.IOException; |
4 | import java.util.ArrayList; |
5 | import java.util.HashMap; |
6 | import java.util.Iterator; |
7 | |
8 | import org.apache.log4j.Logger; |
9 | import org.eclipse.core.resources.IFolder; |
10 | import org.eclipse.core.runtime.IProgressMonitor; |
11 | import org.eclipse.emf.common.util.EList; |
12 | import org.eclipse.emf.common.util.URI; |
13 | import org.eclipse.emf.ecore.EObject; |
14 | import org.eclipse.emf.ecore.resource.Resource; |
15 | import org.eclipse.emf.ecore.resource.ResourceSet; |
16 | |
17 | import de.uka.ipd.sdq.cip.configuration.CompletionConfiguration; |
18 | import de.uka.ipd.sdq.cip.configuration.QVTConfigurationHelper; |
19 | import de.uka.ipd.sdq.cip.configuration.Transformation; |
20 | import de.uka.ipd.sdq.workflow.IBlackboardInteractingJob; |
21 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
22 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
23 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
24 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
25 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.ModelLocation; |
26 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.ResourceSetPartition; |
27 | import de.uka.ipd.sdq.workflow.mdsd.emf.qvtr.jobs.QVTRTransformationJobConfiguration; |
28 | |
29 | public class SplitTransformationResultJob implements IBlackboardInteractingJob<MDSDBlackboard> { |
30 | |
31 | /** The logger for this class */ |
32 | private Logger logger = Logger.getLogger(SplitTransformationResultJob.class); |
33 | |
34 | /** The blackboard to interact with */ |
35 | private MDSDBlackboard blackboard = null; |
36 | |
37 | /** The blackboard partition for this Job */ |
38 | private String modelPartitionID; |
39 | |
40 | /** The project id where to create folders */ |
41 | private String projectID; |
42 | |
43 | /** The transformation id where to copy models */ |
44 | private String transformationID; |
45 | |
46 | /** The configuration for a qvtr completion job */ |
47 | private QVTRTransformationJobConfiguration qvtrTransformationJobConfiguration; |
48 | |
49 | public SplitTransformationResultJob( |
50 | QVTRTransformationJobConfiguration qvtrTransformationJobConfiguration, |
51 | CompletionConfiguration completionConfiguration, |
52 | Transformation transformation) { |
53 | |
54 | this.modelPartitionID = completionConfiguration.getInputPartitionName(); |
55 | this.qvtrTransformationJobConfiguration = qvtrTransformationJobConfiguration; |
56 | this.projectID = completionConfiguration.getProjectID(); |
57 | |
58 | |
59 | this.transformationID = QVTConfigurationHelper.createTransformationID(completionConfiguration, transformation); |
60 | } |
61 | |
62 | @Override |
63 | public void execute(IProgressMonitor monitor) throws JobFailedException, |
64 | UserCanceledException { |
65 | |
66 | IFolder modelFolder = PrepareTransformationsJob.createFolder( |
67 | PrepareTransformationsJob.getModelFolder(projectID).getFolder( |
68 | transformationID), |
69 | logger); |
70 | |
71 | ResourceSetPartition partition = this.blackboard.getPartition(modelPartitionID); |
72 | ResourceSet resourceSet = partition.getResourceSet(); |
73 | |
74 | // Collect Resources to delete |
75 | ArrayList<Resource> resourcesToDelete = new ArrayList<Resource>(); |
76 | for (Resource resource : resourceSet.getResources()) { |
77 | // we only need to delete the file models |
78 | if (resource.getURI().isFile()) { |
79 | resourcesToDelete.add(resource); |
80 | } |
81 | } |
82 | |
83 | // Split output models |
84 | Iterator<ModelLocation[]> iterator = qvtrTransformationJobConfiguration.getModelLocationSets().iterator(); |
85 | iterator.next(); // go to second element |
86 | for (ModelLocation modelLocation : iterator.next()) { |
87 | |
88 | ResourceSetPartition modelPartition = this.blackboard.getPartition(modelLocation.getPartitionID()); |
89 | |
90 | EList<EObject> topLevelObjects = (EList<EObject>) modelPartition.getContents(modelLocation.getModelID()); |
91 | while (topLevelObjects.size() > 0) |
92 | createResource(topLevelObjects.get(0), partition, modelFolder); |
93 | |
94 | // Delete all temporary resources |
95 | Resource resource = modelPartition.getResourceSet().getResource(modelLocation.getModelID(), false); |
96 | if(resource.isLoaded()) |
97 | try { |
98 | resource.delete(new HashMap<String, String>()); |
99 | } catch (IOException e) { |
100 | logger.error("Unable to delete resource " + resource.getURI(),e); |
101 | } |
102 | } |
103 | |
104 | // remove all marked resources from partition |
105 | for (Resource resource : resourcesToDelete) { |
106 | partition.getResourceSet().getResources().remove(resource); |
107 | } |
108 | |
109 | |
110 | } |
111 | |
112 | private void createResource(EObject eobject, ResourceSetPartition partition, IFolder modelFolder) { |
113 | |
114 | URI baseURI = URI.createFileURI(modelFolder.getFullPath().toString()); |
115 | String eobjectName = eobject.eClass().getName(); |
116 | URI modelURI = baseURI.appendSegment(eobjectName + ".xmi"); |
117 | Resource resource = partition.getResourceSet().getResource(modelURI, false); |
118 | |
119 | // find unused name |
120 | long num = 1; |
121 | while(resource != null) |
122 | { |
123 | |
124 | modelURI = baseURI.appendSegment(eobjectName + "_" + String.valueOf(num) + ".xmi"); |
125 | resource = partition.getResourceSet().getResource(modelURI, false); |
126 | num++; |
127 | } |
128 | |
129 | resource = partition.getResourceSet().createResource(modelURI); |
130 | |
131 | resource.getContents().add(eobject); |
132 | |
133 | try { |
134 | resource.save(new HashMap<String, String>()); |
135 | //partition.setContents(resource.getURI(), resource.getContents()); |
136 | } catch (IOException e) { |
137 | logger.error("Unable to store resource "+resource.getURI(),e); |
138 | } |
139 | } |
140 | |
141 | @Override |
142 | public String getName() { |
143 | return "Clean up qvtr completion transformation result"; |
144 | } |
145 | |
146 | @Override |
147 | public void rollback(IProgressMonitor monitor) |
148 | throws RollbackFailedException {} // not needed |
149 | |
150 | @Override |
151 | public void setBlackboard(MDSDBlackboard blackboard) { |
152 | this.blackboard = blackboard; |
153 | } |
154 | |
155 | } |