Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f476a9b9e94b24c2f80b8977f828a29710602004 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * Copyright (c) 2013 Atos
 * 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:
 *     Arthur Daussy - initial implementation
 *******************************************************************************/
package org.eclipse.papyrus.team.collaborative.svn.versioncontroller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.papyrus.team.collaborative.core.IExtendedURI;
import org.eclipse.papyrus.team.collaborative.core.participants.version.IUpdater;
import org.eclipse.papyrus.team.collaborative.svn.tracing.ITracingConstant;
import org.eclipse.papyrus.team.collaborative.svn.tracing.Tracer;


/**
 * SVN implementation of {@link IUpdater}
 *
 * @author adaussy
 *
 */
public class SVNUpdater extends AbstractSVNVersionController implements IUpdater {

	/**
	 * Instantiates a new sVN updater.
	 *
	 * @param uris
	 *            the uris
	 * @param resourceSet
	 *            the resource set
	 */
	public SVNUpdater(Set<IExtendedURI> uris, ResourceSet resourceSet) {
		super(uris, resourceSet);
	}

	@Override
	public IStatus update() {
		IFile[] resourcesToProcess = getTargetFiles(getTargetResources());
		// The all project of each file shall be updated (In order to see if new files appeared (New control))
		Set<IProject> projects = new HashSet<IProject>();
		for (IFile f : resourcesToProcess) {
			projects.add(f.getProject());
		}

		IProject[] projectsToUpdate = new IProject[projects.size()];
		projects.toArray(projectsToUpdate);
		if (ITracingConstant.UPDATE_TRACING) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("Updating projects: ").append("\n");
			for (IProject p : projectsToUpdate) {
				stringBuilder.append(p.getFullPath()).append("\n");
			}
			Tracer.logInfo(stringBuilder.toString());
		}
		CompositeOperation op = UpdateAction.getUpdateOperation(projectsToUpdate, SVNRevision.HEAD);
		ICancellableOperationWrapper runnable = UIMonitorUtility.doTaskNowDefault(op, false);
		IActionOperation resultStatus = runnable.getOperation();

		return resultStatus.getStatus();
	}

	@Override
	protected Set<IExtendedURI> doBuild() {
		for (IExtendedURI extendedURI : getUris()) {
			IExtendedURI resourceURI = getResourceURI(extendedURI);
			if (resourceURI != null) {
				IFile file = toIFile(toResource(resourceURI.getUri()));
				if (file != null && file.exists()) {
					if (!isUpToDate(resourceURI).isUpToDate()) {
						getExtendedSet().add(resourceURI);
					}
				}
			}
		}
		return getExtendedSet();
	}


	/**
	 * Checks if is up to date.
	 *
	 * @param uri
	 *            the uri
	 * @return the i status
	 */
	@Override
	public UpToDateStatus isUpToDate(IExtendedURI uri) {
		IExtendedURI resourceEURI = getResourceURI(uri);
		if (resourceEURI == null) {
			return UpToDateStatus.createErrorDuringUpToDataStatus("The URI is null");
		}
		UpToDateStatus status = getCache().get(resourceEURI);
		if (status == null) {
			URI resourceURI = resourceEURI.getUri();
			Resource resource = toResource(resourceURI);
			if (resource == null) {
				return UpToDateStatus.createErrorDuringUpToDataStatus(resourceURI + " is not a URI pointing to a resource");
			}
			IFile file = toIFile(resource);
			if (file == null || !file.exists()) {
				return UpToDateStatus.createErrorDuringUpToDataStatus(resourceURI + " is not a URI pointing to a existing file");
			}

			SVNLogEntry log = getRemoteHEADRevision(file);
			if (log == null) {
				return UpToDateStatus.createErrorDuringUpToDataStatus("Unable to get remote HEAD revision for file " + file.getFullPath());
			}
			InfoOperation refreshOperation = new InfoOperation(file);
			refreshOperation.run(new NullProgressMonitor());
			SVNEntryInfo info = refreshOperation.getInfo();
			long localRevision = info.revision;
			if (log.revision > localRevision) {
				status = UpToDateStatus.createNotUpToDataStatus(log.author, log.date, log.revision);
				getCache().put(resourceEURI, status);
				return status;
			} else {
				status = UpToDateStatus.createUpToDataStatus(log.author, log.date, log.revision);
				getCache().put(resourceEURI, status);
			}
		}
		return status;
	}




	protected Map<IExtendedURI, UpToDateStatus> getCache() {
		if (cache == null) {
			cache = new HashMap<IExtendedURI, IUpdater.UpToDateStatus>();
		}
		return cache;
	}



	private Map<IExtendedURI, UpToDateStatus> cache = null;

	public static final long INVALID_REVISION = -1;


	public SVNLogEntry getRemoteHEADRevision(IResource resource) {
		IRepositoryResource remote = SVNRemoteStorage.instance().asRepositoryResource(resource);
		GetLogMessagesOperation logOp = new GetLogMessagesOperation(remote);
		logOp.setLimit(1);
		logOp.run(new NullProgressMonitor());
		if (logOp.getExecutionState() == IActionOperation.OK && logOp.getMessages() != null) {
			SVNLogEntry[] messages = logOp.getMessages();
			if (messages.length > 0) {
				return messages[0];
			}
		}
		return null;
	}





}

Back to the top