001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.RequiredLayoutSetPrototypeException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.systemevent.SystemEvent;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.GroupConstants;
027    import com.liferay.portal.model.LayoutConstants;
028    import com.liferay.portal.model.LayoutSetPrototype;
029    import com.liferay.portal.model.ResourceConstants;
030    import com.liferay.portal.model.SystemEventConstants;
031    import com.liferay.portal.model.User;
032    import com.liferay.portal.security.permission.PermissionCacheUtil;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.base.LayoutSetPrototypeLocalServiceBaseImpl;
035    
036    import java.util.Date;
037    import java.util.List;
038    import java.util.Locale;
039    import java.util.Map;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Ryan Park
044     */
045    public class LayoutSetPrototypeLocalServiceImpl
046            extends LayoutSetPrototypeLocalServiceBaseImpl {
047    
048            @Override
049            public LayoutSetPrototype addLayoutSetPrototype(
050                            long userId, long companyId, Map<Locale, String> nameMap,
051                            String description, boolean active, boolean layoutsUpdateable,
052                            ServiceContext serviceContext)
053                    throws PortalException, SystemException {
054    
055                    // Layout set prototype
056    
057                    User user = userPersistence.findByPrimaryKey(userId);
058                    Date now = new Date();
059    
060                    long layoutSetPrototypeId = counterLocalService.increment();
061    
062                    LayoutSetPrototype layoutSetPrototype =
063                            layoutSetPrototypePersistence.create(layoutSetPrototypeId);
064    
065                    layoutSetPrototype.setUuid(serviceContext.getUuid());
066                    layoutSetPrototype.setCompanyId(companyId);
067                    layoutSetPrototype.setUserId(userId);
068                    layoutSetPrototype.setUserName(user.getFullName());
069                    layoutSetPrototype.setCreateDate(serviceContext.getCreateDate(now));
070                    layoutSetPrototype.setModifiedDate(serviceContext.getModifiedDate(now));
071                    layoutSetPrototype.setNameMap(nameMap);
072                    layoutSetPrototype.setDescription(description);
073                    layoutSetPrototype.setActive(active);
074    
075                    UnicodeProperties settingsProperties =
076                            layoutSetPrototype.getSettingsProperties();
077    
078                    settingsProperties.put(
079                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
080    
081                    layoutSetPrototype.setSettingsProperties(settingsProperties);
082    
083                    layoutSetPrototypePersistence.update(layoutSetPrototype);
084    
085                    // Resources
086    
087                    resourceLocalService.addResources(
088                            companyId, 0, userId, LayoutSetPrototype.class.getName(),
089                            layoutSetPrototype.getLayoutSetPrototypeId(), false, true, false);
090    
091                    // Group
092    
093                    String friendlyURL =
094                            "/template-" + layoutSetPrototype.getLayoutSetPrototypeId();
095    
096                    Group group = groupLocalService.addGroup(
097                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
098                            LayoutSetPrototype.class.getName(),
099                            layoutSetPrototype.getLayoutSetPrototypeId(),
100                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
101                            layoutSetPrototype.getName(LocaleUtil.getDefault()), null, 0, true,
102                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendlyURL, false,
103                            true, serviceContext);
104    
105                    if (GetterUtil.getBoolean(
106                                    serviceContext.getAttribute("addDefaultLayout"), true)) {
107    
108                            layoutLocalService.addLayout(
109                                    userId, group.getGroupId(), true,
110                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "home", null, null,
111                                    LayoutConstants.TYPE_PORTLET, false, "/home", serviceContext);
112                    }
113    
114                    return layoutSetPrototype;
115            }
116    
117            @Override
118            @SystemEvent(
119                    action = SystemEventConstants.ACTION_SKIP,
120                    type = SystemEventConstants.TYPE_DELETE)
121            public LayoutSetPrototype deleteLayoutSetPrototype(
122                            LayoutSetPrototype layoutSetPrototype)
123                    throws PortalException, SystemException {
124    
125                    // Group
126    
127                    if (layoutSetPersistence.countByLayoutSetPrototypeUuid(
128                                    layoutSetPrototype.getUuid()) > 0) {
129    
130                            throw new RequiredLayoutSetPrototypeException();
131                    }
132    
133                    Group group = layoutSetPrototype.getGroup();
134    
135                    groupLocalService.deleteGroup(group);
136    
137                    // Resources
138    
139                    resourceLocalService.deleteResource(
140                            layoutSetPrototype.getCompanyId(),
141                            LayoutSetPrototype.class.getName(),
142                            ResourceConstants.SCOPE_INDIVIDUAL,
143                            layoutSetPrototype.getLayoutSetPrototypeId());
144    
145                    // Layout set prototype
146    
147                    layoutSetPrototypePersistence.remove(layoutSetPrototype);
148    
149                    // Permission cache
150    
151                    PermissionCacheUtil.clearCache();
152    
153                    return layoutSetPrototype;
154            }
155    
156            @Override
157            public LayoutSetPrototype deleteLayoutSetPrototype(
158                            long layoutSetPrototypeId)
159                    throws PortalException, SystemException {
160    
161                    LayoutSetPrototype layoutSetPrototype =
162                            layoutSetPrototypePersistence.findByPrimaryKey(
163                                    layoutSetPrototypeId);
164    
165                    return deleteLayoutSetPrototype(layoutSetPrototype);
166            }
167    
168            @Override
169            public void deleteLayoutSetPrototypes()
170                    throws PortalException, SystemException {
171    
172                    List<LayoutSetPrototype> layoutSetPrototypes =
173                            layoutSetPrototypePersistence.findAll();
174    
175                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
176                            layoutSetPrototypeLocalService.deleteLayoutSetPrototype(
177                                    layoutSetPrototype);
178                    }
179            }
180    
181            @Override
182            public void deleteNondefaultLayoutSetPrototypes(long companyId)
183                    throws PortalException, SystemException {
184    
185                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
186    
187                    List<LayoutSetPrototype> layoutSetPrototypes =
188                            layoutSetPrototypePersistence.findByCompanyId(companyId);
189    
190                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
191                            if (layoutSetPrototype.getUserId() != defaultUserId) {
192                                    deleteLayoutSetPrototype(layoutSetPrototype);
193                            }
194                    }
195            }
196    
197            /**
198             * @deprecated As of 6.2.0, replaced by {@link
199             *             #getLayoutSetPrototypeByUuidAndCompanyId(String, long)}
200             */
201            @Override
202            public LayoutSetPrototype getLayoutSetPrototypeByUuid(String uuid)
203                    throws PortalException, SystemException {
204    
205                    return layoutSetPrototypePersistence.findByUuid_First(uuid, null);
206            }
207    
208            @Override
209            public LayoutSetPrototype getLayoutSetPrototypeByUuidAndCompanyId(
210                            String uuid, long companyId)
211                    throws PortalException, SystemException {
212    
213                    return layoutSetPrototypePersistence.findByUuid_C_First(
214                            uuid, companyId, null);
215            }
216    
217            @Override
218            public List<LayoutSetPrototype> getLayoutSetPrototypes(long companyId)
219                    throws SystemException {
220    
221                    return layoutSetPrototypePersistence.findByCompanyId(companyId);
222            }
223    
224            @Override
225            public List<LayoutSetPrototype> search(
226                            long companyId, Boolean active, int start, int end,
227                            OrderByComparator obc)
228                    throws SystemException {
229    
230                    if (active != null) {
231                            return layoutSetPrototypePersistence.findByC_A(
232                                    companyId, active, start, end, obc);
233                    }
234                    else {
235                            return layoutSetPrototypePersistence.findByCompanyId(
236                                    companyId, start, end, obc);
237                    }
238            }
239    
240            @Override
241            public int searchCount(long companyId, Boolean active)
242                    throws SystemException {
243    
244                    if (active != null) {
245                            return layoutSetPrototypePersistence.countByC_A(companyId, active);
246                    }
247                    else {
248                            return layoutSetPrototypePersistence.countByCompanyId(companyId);
249                    }
250            }
251    
252            @Override
253            public LayoutSetPrototype updateLayoutSetPrototype(
254                            long layoutSetPrototypeId, Map<Locale, String> nameMap,
255                            String description, boolean active, boolean layoutsUpdateable,
256                            ServiceContext serviceContext)
257                    throws PortalException, SystemException {
258    
259                    // Layout set prototype
260    
261                    LayoutSetPrototype layoutSetPrototype =
262                            layoutSetPrototypePersistence.findByPrimaryKey(
263                                    layoutSetPrototypeId);
264    
265                    layoutSetPrototype.setModifiedDate(
266                            serviceContext.getModifiedDate(new Date()));
267                    layoutSetPrototype.setNameMap(nameMap);
268                    layoutSetPrototype.setDescription(description);
269                    layoutSetPrototype.setActive(active);
270    
271                    UnicodeProperties settingsProperties =
272                            layoutSetPrototype.getSettingsProperties();
273    
274                    settingsProperties.put(
275                            "layoutsUpdateable", String.valueOf(layoutsUpdateable));
276    
277                    layoutSetPrototype.setSettingsProperties(settingsProperties);
278    
279                    layoutSetPrototypePersistence.update(layoutSetPrototype);
280    
281                    return layoutSetPrototype;
282            }
283    
284            @Override
285            public LayoutSetPrototype updateLayoutSetPrototype(
286                            long layoutSetPrototypeId, String settings)
287                    throws PortalException, SystemException {
288    
289                    // Layout set prototype
290    
291                    LayoutSetPrototype layoutSetPrototype =
292                            layoutSetPrototypePersistence.findByPrimaryKey(
293                                    layoutSetPrototypeId);
294    
295                    layoutSetPrototype.setModifiedDate(new Date());
296                    layoutSetPrototype.setSettings(settings);
297    
298                    layoutSetPrototypePersistence.update(layoutSetPrototype);
299    
300                    // Group
301    
302                    UnicodeProperties settingsProperties =
303                            layoutSetPrototype.getSettingsProperties();
304    
305                    if (!settingsProperties.containsKey("customJspServletContextName")) {
306                            return layoutSetPrototype;
307                    }
308    
309                    Group group = groupLocalService.getLayoutSetPrototypeGroup(
310                            layoutSetPrototype.getCompanyId(), layoutSetPrototypeId);
311    
312                    UnicodeProperties typeSettingsProperties =
313                            group.getTypeSettingsProperties();
314    
315                    typeSettingsProperties.setProperty(
316                            "customJspServletContextName",
317                            settingsProperties.getProperty("customJspServletContextName"));
318    
319                    group.setTypeSettings(typeSettingsProperties.toString());
320    
321                    groupPersistence.update(group);
322    
323                    return layoutSetPrototype;
324            }
325    
326    }