Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b3017c207644d44ee259b39eb036391b9c652f7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*****************************************************************************
 * Copyright (c) 2015 CEA LIST.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Jeremie Tatibouet
 * 
 *****************************************************************************/
package org.eclipse.papyrus.uml.alf.transaction.commit;

import static org.eclipse.papyrus.uml.alf.transaction.ActivatorTransaction.logger;

import java.sql.Timestamp;
import java.util.Calendar;

import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.papyrus.uml.alf.libraries.helper.BackupState;
import org.eclipse.papyrus.uml.alf.libraries.helper.BackupState.EditionStatus;
import org.eclipse.papyrus.uml.alf.transaction.job.AlfCompilationJob;
import org.eclipse.uml2.uml.NamedElement;


/**
 * This class describes the process of propagating the state of a particular
 * model element in a given UML model
 */
public class CommitScenario extends ChangeScenario {

	/**
	 * Flag use to know if the commit is required. it may change in the before method
	 */
	private boolean isCommitRequired;

	public CommitScenario() {
		super();
		this.isCommitRequired = true;
	}

	/**
	 * Update the user model state edition status
	 */
	public void before() {
		/* 1. Check parent constraints */
		super.before();
		/* 2. Update user model state meta-data */
		BackupState editionState = new BackupState();
		editionState.status = EditionStatus.MERGED;
		editionState.timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
		this.userModelState.setEditionState(editionState);
		this.modelStateToBeCommitted = this.userModelState;
	}

	/**
	 * Propagate the model state specified in text within the model
	 * 
	 * @param - the model element to update with the specified changes
	 */
	public void execute(NamedElement target, final String lastEditedVersion) {
		/* 1. Load the states of the target */
		this.init(target);
		if (!this.userModelState.getContent().equals(lastEditedVersion)) {
			this.userModelState.setText(lastEditedVersion);
		}
		/* 2. Realize before actions */
		this.before();
		if (this.isCommitRequired) {
			/* 3.1. Schedule a job in charge of propagated the changes in the model */
			Job job = new AlfCompilationJob(this.modelStateToBeCommitted);
			job.setPriority(Job.SHORT);
			job.addJobChangeListener(new JobChangeAdapter() {
				@Override
				public void done(IJobChangeEvent event) {
					CommitScenario.this.after();
				}
			});
			job.schedule();
		} else {
			this.isCommitRequired = true;
		}
	}

	/**
	 * This method is automatically called after the job scheduled by this scenario terminates.
	 */
	public void after() {
		logger.info("Compilation Job Done");
	}

}

Back to the top