/
StandardVersioningService.java
379 lines (327 loc) · 14.1 KB
/
StandardVersioningService.java
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
*
* 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:
* Florent Guillaume
* Laurent Doguin
*/
package org.nuxeo.ecm.core.versioning;
import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSITION_EVENT;
import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_FROM;
import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_TO;
import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_TRANSITION;
import static org.nuxeo.ecm.core.api.VersioningOption.MAJOR;
import static org.nuxeo.ecm.core.api.VersioningOption.MINOR;
import static org.nuxeo.ecm.core.api.VersioningOption.NONE;
import static org.nuxeo.ecm.core.api.event.CoreEventConstants.DOC_LIFE_CYCLE;
import static org.nuxeo.ecm.core.api.event.CoreEventConstants.REPOSITORY_NAME;
import static org.nuxeo.ecm.core.api.event.CoreEventConstants.SESSION_ID;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelFactory;
import org.nuxeo.ecm.core.api.LifeCycleException;
import org.nuxeo.ecm.core.api.VersioningOption;
import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
import org.nuxeo.ecm.core.api.model.PropertyNotFoundException;
import org.nuxeo.ecm.core.event.EventService;
import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
import org.nuxeo.ecm.core.model.Document;
import org.nuxeo.ecm.core.schema.FacetNames;
import org.nuxeo.runtime.api.Framework;
/**
* Implementation of the versioning service that follows standard checkout / checkin semantics.
*/
public class StandardVersioningService implements ExtendableVersioningService {
private static final Log log = LogFactory.getLog(StandardVersioningService.class);
public static final String FILE_TYPE = "File";
public static final String NOTE_TYPE = "Note";
public static final String PROJECT_STATE = "project";
public static final String APPROVED_STATE = "approved";
public static final String OBSOLETE_STATE = "obsolete";
public static final String BACK_TO_PROJECT_TRANSITION = "backToProject";
protected static final String AUTO_CHECKED_OUT = "AUTO_CHECKED_OUT";
/** Key for major version in Document API. */
protected static final String MAJOR_VERSION = "ecm:majorVersion";
/** Key for minor version in Document API. */
protected static final String MINOR_VERSION = "ecm:minorVersion";
private Map<String, VersioningRuleDescriptor> versioningRules;
/**
* @since 9.3
*/
public static final String CATEGORY = "category";
/**
* @since 9.3
*/
public static final String COMMENT = "comment";
private DefaultVersioningRuleDescriptor defaultVersioningRule;
@Override
public String getVersionLabel(DocumentModel docModel) {
String label;
try {
label = getMajor(docModel) + "." + getMinor(docModel);
if (docModel.isCheckedOut() && !"0.0".equals(label)) {
label += "+";
}
} catch (PropertyNotFoundException e) {
label = "";
}
return label;
}
protected long getMajor(DocumentModel docModel) {
return getVersion(docModel, VersioningService.MAJOR_VERSION_PROP);
}
protected long getMinor(DocumentModel docModel) {
return getVersion(docModel, VersioningService.MINOR_VERSION_PROP);
}
protected long getVersion(DocumentModel docModel, String prop) {
Object propVal = docModel.getPropertyValue(prop);
if (propVal == null || !(propVal instanceof Long)) {
return 0;
} else {
return ((Long) propVal).longValue();
}
}
protected long getMajor(Document doc) {
return getVersion(doc, MAJOR_VERSION);
}
protected long getMinor(Document doc) {
return getVersion(doc, MINOR_VERSION);
}
protected long getVersion(Document doc, String prop) {
Object propVal = doc.getPropertyValue(prop);
if (propVal == null || !(propVal instanceof Long)) {
return 0;
} else {
return ((Long) propVal).longValue();
}
}
protected void setVersion(Document doc, long major, long minor) {
doc.setPropertyValue(MAJOR_VERSION, Long.valueOf(major));
doc.setPropertyValue(MINOR_VERSION, Long.valueOf(minor));
}
protected void incrementMajor(Document doc) {
setVersion(doc, getMajor(doc) + 1, 0);
}
protected void incrementMinor(Document doc) {
// make sure major is not null by re-setting it
setVersion(doc, getMajor(doc), getMinor(doc) + 1);
}
protected void incrementByOption(Document doc, VersioningOption option) {
try {
if (option == MAJOR) {
incrementMajor(doc);
} else if (option == MINOR) {
incrementMinor(doc);
}
// else nothing
} catch (PropertyNotFoundException e) {
// ignore
}
}
@Override
public void doPostCreate(Document doc, Map<String, Serializable> options) {
if (doc.isVersion() || doc.isProxy()) {
return;
}
setInitialVersion(doc);
}
/**
* Sets the initial version on a document. Can be overridden.
*/
protected void setInitialVersion(Document doc) {
InitialStateDescriptor initialState = null;
if (versioningRules != null) {
VersioningRuleDescriptor versionRule = versioningRules.get(doc.getType().getName());
if (versionRule != null) {
initialState = versionRule.getInitialState();
}
}
if (initialState == null && defaultVersioningRule != null) {
initialState = defaultVersioningRule.getInitialState();
}
if (initialState != null) {
int initialMajor = initialState.getMajor();
int initialMinor = initialState.getMinor();
setVersion(doc, initialMajor, initialMinor);
return;
}
setVersion(doc, 0, 0);
}
@Override
public List<VersioningOption> getSaveOptions(DocumentModel docModel) {
boolean versionable = docModel.isVersionable();
String lifecycleState = docModel.getCoreSession().getCurrentLifeCycleState(docModel.getRef());
String type = docModel.getType();
return getSaveOptions(versionable, lifecycleState, type);
}
protected List<VersioningOption> getSaveOptions(Document doc) {
boolean versionable = doc.getType().getFacets().contains(FacetNames.VERSIONABLE);
String lifecycleState;
try {
lifecycleState = doc.getLifeCycleState();
} catch (LifeCycleException e) {
lifecycleState = null;
}
String type = doc.getType().getName();
return getSaveOptions(versionable, lifecycleState, type);
}
protected List<VersioningOption> getSaveOptions(boolean versionable, String lifecycleState, String type) {
if (!versionable) {
return Arrays.asList(NONE);
}
if (lifecycleState == null) {
return Arrays.asList(NONE);
}
SaveOptionsDescriptor option = null;
if (versioningRules != null) {
VersioningRuleDescriptor saveOption = versioningRules.get(type);
if (saveOption != null) {
option = saveOption.getOptions().get(lifecycleState);
if (option == null) {
// try on any life cycle state
option = saveOption.getOptions().get("*");
}
}
}
if (option == null && defaultVersioningRule != null) {
option = defaultVersioningRule.getOptions().get(lifecycleState);
if (option == null) {
// try on any life cycle state
option = defaultVersioningRule.getOptions().get("*");
}
}
if (option != null) {
return option.getVersioningOptionList();
}
if (PROJECT_STATE.equals(lifecycleState) || APPROVED_STATE.equals(lifecycleState)
|| OBSOLETE_STATE.equals(lifecycleState)) {
return Arrays.asList(NONE, MINOR, MAJOR);
}
if (FILE_TYPE.equals(type) || NOTE_TYPE.equals(type)) {
return Arrays.asList(NONE, MINOR, MAJOR);
}
return Arrays.asList(NONE);
}
protected VersioningOption validateOption(Document doc, VersioningOption option) {
List<VersioningOption> options = getSaveOptions(doc);
if (!options.contains(option)) {
option = options.isEmpty() ? NONE : options.get(0);
}
return option;
}
@Override
public boolean isPreSaveDoingCheckOut(Document doc, boolean isDirty, VersioningOption option,
Map<String, Serializable> options) {
boolean disableAutoCheckOut = Boolean.TRUE.equals(options.get(VersioningService.DISABLE_AUTO_CHECKOUT));
return !doc.isCheckedOut() && isDirty && !disableAutoCheckOut;
}
@Override
public VersioningOption doPreSave(Document doc, boolean isDirty, VersioningOption option,
String checkinComment, Map<String, Serializable> options) {
return doPreSave(null, doc, isDirty, option, checkinComment, options);
}
@Override
public VersioningOption doPreSave(CoreSession session, Document doc, boolean isDirty, VersioningOption option,
String checkinComment, Map<String, Serializable> options) {
option = validateOption(doc, option);
if (isPreSaveDoingCheckOut(doc, isDirty, option, options)) {
doCheckOut(doc);
followTransitionByOption(session, doc, options);
}
// transition follow shouldn't change what postSave options will be
return option;
}
protected void followTransitionByOption(CoreSession session, Document doc, Map<String, Serializable> options) {
String lifecycleState = doc.getLifeCycleState();
if ((APPROVED_STATE.equals(lifecycleState) || OBSOLETE_STATE.equals(lifecycleState))
&& doc.getAllowedStateTransitions().contains(BACK_TO_PROJECT_TRANSITION)) {
doc.followTransition(BACK_TO_PROJECT_TRANSITION);
if (session != null) {
// Send an event to notify that the document state has changed
sendEvent(session, doc, lifecycleState, options);
}
}
}
@Override
public boolean isPostSaveDoingCheckIn(Document doc, VersioningOption option, Map<String, Serializable> options) {
// option = validateOption(doc, option); // validated before
return doc.isCheckedOut() && option != NONE;
}
@Override
public Document doPostSave(Document doc, VersioningOption option, String checkinComment,
Map<String, Serializable> options) {
return doPostSave(null, doc, option, checkinComment, options);
}
@Override
public Document doPostSave(CoreSession session, Document doc, VersioningOption option, String checkinComment,
Map<String, Serializable> options) {
if (isPostSaveDoingCheckIn(doc, option, options)) {
incrementByOption(doc, option);
return doc.checkIn(null, checkinComment); // auto-label
}
return null;
}
@Override
public Document doCheckIn(Document doc, VersioningOption option, String checkinComment) {
if (option != VersioningOption.NONE) {
incrementByOption(doc, option == MAJOR ? MAJOR : MINOR);
}
return doc.checkIn(null, checkinComment); // auto-label
}
@Override
public void doCheckOut(Document doc) {
Document base = doc.getBaseVersion();
doc.checkOut();
// set version number to that of the latest version
if (base.isLatestVersion()) {
// nothing to do, already at proper version
} else {
// this doc was restored from a non-latest version, find the latest one
Document last = doc.getLastVersion();
if (last != null) {
try {
setVersion(doc, getMajor(last), getMinor(last));
} catch (PropertyNotFoundException e) {
// ignore
}
}
}
}
@Override
public Map<String, VersioningRuleDescriptor> getVersioningRules() {
return versioningRules;
}
@Override
public void setVersioningRules(Map<String, VersioningRuleDescriptor> versioningRules) {
this.versioningRules = versioningRules;
}
@Override
public void setDefaultVersioningRule(DefaultVersioningRuleDescriptor defaultVersioningRule) {
this.defaultVersioningRule = defaultVersioningRule;
}
protected void sendEvent(CoreSession session, Document doc, String previousLifecycleState, Map<String, Serializable> options) {
String sid = session.getSessionId();
DocumentModel docModel = DocumentModelFactory.createDocumentModel(doc, sid, null);
DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), docModel);
ctx.setProperty(TRANSTION_EVENT_OPTION_FROM, previousLifecycleState);
ctx.setProperty(TRANSTION_EVENT_OPTION_TO, doc.getLifeCycleState());
ctx.setProperty(TRANSTION_EVENT_OPTION_TRANSITION, BACK_TO_PROJECT_TRANSITION);
ctx.setProperty(REPOSITORY_NAME, session.getRepositoryName());
ctx.setProperty(SESSION_ID, sid);
ctx.setProperty(DOC_LIFE_CYCLE, BACK_TO_PROJECT_TRANSITION);
ctx.setProperty(CATEGORY, DocumentEventCategories.EVENT_LIFE_CYCLE_CATEGORY);
ctx.setProperty(COMMENT, options.get(COMMENT));
Framework.getService(EventService.class).fireEvent(ctx.newEvent(TRANSITION_EVENT));
}
}