View Javadoc
1   /*
2    * Copyright (C) 2003-2014 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.wcm.skin;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.LinkedList;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Map.Entry;
27  
28  import org.exoplatform.portal.resource.SkinConfig;
29  import org.exoplatform.portal.resource.SkinKey;
30  import org.exoplatform.portal.resource.SkinVisitor;
31  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32  
33  /**
34   * Created by The eXo Platform SAS
35   * Author : eXoPlatform
36   *          exo@exoplatform.com
37   * Jun 16, 2014
38   */
39  public class WCMSkinVisitor implements SkinVisitor{
40  
41      private List<SkinConfig> wcmSiteSkins = new ArrayList<SkinConfig>();
42      private List<SkinConfig> wcmSharedSkins = new ArrayList<SkinConfig>();
43      
44      private String skinName;
45      private String moduleName;
46  
47      public WCMSkinVisitor(String siteName,String skinName){
48        this.skinName = skinName;
49        this.moduleName = XSkinService.createModuleName(siteName);
50      }
51  
52      @Override
53      public void visitPortalSkin(Entry<SkinKey, SkinConfig> entry) {
54        String name = entry.getKey().getName();
55        if (name.equals(this.skinName))
56          visit(wcmSharedSkins, entry);
57      }
58  
59      @Override
60      public void visitSkin(Entry<SkinKey, SkinConfig> entry) {
61        String module = entry.getKey().getModule();
62        String name = entry.getKey().getName();
63        if (name.equals(this.skinName) && module.equals(moduleName))
64            visit(wcmSiteSkins,entry);
65      }
66  
67      @Override
68      public Collection<SkinConfig> getSkins() {
69        List<SkinConfig> skins = new LinkedList<SkinConfig>();
70        Comparator<SkinConfig> skinComparator = new Comparator<SkinConfig>(){
71          @Override
72          public int compare(SkinConfig o1, SkinConfig o2) {
73            return o1.getCSSPriority() - o2.getCSSPriority();
74          }}; 
75              
76        Collections.sort(wcmSharedSkins, skinComparator);
77        skins.addAll(wcmSharedSkins);
78        
79        Collections.sort(wcmSiteSkins, skinComparator);
80        skins.addAll(wcmSiteSkins);
81        
82        return skins;
83      }
84  
85      private void visit(Collection<SkinConfig> skins, Entry<SkinKey, SkinConfig> entry){
86        String currentContext = WCMCoreUtils.getRepository().getConfiguration().getName();
87        String cssPath = entry.getValue().getCSSPath();
88        Map<String,String> params = XSkinService.getSkinParams(cssPath);
89        if (cssPath.matches(XSkinService.SKIN_PATH_REGEXP)){
90          if (!params.get(XSkinService.CONTEXT_PARAM).equals(currentContext))
91            return;
92        }
93        skins.add(entry.getValue());
94      }
95  }
96  
97