View Javadoc
1   /*
2    * Copyright (C) 2003-2013 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.cms.jcrext.activity;
18  
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import javax.jcr.AccessDeniedException;
26  import javax.jcr.Item;
27  import javax.jcr.Node;
28  import javax.jcr.RepositoryException;
29  
30  import org.exoplatform.container.xml.InitParams;
31  import org.exoplatform.services.cms.link.LinkManager;
32  import org.exoplatform.services.cms.templates.TemplateService;
33  import org.exoplatform.services.jcr.impl.core.NodeImpl;
34  import org.exoplatform.services.wcm.core.NodeLocation;
35  import org.exoplatform.services.wcm.core.NodetypeConstant;
36  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
37  
38  /**
39   * Created by The eXo Platform SAS
40   * Author : Nguyen The Vinh From ECM Of eXoPlatform
41   *          vinh_nguyen@exoplatform.com
42   * List of available activity event
43   * List of node-type with allow the activity raise on.
44   * 15 Jan 2013  
45   */
46  public class ActivityCommonService {
47    private String acceptedNodeTypes;
48    private String acceptedProperties;// = "{exo:summary}{exo:title}{exo:text}";
49    private String acceptedFileProperties;
50    public static String NT_FILE                       = "nt:file";
51    public static String WEB_CONTENT                   = "exo:webContent";
52    public static String ACCESSIBLE_MEDIA              = "exo:accessibleMedia";
53    public static String EDIT_ACTIVITY                 = "ActivityNotify.event.PropertyUpdated";
54    public static String FILE_EDIT_ACTIVITY            = "FileActivityNotify.event.PropertyUpdated";
55    public static String FILE_PROPERTY_REMOVE_ACTIVITY = "FileActivityNotify.event.PropertyRemoved";
56    public static String FILE_ADD_ACTIVITY             = "FileActivityNotify.event.PropertyAdded";
57    public static String FILE_REMOVE_ACTIVITY          = "FileActivityNotify.event.FileRemoved";
58    public static String FILE_CREATED_ACTIVITY         = "ActivityNotify.event.FileCreated";
59    
60    
61    public static String ATTACH_ADDED_ACTIVITY         = "ActivityNotify.event.AttachmentAdded";
62    public static String ATTACH_REMOVED_ACTIVITY       = "ActivityNotify.event.AttachmentRemoved";
63    
64    public static String NODE_CREATED_ACTIVITY      = "ActivityNotify.event.NodeCreated";
65    public static String NODE_REMOVED_ACTIVITY      = "ActivityNotify.event.NodeRemoved";
66    public static String NODE_MOVED_ACTIVITY        = "ActivityNotify.event.NodeMoved";
67    public static String NODE_REVISION_CHANGED      = "ActivityNotify.event.RevisionChanged";  
68    
69    public static String CATEGORY_ADDED_ACTIVITY    = "ActivityNotify.event.CategoryAdded";
70    public static String CATEGORY_REMOVED_ACTIVITY  = "ActivityNotify.event.CategoryRemoved";
71    
72    public static String TAG_ADDED_ACTIVITY         = "ActivityNotify.event.TagAdded";
73    public static String TAG_REMOVED_ACTIVITY       = "ActivityNotify.event.TagRemoved";
74    
75    public static String COMMENT_ADDED_ACTIVITY     = "ActivityNotify.event.CommentAdded";
76    public static String COMMENT_UPDATED_ACTIVITY   = "ActivityNotify.event.CommentUpdated";
77    public static String COMMENT_REMOVED_ACTIVITY   = "ActivityNotify.event.CommentRemoved";
78    
79    public static String STATE_CHANGED_ACTIVITY     = "ActivityNotify.event.StateChanged";
80    
81    public static String VALUE_SEPERATOR            = "_S#E#P#R_";
82    
83    public static String METADATA_VALUE_SEPERATOR   = "_M#S#E#P#R_";
84    
85    public static String MIX_COMMENT                = "exo:activityComment";
86    public static String MIX_COMMENT_CREATOR        = "exo:activityCreator";
87    public static String MIX_COMMENT_ACTIVITY_ID    = "exo:activityCommentID";
88    
89    private Map<String, Object> properties = new HashMap<String, Object>();
90    
91    private Set<Integer> creatingNodes = new HashSet<Integer>();
92  
93    private Set<Integer> editingNodes = new HashSet<Integer>();
94  
95    public Map<String, Object> getPreProperties() { return properties; }
96    
97    public void setPreProperties(Map<String, Object> preProperties) { properties = preProperties; }
98    
99    public ActivityCommonService(InitParams initParams) {
100     this.acceptedNodeTypes = initParams.getValueParam("acceptedNodeTypes").getValue();
101     this.acceptedProperties = initParams.getValueParam("acceptedProperties").getValue();
102     this.acceptedFileProperties = initParams.getValueParam("acceptedFileProperties").getValue();
103     if (acceptedNodeTypes==null) acceptedNodeTypes = "";
104     if (acceptedProperties==null) acceptedProperties =""; 
105     if (acceptedFileProperties==null) acceptedFileProperties ="";
106   }
107   
108   /**
109    * set the node status to Creating
110    * @param node
111    * @param isCreating
112    *  true if successful to setCreating
113    */
114   public void setCreating(Node node, boolean isCreating){
115     NodeLocation nodeLocation = NodeLocation.getNodeLocationByNode(node);
116     if(isCreating){
117       creatingNodes.add(nodeLocation.hashCode());
118       setEditing(node, true);
119     }else{
120       creatingNodes.remove(nodeLocation.hashCode());
121       setEditing(node, false);
122     }
123   }
124 
125   /**
126    * Set the node status to Editing
127    * Status Editing means node in status creating or editing
128    *
129    * @param node Node
130    * @param isEditing
131    *  true if successful to Editing
132    */
133   public void setEditing(Node node, boolean isEditing) {
134     NodeLocation nodeLocation = NodeLocation.getNodeLocationByNode(node);
135     if(isEditing){
136       editingNodes.add(nodeLocation.hashCode());
137     }else{
138       editingNodes.remove(nodeLocation.hashCode());
139     }
140   }
141 
142   /**
143    * return if the node is Creating
144    * @param node
145    * @return
146    *   true if the node is creating
147    */
148   public boolean isCreating(Node node){
149     LinkManager linkManager = WCMCoreUtils.getService(LinkManager.class);
150     Node realNode = node;
151     try {
152       if (linkManager.isLink(node)) {
153         realNode = linkManager.getTarget(node);
154       }
155       NodeLocation nodeLocation = NodeLocation.getNodeLocationByNode(realNode);
156       if (creatingNodes.contains(nodeLocation.hashCode())) {
157         return true;
158       }
159       List<Node> allLinks = linkManager.getAllLinks(realNode, "exo:symlink");
160       for (Node link : allLinks) {
161         nodeLocation = NodeLocation.getNodeLocationByNode(link);
162         nodeLocation.setUUID(realNode.getUUID());
163         if (creatingNodes.contains(nodeLocation.hashCode())){
164           return true;
165         }
166       }
167       return false;
168     } catch (Exception e) {
169       return false;
170     }
171   }
172 
173   /**
174    * Return if the node is Editing.
175    * Status Editing means node in status creating or editing
176    * @param node Node
177    * @return
178    *   true if the node is Editing
179    */
180   public boolean isEditing(Node node){
181     boolean isEditing = false;
182     NodeLocation nodeLocation = NodeLocation.getNodeLocationByNode(node);
183     isEditing = editingNodes.contains(nodeLocation.hashCode());
184     return isEditing;
185   }
186   
187   public boolean isAcceptedNode(Node node) {
188     try {
189       return node==null?false:acceptedNodeTypes.indexOf("{" + node.getPrimaryNodeType().getName() +"}")>=0;
190     } catch (RepositoryException e) {
191       return false;
192     }
193   }
194   private boolean isDocumentNodeType(Node node) throws Exception {
195   	boolean isBroadCast = true;
196     TemplateService templateService = WCMCoreUtils.getService(TemplateService.class);
197     isBroadCast = templateService.getAllDocumentNodeTypes().contains(node.getPrimaryNodeType().getName()); 
198     
199     if(!isBroadCast) {
200     	isBroadCast = !(node.isNodeType(NodetypeConstant.NT_UNSTRUCTURED) || node.isNodeType(NodetypeConstant.NT_FOLDER));
201     }
202     return isBroadCast;
203   }
204   
205   public boolean isBroadcastNTFileEvents(Node node) throws Exception {
206     boolean result = true;
207     while(result && !((NodeImpl)node).isRoot()) {
208       try{
209         node = node.getParent();
210         result = !isDocumentNodeType(node);
211       }catch (AccessDeniedException ex){
212         return result;
213       }catch (RepositoryException ex) {
214         return !isDocumentNodeType(node);
215       }
216     }
217     return result;
218   }
219   public boolean isAcceptedProperties(String propertyName) {
220     return (acceptedProperties.indexOf("{" + propertyName + "}")>=0);
221   }
222   public boolean isAcceptedFileProperties(String propertyName) {
223     return (acceptedFileProperties.indexOf("{" + propertyName + "}")>=0 || propertyName.startsWith("dc:"));  
224   }
225   
226   public Node isSpecialContentNodeType(Item item) {
227     String webSpecialContentPath = "default.html/jcr:content/jcr:data";
228     String specialSummaryCase    = "jcr:content/dc:description";
229     String path;
230     try {
231       path = item.getPath();
232     } catch (RepositoryException e1) {
233       return null;
234     }
235     if ( path.endsWith(webSpecialContentPath)) {
236       try {
237         Node node = (Node) item.getParent().getParent().getParent();
238         if (node.isNodeType(WEB_CONTENT)) return node;
239       }catch (Exception e) {
240         return null;
241       }
242     }else if (path.endsWith(specialSummaryCase)){
243       try {
244         Node node = (Node) item.getParent().getParent();
245         if (isAcceptedNode(node)) return node;
246       }catch (Exception e) {
247         return null;
248       }
249     }
250     return null;
251   }
252 }