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.skin;
18  
19  import javax.jcr.Node;
20  
21  import org.exoplatform.services.jcr.ext.common.SessionProvider;
22  import org.exoplatform.services.wcm.core.BaseWebSchemaHandler;
23  import org.exoplatform.services.wcm.core.WebSchemaConfigService;
24  import org.exoplatform.services.wcm.portal.PortalFolderSchemaHandler;
25  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
26  
27  /**
28   * Created by The eXo Platform SAS.
29   *
30   * @author : Hoa.Pham
31   * hoa.pham@exoplatform.com
32   * May 28, 2008
33   */
34  public class CSSFileHandler extends BaseWebSchemaHandler {
35  
36    /* (non-Javadoc)
37     * @see org.exoplatform.services.wcm.core.BaseWebSchemaHandler#getHandlerNodeType()
38     */
39    protected String getHandlerNodeType() { return "nt:file"; }
40  
41    /* (non-Javadoc)
42     * @see org.exoplatform.services.wcm.core.BaseWebSchemaHandler#getParentNodeType()
43     */
44    protected String getParentNodeType() { return "exo:cssFolder" ; }
45  
46    /** The is in portal css folder. */
47    private boolean isInPortalCSSFolder = false;
48  
49    /*
50     * (non-Javadoc)
51     * @see
52     * org.exoplatform.services.wcm.core.BaseWebSchemaHandler#matchHandler(javax
53     * .jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
54     */
55    public boolean matchHandler(SessionProvider sessionProvider, Node node) throws Exception {
56      if (!matchNodeType(node))
57        return false;
58      if (!matchMimeType(node))
59        return false;
60      isInPortalCSSFolder = matchPortalCSSFolder(sessionProvider, node);
61      if (isInPortalCSSFolder)
62        return true;
63      if (!matchParentNodeType(node)) {
64        return false;
65      }
66      return true;
67    }
68  
69    /**
70     * Match node type.
71     *
72     * @param node the node
73     *
74     * @return true, if successful
75     *
76     * @throws Exception the exception
77     */
78    private boolean matchNodeType(Node node) throws Exception{
79      return node.getPrimaryNodeType().getName().equals("nt:file");
80    }
81  
82    /**
83     * Match mime type.
84     *
85     * @param node the node
86     *
87     * @return true, if successful
88     *
89     * @throws Exception the exception
90     */
91    private boolean matchMimeType(Node node) throws Exception {
92      String mimeType = getFileMimeType(node);
93      if ("text/css".equals(mimeType))
94        return true;
95      if ("text/plain".equals(mimeType))
96        return true;
97      return false;
98    }
99  
100   /**
101    * Match parent node type.
102    *
103    * @param file the file
104    *
105    * @return true, if successful
106    *
107    * @throws Exception the exception
108    */
109   private boolean matchParentNodeType(Node file) throws Exception {
110     return file.getParent().isNodeType("exo:cssFolder");
111   }
112 
113   /**
114    * Match portal css folder.
115    *
116    * @param file the file
117    * @param sessionProvider the session provider
118    *
119    * @return true, if successful
120    *
121    * @throws Exception the exception
122    */
123   private boolean matchPortalCSSFolder(SessionProvider sessionProvider, Node file) throws Exception {
124     Node portal = findPortalNode(sessionProvider, file);
125     if (portal == null)
126       return false;
127     WebSchemaConfigService schemaConfigService = WCMCoreUtils.getService(WebSchemaConfigService.class);
128     PortalFolderSchemaHandler schemaHandler = schemaConfigService.getWebSchemaHandlerByType(PortalFolderSchemaHandler.class);
129     Node cssFolder = schemaHandler.getCSSFolder(portal);
130     return cssFolder == null ? false : file.getPath().startsWith(cssFolder.getPath());
131   }
132 
133   /*
134    * (non-Javadoc)
135    * @see
136    * org.exoplatform.services.wcm.core.BaseWebSchemaHandler#onCreateNode(javax
137    * .jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
138    */
139   public void onCreateNode(SessionProvider sessionProvider, Node file) throws Exception {
140     addMixin(file, "exo:cssFile");
141     addMixin(file,"exo:owneable");
142     file.setProperty("exo:presentationType","exo:cssFile");
143     if(isInPortalCSSFolder) {
144       file.setProperty("exo:sharedCSS",true);
145     }
146   }
147 
148   /*
149    * (non-Javadoc)
150    * @see
151    * org.exoplatform.services.wcm.core.BaseWebSchemaHandler#onModifyNode(javax
152    * .jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
153    */
154   public void onModifyNode(SessionProvider sessionProvider, Node file) throws Exception {
155     if(isInPortalCSSFolder) {
156       Node portal = findPortalNode(sessionProvider, file);
157       XSkinService skinService = WCMCoreUtils.getService(XSkinService.class);
158       skinService.updatePortalSkinOnModify(portal, file);
159     }
160   }
161 
162   /*
163    * (non-Javadoc)
164    * @see
165    * org.exoplatform.services.wcm.core.BaseWebSchemaHandler#onRemoveNode(javax
166    * .jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
167    */
168   public void onRemoveNode(SessionProvider sessionProvider, Node file) throws Exception {
169     if (isInPortalCSSFolder) {
170       XSkinService skinService = WCMCoreUtils.getService(XSkinService.class);
171       Node portal = findPortalNode(sessionProvider, file);
172       skinService.updatePortalSkinOnRemove(portal, file);
173     }
174   }
175 
176 }