1 package org.exoplatform.services.cms.documents.impl;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.version.Version;
10 import javax.jcr.version.VersionHistory;
11
12 import org.apache.commons.lang.StringUtils;
13
14 import org.exoplatform.container.xml.InitParams;
15 import org.exoplatform.services.cms.documents.AutoVersionService;
16 import org.exoplatform.services.cms.documents.VersionHistoryUtils;
17 import org.exoplatform.services.cms.drives.DriveData;
18 import org.exoplatform.services.cms.drives.ManageDriveService;
19 import org.exoplatform.services.log.ExoLogger;
20 import org.exoplatform.services.log.Log;
21 import org.exoplatform.services.wcm.core.NodetypeConstant;
22 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
23
24
25
26
27
28
29
30 public class AutoVersionServiceImpl implements AutoVersionService{
31
32 private ManageDriveService manageDriveService;
33 private List<String> lstDriveAutoVersion = new ArrayList<String>();
34 private int maxVersionNumber=0;
35
36 public AutoVersionServiceImpl(ManageDriveService manageDriveService, InitParams params) {
37 this.manageDriveService = manageDriveService;
38 String driveAutoVersion = params.getValueParam(DRIVES_AUTO_VERSION).getValue();
39 maxVersionNumber = Integer.parseInt(params.getValueParam(DRIVES_AUTO_VERSION_MAX).getValue());
40 if(StringUtils.isNotEmpty(driveAutoVersion)) lstDriveAutoVersion = Arrays.asList(driveAutoVersion.split(","));
41 }
42
43
44
45
46 @Override
47 public Version autoVersion(Node currentNode) throws Exception {
48 return autoVersion(currentNode,false);
49 }
50
51
52
53
54 @Override
55 public Version autoVersion(Node currentNode, boolean isSkipCheckDrive) throws Exception {
56 manageDriveService = WCMCoreUtils.getService(ManageDriveService.class);
57 if(currentNode.canAddMixin(NodetypeConstant.MIX_REFERENCEABLE)){
58 currentNode.addMixin(NodetypeConstant.MIX_REFERENCEABLE);
59 }
60
61 if(isSkipCheckDrive){
62 Version createdVersion = VersionHistoryUtils.createVersion(currentNode);
63 addRootVersionLabelsToCreatedVersion(currentNode, createdVersion);
64 return createdVersion;
65 }
66 String nodePath = currentNode.getPath();
67 for (String driveAutoVersion: lstDriveAutoVersion){
68 DriveData driveData = manageDriveService.getDriveByName(StringUtils.trim(driveAutoVersion));
69 if(driveData==null) continue;
70 String driveHomePath = driveData.getHomePath();
71 if(!StringUtils.equals(driveData.getWorkspace(), currentNode.getSession().getWorkspace().getName())) continue;
72 if((driveHomePath.startsWith(PERSONAL_DRIVE_PARRTEN) && nodePath.startsWith(PERSONAL_DRIVE_PREFIX)) ||
73 driveHomePath.startsWith(GROUP_DRIVE_PARRTEN) && nodePath.startsWith(GROUP_DRIVE_PREFIX) ||
74 nodePath.startsWith(driveHomePath)){
75 Version createdVersion = VersionHistoryUtils.createVersion(currentNode);
76 addRootVersionLabelsToCreatedVersion(currentNode, createdVersion);
77 return createdVersion;
78 }
79 }
80 return null;
81 }
82
83
84
85
86 @Override
87 public boolean isVersionSupport(String nodePath, String workspace) throws Exception {
88 if(StringUtils.isEmpty(nodePath)) return false;
89 for (String driveAutoVersion: lstDriveAutoVersion){
90 DriveData driveData = manageDriveService.getDriveByName(StringUtils.trim(driveAutoVersion));
91 if(driveData==null) continue;
92 String driveHomePath = driveData.getHomePath();
93 if(!StringUtils.equals(driveData.getWorkspace(), workspace)) continue;
94 if((driveHomePath.startsWith(PERSONAL_DRIVE_PARRTEN) && nodePath.startsWith(PERSONAL_DRIVE_PREFIX)) ||
95 driveHomePath.startsWith(GROUP_DRIVE_PARRTEN) && nodePath.startsWith(GROUP_DRIVE_PREFIX) ||
96 nodePath.startsWith(driveHomePath)){
97 return true;
98 }
99 }
100 return false;
101 }
102
103 @Override
104 public List<String> getDriveAutoVersion()
105 {
106 return lstDriveAutoVersion;
107 }
108 @Override
109 public void autoVersion(Node currentNode, Node sourceNode) throws Exception {
110 autoVersion(currentNode,sourceNode,false);
111 }
112 @Override
113 public void autoVersion(Node currentNode, Node sourceNode, boolean isSkipDriveCheck) throws Exception {
114 manageDriveService = WCMCoreUtils.getService(ManageDriveService.class);
115 if(currentNode.canAddMixin(NodetypeConstant.MIX_REFERENCEABLE)){
116 currentNode.addMixin(NodetypeConstant.MIX_REFERENCEABLE);
117 currentNode.save();
118 }
119 if(isSkipDriveCheck){
120 createVersion(currentNode, sourceNode);
121 return;
122 }
123 for (String driveAutoVersion: lstDriveAutoVersion){
124 DriveData driveData = manageDriveService.getDriveByName(StringUtils.trim(driveAutoVersion));
125 if(driveData==null) continue;
126 String driveHomePath = driveData.getHomePath();
127 String nodePath = currentNode.getPath();
128 if(!StringUtils.equals(driveData.getWorkspace(), currentNode.getSession().getWorkspace().getName())) continue;
129 if((driveHomePath.startsWith(PERSONAL_DRIVE_PARRTEN) && nodePath.startsWith(PERSONAL_DRIVE_PREFIX)) ||
130 driveHomePath.startsWith(GROUP_DRIVE_PARRTEN) && nodePath.startsWith(GROUP_DRIVE_PREFIX) ||
131 nodePath.startsWith(driveHomePath)){
132 createVersion(currentNode, sourceNode);
133 return;
134 }
135 }
136 }
137
138
139
140
141
142
143
144
145 private boolean createVersion(Node currentNode, Node sourceNode)throws Exception{
146 if(currentNode.canAddMixin(NodetypeConstant.MIX_VERSIONABLE)){
147 currentNode.addMixin(NodetypeConstant.MIX_VERSIONABLE);
148 currentNode.save();
149 return true;
150 }
151 long allCurrentVersions = currentNode.getVersionHistory().getAllVersions().getSize();
152 if(maxVersionNumber==DOCUMENT_AUTO_DEFAULT_VERSION_MAX || maxVersionNumber >= allCurrentVersions){
153 VersionHistoryUtils.createVersion(currentNode);
154 Node jcrContent = currentNode.hasNode(NodetypeConstant.JCR_CONTENT)?
155 currentNode.getNode(NodetypeConstant.JCR_CONTENT):currentNode.addNode(NodetypeConstant.JCR_CONTENT);
156 Node srcJcrContent = sourceNode.getNode(NodetypeConstant.JCR_CONTENT);
157 if(srcJcrContent.getProperty(NodetypeConstant.JCR_DATA).getStream().available()>0) {
158 jcrContent.setProperty(NodetypeConstant.JCR_DATA, srcJcrContent.getProperty(NodetypeConstant.JCR_DATA).getStream());
159 }
160 currentNode.save();
161 return true;
162 }
163 return false;
164 }
165
166 private void addRootVersionLabelsToCreatedVersion(Node currentNode, Version createdVersion) throws RepositoryException {
167 VersionHistory versionHistory = currentNode.getVersionHistory();
168 String[] oldVersionLabels = versionHistory.getVersionLabels(versionHistory.getRootVersion());
169 if(oldVersionLabels != null) {
170 for (String oldVersionLabel : oldVersionLabels) {
171 versionHistory.addVersionLabel(createdVersion.getName(), oldVersionLabel, true);
172 currentNode.save();
173 }
174 }
175 }
176 }