View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (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 General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.core.impl;
18  
19  import javax.jcr.Node;
20  import javax.jcr.Property;
21  
22  import org.apache.commons.chain.Context;
23  import org.exoplatform.services.command.action.Action;
24  import org.exoplatform.services.jcr.ext.common.SessionProvider;
25  import org.exoplatform.services.log.ExoLogger;
26  import org.exoplatform.services.log.Log;
27  import org.exoplatform.services.wcm.core.NodetypeConstant;
28  import org.exoplatform.services.wcm.core.WebSchemaConfigService;
29  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
30  /**
31   * Created by The eXo Platform SAS
32   * Author : Hoa Pham
33   *          hoa.pham@exoplatform.com
34   * Sep 17, 2008
35   */
36  public class WebSchemaModificationAction implements Action{
37    private static final Log LOG = ExoLogger.getLogger(WebSchemaModificationAction.class.getName());
38    public boolean execute(Context context) throws Exception {
39      Property property = (Property)context.get("currentItem");
40      String propertyName = property.getName();
41  
42      if (!propertyName.equals("jcr:data")
43          && !propertyName.equals(NodetypeConstant.EXO_PRIORITY)
44          && !propertyName.equals(NodetypeConstant.EXO_ACTIVE)
45          && !propertyName.equals("exo:restorePath")) {
46  
47        // use exo:active in case of exo:cssFile or exo:jsFile
48        return propertyName.equalsIgnoreCase("exo:active");
49      }
50      Node grandParent = property.getParent().getParent();
51      if(propertyName.equals("jcr:data") && !grandParent.getPrimaryNodeType().getName().equals("nt:file"))
52        return false;
53  
54      WebSchemaConfigService schemaConfigService = WCMCoreUtils.getService(WebSchemaConfigService.class);
55      SessionProvider sessionProvider = WCMCoreUtils.getSystemSessionProvider();
56  
57      Node node = null;
58      if (propertyName.equals("jcr:data")) {
59        node = grandParent;
60      } else {
61        node = property.getParent();
62      }
63  
64      try {
65        if (propertyName.equals("exo:restorePath")) {
66          schemaConfigService.updateSchemaOnRemove(sessionProvider, node);
67        } else {
68          schemaConfigService.updateSchemaOnModify(sessionProvider, node);
69        }
70      } catch (Exception e) {
71        if (LOG.isErrorEnabled()) {
72          LOG.error("Error when update schema when modify node: "+node.getPath(), e);
73        }
74      }
75      return true;
76    }
77  
78  }