001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.LayoutBranchNameException;
018    import com.liferay.portal.NoSuchLayoutBranchException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.LayoutBranch;
024    import com.liferay.portal.model.LayoutRevision;
025    import com.liferay.portal.model.LayoutRevisionConstants;
026    import com.liferay.portal.model.LayoutSetBranch;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.base.LayoutBranchLocalServiceBaseImpl;
030    
031    import java.util.List;
032    
033    /**
034     * @author Julio Camarero
035     */
036    public class LayoutBranchLocalServiceImpl
037            extends LayoutBranchLocalServiceBaseImpl {
038    
039            public LayoutBranch addLayoutBranch(
040                            long layoutSetBranchId, long plid, String name, String description,
041                            boolean master, ServiceContext serviceContext)
042                    throws PortalException, SystemException {
043    
044                    User user = userPersistence.findByPrimaryKey(
045                            serviceContext.getUserId());
046                    LayoutSetBranch layoutSetBranch =
047                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
048    
049                    long layoutBranchId = counterLocalService.increment();
050    
051                    LayoutBranch layoutBranch = layoutBranchPersistence.create(
052                            layoutBranchId);
053    
054                    layoutBranch.setGroupId(layoutSetBranch.getGroupId());
055                    layoutBranch.setCompanyId(user.getCompanyId());
056                    layoutBranch.setUserId(user.getUserId());
057                    layoutBranch.setUserName(user.getFullName());
058                    layoutBranch.setLayoutSetBranchId(layoutSetBranchId);
059                    layoutBranch.setPlid(plid);
060                    layoutBranch.setName(name);
061                    layoutBranch.setDescription(description);
062                    layoutBranch.setMaster(master);
063    
064                    layoutBranchPersistence.update(layoutBranch, false);
065    
066                    return layoutBranch;
067            }
068    
069            public LayoutBranch addLayoutBranch(
070                            long layoutRevisionId, String name, String description,
071                            boolean master, ServiceContext serviceContext)
072                    throws PortalException, SystemException {
073    
074                    LayoutRevision layoutRevision =
075                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
076    
077                    validate(
078                            0, layoutRevision.getLayoutSetBranchId(), layoutRevision.getPlid(),
079                            name);
080    
081                    LayoutBranch layoutBranch = addLayoutBranch(
082                            layoutRevision.getLayoutSetBranchId(), layoutRevision.getPlid(),
083                            name, description, master, serviceContext);
084    
085                    layoutRevisionService.addLayoutRevision(
086                            layoutBranch.getUserId(), layoutRevision.getLayoutSetBranchId(),
087                            layoutBranch.getLayoutBranchId(),
088                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID, false,
089                            layoutRevision.getPlid(), layoutRevision.getLayoutRevisionId(),
090                            layoutRevision.isPrivateLayout(), layoutRevision.getName(),
091                            layoutRevision.getTitle(), layoutRevision.getDescription(),
092                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
093                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
094                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
095                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
096                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
097                            serviceContext);
098    
099                    return layoutBranch;
100            }
101    
102            @Override
103            public void deleteLayoutBranch(long layoutBranchId)
104                    throws PortalException, SystemException {
105    
106                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
107                            layoutBranchId);
108    
109                    layoutRevisionLocalService.deleteLayoutRevisions(
110                            layoutBranch.getLayoutSetBranchId(), layoutBranchId,
111                            layoutBranch.getPlid());
112    
113                    layoutBranchLocalService.deleteLayoutBranch(layoutBranch);
114            }
115    
116            public void deleteLayoutSetBranchLayoutBranches(long layoutSetBranchId)
117                    throws PortalException, SystemException {
118    
119                    List<LayoutBranch> layoutBranches =
120                            layoutBranchPersistence.findByLayoutSetBranchId(layoutSetBranchId);
121    
122                    for (LayoutBranch layoutBranch : layoutBranches) {
123                            deleteLayoutBranch(layoutBranch.getLayoutBranchId());
124                    }
125            }
126    
127            @Override
128            public LayoutBranch getLayoutBranch(long layoutBranchId)
129                    throws PortalException, SystemException {
130    
131                    return layoutBranchPersistence.findByPrimaryKey(layoutBranchId);
132            }
133    
134            public List<LayoutBranch> getLayoutBranches(
135                            long layoutSetBranchId, long plid, int start, int end,
136                            OrderByComparator orderByComparator)
137                    throws SystemException {
138    
139                    return layoutBranchPersistence.findByL_P(
140                            layoutSetBranchId, plid, start, end, orderByComparator);
141            }
142    
143            public List<LayoutBranch> getLayoutSetBranchLayoutBranches(
144                            long layoutSetBranchId)
145                    throws SystemException {
146    
147                    return layoutBranchPersistence.findByLayoutSetBranchId(
148                            layoutSetBranchId);
149            }
150    
151            public LayoutBranch getMasterLayoutBranch(long layoutSetBranchId, long plid)
152                    throws PortalException, SystemException {
153    
154                    return layoutBranchPersistence.findByL_P_M(
155                            layoutSetBranchId, plid, true);
156            }
157    
158            public LayoutBranch updateLayoutBranch(
159                            long layoutBranchId, String name, String description,
160                            ServiceContext serviceContext)
161                    throws PortalException, SystemException {
162    
163                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
164                            layoutBranchId);
165    
166                    validate(
167                            layoutBranch.getLayoutBranchId(),
168                            layoutBranch.getLayoutSetBranchId(), layoutBranch.getPlid(), name);
169    
170                    layoutBranch.setName(name);
171                    layoutBranch.setDescription(description);
172    
173                    layoutBranchPersistence.update(layoutBranch, false);
174    
175                    return layoutBranch;
176            }
177    
178            protected void validate(
179                            long layoutBranchId, long layoutSetBranchId, long plid, String name)
180                    throws PortalException, SystemException {
181    
182                    if (Validator.isNull(name) || (name.length() < 4)) {
183                            throw new LayoutBranchNameException(
184                                    LayoutBranchNameException.TOO_SHORT);
185                    }
186    
187                    if (name.length() > 100) {
188                            throw new LayoutBranchNameException(
189                                    LayoutBranchNameException.TOO_LONG);
190                    }
191    
192                    try {
193                            LayoutBranch layoutBranch = layoutBranchPersistence.findByL_P_N(
194                                    layoutSetBranchId, plid, name);
195    
196                            if (layoutBranch.getLayoutBranchId() != layoutBranchId) {
197                                    throw new LayoutBranchNameException(
198                                            LayoutBranchNameException.DUPLICATE);
199                            }
200                    }
201                    catch (NoSuchLayoutBranchException nsbe) {
202                    }
203            }
204    
205    }