| 1 | package de.uka.ipd.sdq.cip.workflow.jobs; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.HashMap; |
| 5 | |
| 6 | import org.apache.log4j.Logger; |
| 7 | import org.eclipse.core.resources.IFolder; |
| 8 | import org.eclipse.core.runtime.IProgressMonitor; |
| 9 | import org.eclipse.emf.common.util.URI; |
| 10 | import org.eclipse.emf.ecore.resource.Resource; |
| 11 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 12 | |
| 13 | import de.uka.ipd.sdq.cip.configuration.CompletionConfiguration; |
| 14 | import de.uka.ipd.sdq.cip.configuration.Transformation; |
| 15 | import de.uka.ipd.sdq.workflow.IBlackboardInteractingJob; |
| 16 | import de.uka.ipd.sdq.workflow.exceptions.JobFailedException; |
| 17 | import de.uka.ipd.sdq.workflow.exceptions.RollbackFailedException; |
| 18 | import de.uka.ipd.sdq.workflow.exceptions.UserCanceledException; |
| 19 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.MDSDBlackboard; |
| 20 | import de.uka.ipd.sdq.workflow.mdsd.blackboard.ResourceSetPartition; |
| 21 | |
| 22 | public class CreateCopyOfModelsJob implements |
| 23 | IBlackboardInteractingJob<MDSDBlackboard> { |
| 24 | |
| 25 | /** The logger for this class */ |
| 26 | private Logger logger = Logger.getLogger(CreateCopyOfModelsJob.class); |
| 27 | |
| 28 | /** The blackboard to interact with */ |
| 29 | private MDSDBlackboard blackboard = null; |
| 30 | |
| 31 | /** The blackboard partition for this Job */ |
| 32 | private String modelPartition; |
| 33 | |
| 34 | /** The project id where to create folders */ |
| 35 | private String projectID; |
| 36 | |
| 37 | /** The transformation id where to copy models */ |
| 38 | private String transformationID; |
| 39 | |
| 40 | public CreateCopyOfModelsJob( |
| 41 | CompletionConfiguration completionConfiguration, |
| 42 | Transformation transformation) { |
| 43 | |
| 44 | this.projectID = completionConfiguration.getProjectID(); |
| 45 | this.modelPartition = completionConfiguration.getInputPartitionName(); |
| 46 | |
| 47 | URI qvtFileURI = URI.createURI(transformation.getQVTFileURI()); |
| 48 | this.transformationID = qvtFileURI.lastSegment().replace(qvtFileURI.fileExtension(), "") + |
| 49 | completionConfiguration.getTransformations().indexOf(transformation); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void execute(IProgressMonitor monitor) throws JobFailedException, |
| 54 | UserCanceledException { |
| 55 | |
| 56 | IFolder modelFolder = PrepareTransformationsJob.createFolder( |
| 57 | PrepareTransformationsJob.getModelFolder(projectID).getFolder( |
| 58 | transformationID), |
| 59 | logger); |
| 60 | |
| 61 | // Copy models |
| 62 | copyModelsFromBlackboard(modelFolder); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | private void copyModelsFromBlackboard(IFolder modelFolder) throws JobFailedException { |
| 67 | |
| 68 | |
| 69 | URI baseURI = URI.createURI("file:/"+modelFolder.getLocation().toOSString()); |
| 70 | |
| 71 | // access the resources |
| 72 | ResourceSetPartition partition = (ResourceSetPartition) this.blackboard.getPartition(modelPartition); |
| 73 | ResourceSet resourceSet = partition.getResourceSet(); |
| 74 | for (Resource resource : resourceSet.getResources()) { |
| 75 | |
| 76 | // we only need to copy the file models |
| 77 | if (resource.getURI().isFile()) { |
| 78 | |
| 79 | // get old resource path |
| 80 | URI oldURI = resource.getURI(); |
| 81 | String[] oldURISegments = oldURI.segments(); |
| 82 | // set the new resource path |
| 83 | URI newURI = baseURI; |
| 84 | if(oldURISegments.length > 1) |
| 85 | newURI = newURI.appendSegment(oldURISegments[oldURISegments.length - 2]); |
| 86 | newURI = newURI.appendSegment(oldURISegments[oldURISegments.length - 1]); |
| 87 | resource.setURI(newURI); |
| 88 | |
| 89 | try { |
| 90 | resource.save(new HashMap<String ,String>()); |
| 91 | partition.setContents(resource.getURI(), resource.getContents()); |
| 92 | } catch (IOException e) { |
| 93 | logger.error("Unable to store resource "+resource.getURI(),e); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | try { |
| 99 | partition.storeAllResources(); |
| 100 | } catch (IOException e) { |
| 101 | logger.error("unable to store all resources",e); |
| 102 | throw new JobFailedException("Unable to store all Resources"); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void setBlackboard(MDSDBlackboard blackboard) { |
| 108 | this.blackboard = blackboard; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | @Override |
| 113 | public String getName() { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void rollback(IProgressMonitor monitor) |
| 119 | throws RollbackFailedException {} // nothing to roll back |
| 120 | } |