001    /**
002     * Copyright (c) 2000-2013 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.LayoutBranchConstants;
025    import com.liferay.portal.model.LayoutRevision;
026    import com.liferay.portal.model.LayoutRevisionConstants;
027    import com.liferay.portal.model.LayoutSetBranch;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.base.LayoutBranchLocalServiceBaseImpl;
031    
032    import java.util.List;
033    
034    /**
035     * @author Julio Camarero
036     */
037    public class LayoutBranchLocalServiceImpl
038            extends LayoutBranchLocalServiceBaseImpl {
039    
040            @Override
041            public LayoutBranch addLayoutBranch(
042                            long layoutSetBranchId, long plid, String name, String description,
043                            boolean master, ServiceContext serviceContext)
044                    throws PortalException, SystemException {
045    
046                    User user = userPersistence.findByPrimaryKey(
047                            serviceContext.getUserId());
048                    LayoutSetBranch layoutSetBranch =
049                            layoutSetBranchPersistence.findByPrimaryKey(layoutSetBranchId);
050    
051                    validate(0, layoutSetBranchId, plid, name);
052    
053                    long layoutBranchId = counterLocalService.increment();
054    
055                    LayoutBranch layoutBranch = layoutBranchPersistence.create(
056                            layoutBranchId);
057    
058                    layoutBranch.setGroupId(layoutSetBranch.getGroupId());
059                    layoutBranch.setCompanyId(user.getCompanyId());
060                    layoutBranch.setUserId(user.getUserId());
061                    layoutBranch.setUserName(user.getFullName());
062                    layoutBranch.setLayoutSetBranchId(layoutSetBranchId);
063                    layoutBranch.setPlid(plid);
064                    layoutBranch.setName(name);
065                    layoutBranch.setDescription(description);
066                    layoutBranch.setMaster(master);
067    
068                    layoutBranchPersistence.update(layoutBranch, false);
069    
070                    return layoutBranch;
071            }
072    
073            @Override
074            public LayoutBranch addLayoutBranch(
075                            long layoutRevisionId, String name, String description,
076                            boolean master, ServiceContext serviceContext)
077                    throws PortalException, SystemException {
078    
079                    LayoutRevision layoutRevision =
080                            layoutRevisionPersistence.findByPrimaryKey(layoutRevisionId);
081    
082                    LayoutBranch layoutBranch = addLayoutBranch(
083                            layoutRevision.getLayoutSetBranchId(), layoutRevision.getPlid(),
084                            name, description, master, serviceContext);
085    
086                    layoutRevisionService.addLayoutRevision(
087                            layoutBranch.getUserId(), layoutRevision.getLayoutSetBranchId(),
088                            layoutBranch.getLayoutBranchId(),
089                            LayoutRevisionConstants.DEFAULT_PARENT_LAYOUT_REVISION_ID, false,
090                            layoutRevision.getPlid(), layoutRevision.getLayoutRevisionId(),
091                            layoutRevision.isPrivateLayout(), layoutRevision.getName(),
092                            layoutRevision.getTitle(), layoutRevision.getDescription(),
093                            layoutRevision.getKeywords(), layoutRevision.getRobots(),
094                            layoutRevision.getTypeSettings(), layoutRevision.getIconImage(),
095                            layoutRevision.getIconImageId(), layoutRevision.getThemeId(),
096                            layoutRevision.getColorSchemeId(), layoutRevision.getWapThemeId(),
097                            layoutRevision.getWapColorSchemeId(), layoutRevision.getCss(),
098                            serviceContext);
099    
100                    return layoutBranch;
101            }
102    
103            @Override
104            public LayoutBranch deleteLayoutBranch(long layoutBranchId)
105                    throws PortalException, SystemException {
106    
107                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
108                            layoutBranchId);
109    
110                    layoutRevisionLocalService.deleteLayoutRevisions(
111                            layoutBranch.getLayoutSetBranchId(), layoutBranchId,
112                            layoutBranch.getPlid());
113    
114                    return layoutBranchLocalService.deleteLayoutBranch(layoutBranch);
115            }
116    
117            @Override
118            public void deleteLayoutSetBranchLayoutBranches(long layoutSetBranchId)
119                    throws PortalException, SystemException {
120    
121                    List<LayoutBranch> layoutBranches =
122                            layoutBranchPersistence.findByLayoutSetBranchId(layoutSetBranchId);
123    
124                    for (LayoutBranch layoutBranch : layoutBranches) {
125                            deleteLayoutBranch(layoutBranch.getLayoutBranchId());
126                    }
127            }
128    
129            @Override
130            public List<LayoutBranch> getLayoutBranches(
131                            long layoutSetBranchId, long plid, int start, int end,
132                            OrderByComparator orderByComparator)
133                    throws SystemException {
134    
135                    return layoutBranchPersistence.findByL_P(
136                            layoutSetBranchId, plid, start, end, orderByComparator);
137            }
138    
139            @Override
140            public List<LayoutBranch> getLayoutSetBranchLayoutBranches(
141                            long layoutSetBranchId)
142                    throws SystemException {
143    
144                    return layoutBranchPersistence.findByLayoutSetBranchId(
145                            layoutSetBranchId);
146            }
147    
148            @Override
149            public LayoutBranch getMasterLayoutBranch(long layoutSetBranchId, long plid)
150                    throws PortalException, SystemException {
151    
152                    return layoutBranchPersistence.findByL_P_M_First(
153                            layoutSetBranchId, plid, true, null);
154            }
155    
156            @Override
157            public LayoutBranch getMasterLayoutBranch(
158                            long layoutSetBranchId, long plid, ServiceContext serviceContext)
159                    throws PortalException, SystemException {
160    
161                    LayoutBranch layoutBranch = layoutBranchPersistence.fetchByL_P_M_First(
162                            layoutSetBranchId, plid, true, null);
163    
164                    if (layoutBranch != null) {
165                            return layoutBranch;
166                    }
167    
168                    return layoutBranchLocalService.addLayoutBranch(
169                            layoutSetBranchId, plid, LayoutBranchConstants.MASTER_BRANCH_NAME,
170                            LayoutBranchConstants.MASTER_BRANCH_DESCRIPTION, true,
171                            serviceContext);
172            }
173    
174            @Override
175            public LayoutBranch updateLayoutBranch(
176                            long layoutBranchId, String name, String description,
177                            ServiceContext serviceContext)
178                    throws PortalException, SystemException {
179    
180                    LayoutBranch layoutBranch = layoutBranchPersistence.findByPrimaryKey(
181                            layoutBranchId);
182    
183                    validate(
184                            layoutBranch.getLayoutBranchId(),
185                            layoutBranch.getLayoutSetBranchId(), layoutBranch.getPlid(), name);
186    
187                    layoutBranch.setName(name);
188                    layoutBranch.setDescription(description);
189    
190                    layoutBranchPersistence.update(layoutBranch, false);
191    
192                    return layoutBranch;
193            }
194    
195            protected void validate(
196                            long layoutBranchId, long layoutSetBranchId, long plid, String name)
197                    throws PortalException, SystemException {
198    
199                    if (Validator.isNull(name) || (name.length() < 4)) {
200                            throw new LayoutBranchNameException(
201                                    LayoutBranchNameException.TOO_SHORT);
202                    }
203    
204                    if (name.length() > 100) {
205                            throw new LayoutBranchNameException(
206                                    LayoutBranchNameException.TOO_LONG);
207                    }
208    
209                    try {
210                            LayoutBranch layoutBranch = layoutBranchPersistence.findByL_P_N(
211                                    layoutSetBranchId, plid, name);
212    
213                            if (layoutBranch.getLayoutBranchId() != layoutBranchId) {
214                                    throw new LayoutBranchNameException(
215                                            LayoutBranchNameException.DUPLICATE);
216                            }
217                    }
218                    catch (NoSuchLayoutBranchException nslbe) {
219                    }
220            }
221    
222    }