1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.communities.model;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.lar.PortletDataHandlerKeys;
31  import com.liferay.portal.lar.UserIdStrategy;
32  import com.liferay.portal.model.BaseModelListener;
33  import com.liferay.portal.model.Group;
34  import com.liferay.portal.model.GroupConstants;
35  import com.liferay.portal.model.LayoutSet;
36  import com.liferay.portal.service.GroupLocalServiceUtil;
37  import com.liferay.portal.service.LayoutLocalServiceUtil;
38  
39  import java.io.File;
40  
41  import java.util.LinkedHashMap;
42  import java.util.Map;
43  
44  /**
45   * <a href="CommunityTemplateModelListener.java.html"><b><i>View Source</i></b>
46   * </a>
47   *
48   * <p>
49   * A ModelListener that listens for creation of communities and attempts to
50   * prepopulate the community pages from a template community.
51   * </p>
52   *
53   * <p>
54   * The template community should be a private community to avoid unauthorized
55   * access. The templated pages are stored in the community's staging area to
56   * avoid users accidentally coming to this community.
57   * </p>
58   *
59   * <p>
60   * You may create a separate template for private, open, and protected
61   * communities. You may also create a default template that will apply if the
62   * open, private, and restricted templates are not defined. The template
63   * community names must be: DEFAULT_TEMPLATE, OPEN_TEMPLATE, PRIVATE_TEMPLATE,
64   * or RESTRICTED_TEMPLATE.
65   * </p>
66   *
67   * <p>
68   * A newly created community will have its layouts preconfigured based on its
69   * type. If community is public, templates pages from OPEN_TEMPLATE will be
70   * used. If community is restricted, template pages from RESTRICTED_TEMPLATE
71   * will be used. If community is private, template pages from PRIVATE_TEMPLATE
72   * will be used. If any of the above templates are not found, the
73   * DEFAULT_TEMPLATE will be used. If there are no templates, then nothing is
74   * done.
75   * </p>
76   *
77   * @author Michael C. Han
78   */
79  public class CommunityTemplateModelListener
80      extends BaseModelListener<LayoutSet> {
81  
82      public CommunityTemplateModelListener() {
83          _templateParameters = getTemplateParameters();
84      }
85  
86      public void onAfterCreate(LayoutSet layoutSet) {
87          File file = null;
88  
89          try {
90              Group group = GroupLocalServiceUtil.getGroup(
91                  layoutSet.getGroupId());
92  
93              if (!group.isCommunity() ||
94                  group.getName().contains(_TEMPLATE_POSTFIX)) {
95  
96                  return;
97              }
98  
99              Group templateGroup = getTemplateGroup(group);
100 
101             if (templateGroup == null) {
102                 return;
103             }
104 
105             Group templateStagingGroup = templateGroup.getStagingGroup();
106 
107             if (templateStagingGroup == null) {
108                 return;
109             }
110 
111             file = LayoutLocalServiceUtil.exportLayoutsAsFile(
112                 templateStagingGroup.getGroupId(), layoutSet.isPrivateLayout(),
113                 null, _templateParameters, null, null);
114 
115             LayoutLocalServiceUtil.importLayouts(
116                 group.getCreatorUserId(), group.getGroupId(),
117                 layoutSet.isPrivateLayout(), _templateParameters, file);
118         }
119         catch (Exception e) {
120             _log.error(
121                 "Unble to import layouts for group " + layoutSet.getGroupId(),
122                 e);
123         }
124         finally {
125             if (file != null) {
126                 file.delete();
127             }
128         }
129     }
130 
131     protected Group getTemplateGroup(Group group)
132         throws PortalException, SystemException {
133 
134         String templateCommunityName = null;
135 
136         int type = group.getType();
137 
138         if (type == GroupConstants.TYPE_COMMUNITY_OPEN) {
139             templateCommunityName = _OPEN_TEMPLATE_COMMUNITY_NAME;
140         }
141         else if (type == GroupConstants.TYPE_COMMUNITY_PRIVATE) {
142             templateCommunityName = _PRIVATE_TEMPLATE_COMMUNITY_NAME;
143         }
144         else if (type == GroupConstants.TYPE_COMMUNITY_RESTRICTED) {
145             templateCommunityName = _RESTRICTED_TEMPLATE_COMMUNITY_NAME;
146         }
147         else {
148             throw new IllegalArgumentException(
149                 "Invalid community type " + group.getType());
150         }
151 
152         Group templateGroup = null;
153 
154         try {
155             templateGroup = GroupLocalServiceUtil.getGroup(
156                 group.getCompanyId(), templateCommunityName);
157         }
158         catch (NoSuchGroupException nsge1) {
159             try {
160                 templateGroup = GroupLocalServiceUtil.getGroup(
161                     group.getCompanyId(), _DEFAULT_TEMPLATE_COMMUNITY_NAME);
162             }
163             catch (NoSuchGroupException nsge2) {
164             }
165         }
166 
167         return templateGroup;
168     }
169 
170     protected Map<String, String[]> getTemplateParameters() {
171         Map<String, String[]> parameterMap =
172             new LinkedHashMap<String, String[]>();
173 
174         parameterMap.put(
175             PortletDataHandlerKeys.CATEGORIES,
176             new String[] {Boolean.TRUE.toString()});
177         parameterMap.put(
178             PortletDataHandlerKeys.DATA_STRATEGY,
179             new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
180         parameterMap.put(
181             PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
182             new String[] {Boolean.FALSE.toString()});
183         parameterMap.put(
184             PortletDataHandlerKeys.DELETE_PORTLET_DATA,
185             new String[] {Boolean.FALSE.toString()});
186         parameterMap.put(
187             PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
188             new String[] {
189                 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_NAME
190             });
191         parameterMap.put(
192             PortletDataHandlerKeys.PERMISSIONS,
193             new String[] {Boolean.TRUE.toString()});
194         parameterMap.put(
195             PortletDataHandlerKeys.PORTLET_DATA,
196             new String[] {Boolean.TRUE.toString()});
197         parameterMap.put(
198             PortletDataHandlerKeys.PORTLET_DATA_ALL,
199             new String[] {Boolean.TRUE.toString()});
200         parameterMap.put(
201             PortletDataHandlerKeys.PORTLET_SETUP,
202             new String[] {Boolean.TRUE.toString()});
203         parameterMap.put(
204             PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
205             new String[] {Boolean.TRUE.toString()});
206         parameterMap.put(
207             PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
208             new String[] {
209                 PortletDataHandlerKeys.PORTLETS_MERGE_MODE_ADD_TO_BOTTOM
210             });
211         parameterMap.put(
212             PortletDataHandlerKeys.THEME,
213             new String[] {Boolean.FALSE.toString()});
214         parameterMap.put(
215             PortletDataHandlerKeys.USER_ID_STRATEGY,
216             new String[] {UserIdStrategy.CURRENT_USER_ID});
217         parameterMap.put(
218             PortletDataHandlerKeys.USER_PERMISSIONS,
219             new String[] {Boolean.FALSE.toString()});
220 
221         return parameterMap;
222     }
223 
224     private static final String _DEFAULT_TEMPLATE_COMMUNITY_NAME =
225         "DEFAULT_TEMPLATE";
226 
227     private static final String _OPEN_TEMPLATE_COMMUNITY_NAME =
228         "OPEN_TEMPLATE";
229 
230     private static final String _PRIVATE_TEMPLATE_COMMUNITY_NAME =
231         "PRIVATE_TEMPLATE";
232 
233     private static final String _RESTRICTED_TEMPLATE_COMMUNITY_NAME =
234         "RESTRICTED_TEMPLATE";
235 
236     private static final String _TEMPLATE_POSTFIX = "_TEMPLATE";
237 
238     private static Log _log =
239         LogFactoryUtil.getLog(CommunityTemplateModelListener.class);
240 
241     private Map<String, String[]> _templateParameters;
242 
243 }