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.RequiredLayoutPrototypeException;
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.model.Group;
025    import com.liferay.portal.model.GroupConstants;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.LayoutConstants;
028    import com.liferay.portal.model.LayoutPrototype;
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.LayoutPrototypeLocalServiceBaseImpl;
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 Jorge Ferrer
044     * @author Vilmos Papp
045     */
046    public class LayoutPrototypeLocalServiceImpl
047            extends LayoutPrototypeLocalServiceBaseImpl {
048    
049            /**
050             * @deprecated As of 6.2.0, replaced by {@link #addLayoutPrototype(long,
051             *             long, Map, String, boolean, ServiceContext)}
052             */
053            @Override
054            public LayoutPrototype addLayoutPrototype(
055                            long userId, long companyId, Map<Locale, String> nameMap,
056                            String description, boolean active)
057                    throws PortalException, SystemException {
058    
059                    return addLayoutPrototype(
060                            userId, companyId, nameMap, description, active,
061                            new ServiceContext());
062            }
063    
064            @Override
065            public LayoutPrototype addLayoutPrototype(
066                            long userId, long companyId, Map<Locale, String> nameMap,
067                            String description, boolean active, ServiceContext serviceContext)
068                    throws PortalException, SystemException {
069    
070                    // Layout prototype
071    
072                    User user = userPersistence.findByPrimaryKey(userId);
073                    Date now = new Date();
074    
075                    long layoutPrototypeId = counterLocalService.increment();
076    
077                    LayoutPrototype layoutPrototype = layoutPrototypePersistence.create(
078                            layoutPrototypeId);
079    
080                    layoutPrototype.setUuid(serviceContext.getUuid());
081                    layoutPrototype.setCompanyId(companyId);
082                    layoutPrototype.setUserId(userId);
083                    layoutPrototype.setUserName(user.getFullName());
084                    layoutPrototype.setCreateDate(serviceContext.getCreateDate(now));
085                    layoutPrototype.setModifiedDate(serviceContext.getModifiedDate(now));
086                    layoutPrototype.setNameMap(nameMap);
087                    layoutPrototype.setDescription(description);
088                    layoutPrototype.setActive(active);
089    
090                    layoutPrototypePersistence.update(layoutPrototype);
091    
092                    // Resources
093    
094                    resourceLocalService.addResources(
095                            companyId, 0, userId, LayoutPrototype.class.getName(),
096                            layoutPrototype.getLayoutPrototypeId(), false, true, false);
097    
098                    // Group
099    
100                    String friendlyURL =
101                            "/template-" + layoutPrototype.getLayoutPrototypeId();
102    
103                    Group group = groupLocalService.addGroup(
104                            userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
105                            LayoutPrototype.class.getName(),
106                            layoutPrototype.getLayoutPrototypeId(),
107                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
108                            layoutPrototype.getName(LocaleUtil.getDefault()), null, 0, true,
109                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendlyURL, false,
110                            true, null);
111    
112                    if (GetterUtil.getBoolean(
113                                    serviceContext.getAttribute("addDefaultLayout"), true)) {
114    
115                            layoutLocalService.addLayout(
116                                    userId, group.getGroupId(), true,
117                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
118                                    layoutPrototype.getName(LocaleUtil.getDefault()), null, null,
119                                    LayoutConstants.TYPE_PORTLET, false, "/layout", serviceContext);
120                    }
121    
122                    return layoutPrototype;
123            }
124    
125            @Override
126            @SystemEvent(
127                    action = SystemEventConstants.ACTION_SKIP,
128                    type = SystemEventConstants.TYPE_DELETE)
129            public LayoutPrototype deleteLayoutPrototype(
130                            LayoutPrototype layoutPrototype)
131                    throws PortalException, SystemException {
132    
133                    // Group
134    
135                    if (layoutPersistence.countByC_L(
136                                    layoutPrototype.getCompanyId(),
137                                    layoutPrototype.getUuid()) > 0) {
138    
139                            throw new RequiredLayoutPrototypeException();
140                    }
141    
142                    Group group = layoutPrototype.getGroup();
143    
144                    groupLocalService.deleteGroup(group);
145    
146                    // Resources
147    
148                    resourceLocalService.deleteResource(
149                            layoutPrototype.getCompanyId(), LayoutPrototype.class.getName(),
150                            ResourceConstants.SCOPE_INDIVIDUAL,
151                            layoutPrototype.getLayoutPrototypeId());
152    
153                    // Layout Prototype
154    
155                    layoutPrototypePersistence.remove(layoutPrototype);
156    
157                    // Permission cache
158    
159                    PermissionCacheUtil.clearCache();
160    
161                    return layoutPrototype;
162            }
163    
164            @Override
165            public LayoutPrototype deleteLayoutPrototype(long layoutPrototypeId)
166                    throws PortalException, SystemException {
167    
168                    LayoutPrototype layoutPrototype =
169                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
170    
171                    return deleteLayoutPrototype(layoutPrototype);
172            }
173    
174            @Override
175            public void deleteNondefaultLayoutPrototypes(long companyId)
176                    throws PortalException, SystemException {
177    
178                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
179    
180                    List<LayoutPrototype> layoutPrototypes =
181                            layoutPrototypePersistence.findByCompanyId(companyId);
182    
183                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
184                            if (layoutPrototype.getUserId() != defaultUserId) {
185                                    deleteLayoutPrototype(layoutPrototype);
186                            }
187                    }
188            }
189    
190            /**
191             * @deprecated As of 6.2.0, replaced by {@link
192             *             #getLayoutPrototypeByUuidAndCompanyId(String, long)}
193             */
194            @Override
195            public LayoutPrototype getLayoutPrototypeByUuid(String uuid)
196                    throws PortalException, SystemException {
197    
198                    return layoutPrototypePersistence.findByUuid_First(uuid, null);
199            }
200    
201            @Override
202            public LayoutPrototype getLayoutPrototypeByUuidAndCompanyId(
203                            String uuid, long companyId)
204                    throws PortalException, SystemException {
205    
206                    return layoutPrototypePersistence.findByUuid_C_First(
207                            uuid, companyId, null);
208            }
209    
210            @Override
211            public List<LayoutPrototype> search(
212                            long companyId, Boolean active, int start, int end,
213                            OrderByComparator obc)
214                    throws SystemException {
215    
216                    if (active != null) {
217                            return layoutPrototypePersistence.findByC_A(
218                                    companyId, active, start, end, obc);
219                    }
220                    else {
221                            return layoutPrototypePersistence.findByCompanyId(
222                                    companyId, start, end, obc);
223                    }
224            }
225    
226            @Override
227            public int searchCount(long companyId, Boolean active)
228                    throws SystemException {
229    
230                    if (active != null) {
231                            return layoutPrototypePersistence.countByC_A(companyId, active);
232                    }
233                    else {
234                            return layoutPrototypePersistence.countByCompanyId(companyId);
235                    }
236            }
237    
238            /**
239             * @deprecated As of 6.2.0, replaced by {@link #updateLayoutPrototype(long,
240             *             Map, String, boolean, ServiceContext)}
241             */
242            @Override
243            public LayoutPrototype updateLayoutPrototype(
244                            long layoutPrototypeId, Map<Locale, String> nameMap,
245                            String description, boolean active)
246                    throws PortalException, SystemException {
247    
248                    return updateLayoutPrototype(
249                            layoutPrototypeId, nameMap, description, active, null);
250            }
251    
252            @Override
253            public LayoutPrototype updateLayoutPrototype(
254                            long layoutPrototypeId, Map<Locale, String> nameMap,
255                            String description, boolean active, ServiceContext serviceContext)
256                    throws PortalException, SystemException {
257    
258                    // Layout prototype
259    
260                    LayoutPrototype layoutPrototype =
261                            layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);
262    
263                    layoutPrototype.setModifiedDate(
264                            serviceContext.getModifiedDate(new Date()));
265                    layoutPrototype.setNameMap(nameMap);
266                    layoutPrototype.setDescription(description);
267                    layoutPrototype.setActive(active);
268    
269                    layoutPrototypePersistence.update(layoutPrototype);
270    
271                    // Layout
272    
273                    Layout layout = layoutPrototype.getLayout();
274    
275                    layout.setModifiedDate(layoutPrototype.getModifiedDate());
276                    layout.setNameMap(nameMap);
277    
278                    layoutPersistence.update(layout);
279    
280                    return layoutPrototype;
281            }
282    
283    }