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 javax.jcr.Item;
20  import javax.jcr.Node;
21  import javax.jcr.Property;
22  import javax.jcr.Session;
23  
24  import org.apache.commons.chain.Context;
25  import org.exoplatform.services.command.action.Action;
26  import org.exoplatform.services.jcr.ext.common.SessionProvider;
27  import org.exoplatform.services.listener.ListenerService;
28  import org.exoplatform.services.security.ConversationState;
29  import org.exoplatform.services.wcm.core.NodetypeConstant;
30  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
31  
32  public class EditFilePropertyActivityAction implements Action{
33    private ListenerService listenerService=null;
34    private ActivityCommonService activityService = null;
35    public EditFilePropertyActivityAction() {
36      listenerService =  WCMCoreUtils.getService(ListenerService.class);
37      activityService = WCMCoreUtils.getService(ActivityCommonService.class);
38    }
39    @Override
40    public boolean execute(Context context) throws Exception {
41      Item item = (Item) context.get("currentItem");
42      Node node = (item instanceof Property) ? item.getParent() : (Node) item;
43      String propertyName = item.getName();
44  
45      // Do not create / update activity for bellow cases
46      if (!activityService.isAcceptedFileProperties(propertyName) || ConversationState.getCurrent() == null) return false;
47  
48      if(node.isNodeType(NodetypeConstant.NT_RESOURCE)) node = node.getParent();
49      if(!node.getPrimaryNodeType().getName().equals(NodetypeConstant.NT_FILE)) return false;
50      if(propertyName.equals(NodetypeConstant.JCR_DATA)) {
51        SessionProvider systemSessionProvider = WCMCoreUtils.getSystemSessionProvider();
52        Session systemSession = systemSessionProvider.getSession(node.getSession().getWorkspace().getName(), WCMCoreUtils.getRepository());
53  
54        Node parent = systemSession.itemExists(node.getPath()) ? ((Node)systemSession.getItem(node.getPath())).getParent() : node.getParent();
55  
56        if(parent.hasNode(NodetypeConstant.EXO_THUMBNAILS_FOLDER)) {
57          Node thumnail = parent.getNode(NodetypeConstant.EXO_THUMBNAILS_FOLDER);
58          if(thumnail.hasNode(node.getUUID())) thumnail.getNode(node.getUUID()).remove();
59          parent.save();
60        }
61      }
62      //Notify to update activity
63      if(node.getPrimaryNodeType().getName().equals(NodetypeConstant.NT_FILE) && activityService.isBroadcastNTFileEvents(node) 
64          && !activityService.isCreating(node)) {
65        listenerService.broadcast(ActivityCommonService.FILE_EDIT_ACTIVITY, context, propertyName);
66      }    
67      return false;
68    }
69  
70  }