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.NoSuchLayoutFriendlyURLException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.LayoutFriendlyURL;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.base.LayoutFriendlyURLLocalServiceBaseImpl;
027    
028    import java.util.ArrayList;
029    import java.util.Date;
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    /**
035     * Provides the local service for accessing, adding, deleting, and updating
036     * friendly URLs for layouts.
037     *
038     * <p>
039     * All custom service methods should be put in this class. Whenever methods are
040     * added, rerun ServiceBuilder to copy their definitions into the {@link
041     * com.liferay.portal.service.LayoutFriendlyURLLocalService} interface.
042     * </p>
043     *
044     * <p>
045     * Methods of this service will not have security checks based on the propagated
046     * JAAS credentials because this service can only be accessed from within the
047     * same VM.
048     * </p>
049     *
050     * @author Brian Wing Shun Chan
051     */
052    public class LayoutFriendlyURLLocalServiceImpl
053            extends LayoutFriendlyURLLocalServiceBaseImpl {
054    
055            @Override
056            public LayoutFriendlyURL addLayoutFriendlyURL(
057                            long userId, long companyId, long groupId, long plid,
058                            boolean privateLayout, String friendlyURL, String languageId,
059                            ServiceContext serviceContext)
060                    throws PortalException, SystemException {
061    
062                    User user = userPersistence.findByPrimaryKey(userId);
063                    Date now = new Date();
064    
065                    long layoutFriendlyURLId = counterLocalService.increment();
066    
067                    LayoutFriendlyURL layoutFriendlyURL =
068                            layoutFriendlyURLPersistence.create(layoutFriendlyURLId);
069    
070                    layoutFriendlyURL.setUuid(serviceContext.getUuid());
071                    layoutFriendlyURL.setGroupId(groupId);
072                    layoutFriendlyURL.setCompanyId(companyId);
073                    layoutFriendlyURL.setUserId(user.getUserId());
074                    layoutFriendlyURL.setUserName(user.getFullName());
075                    layoutFriendlyURL.setCreateDate(serviceContext.getCreateDate(now));
076                    layoutFriendlyURL.setModifiedDate(serviceContext.getModifiedDate(now));
077                    layoutFriendlyURL.setPlid(plid);
078                    layoutFriendlyURL.setPrivateLayout(privateLayout);
079                    layoutFriendlyURL.setFriendlyURL(friendlyURL);
080                    layoutFriendlyURL.setLanguageId(languageId);
081    
082                    return layoutFriendlyURLPersistence.update(layoutFriendlyURL);
083            }
084    
085            @Override
086            public List<LayoutFriendlyURL> addLayoutFriendlyURLs(
087                            long userId, long companyId, long groupId, long plid,
088                            boolean privateLayout, Map<Locale, String> friendlyURLMap,
089                            ServiceContext serviceContext)
090                    throws PortalException, SystemException {
091    
092                    List<LayoutFriendlyURL> layoutFriendlyURLs =
093                            new ArrayList<LayoutFriendlyURL>();
094    
095                    Locale[] locales = LanguageUtil.getAvailableLocales(groupId);
096    
097                    for (Locale locale : locales) {
098                            String friendlyURL = friendlyURLMap.get(locale);
099    
100                            if (Validator.isNull(friendlyURL)) {
101                                    continue;
102                            }
103    
104                            LayoutFriendlyURL layoutFriendlyURL = addLayoutFriendlyURL(
105                                    userId, companyId, groupId, plid, privateLayout, friendlyURL,
106                                    LocaleUtil.toLanguageId(locale), serviceContext);
107    
108                            layoutFriendlyURLs.add(layoutFriendlyURL);
109                    }
110    
111                    return layoutFriendlyURLs;
112            }
113    
114            @Override
115            public LayoutFriendlyURL deleteLayoutFriendlyURL(
116                            LayoutFriendlyURL layoutFriendlyURL)
117                    throws SystemException {
118    
119                    return layoutFriendlyURLPersistence.remove(layoutFriendlyURL);
120            }
121    
122            @Override
123            public void deleteLayoutFriendlyURL(long plid, String languageId)
124                    throws SystemException {
125    
126                    LayoutFriendlyURL layoutFriendlyURL =
127                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
128    
129                    if (layoutFriendlyURL != null) {
130                            deleteLayoutFriendlyURL(layoutFriendlyURL);
131                    }
132            }
133    
134            @Override
135            public void deleteLayoutFriendlyURLs(long plid) throws SystemException {
136                    List<LayoutFriendlyURL> layoutFriendlyURLs =
137                            layoutFriendlyURLPersistence.findByPlid(plid);
138    
139                    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
140                            deleteLayoutFriendlyURL(layoutFriendlyURL);
141                    }
142            }
143    
144            @Override
145            public LayoutFriendlyURL fetchFirstLayoutFriendlyURL(
146                            long groupId, boolean privateLayout, String friendlyURL)
147                    throws SystemException {
148    
149                    return layoutFriendlyURLPersistence.fetchByG_P_F_First(
150                            groupId, privateLayout, friendlyURL, null);
151            }
152    
153            @Override
154            public LayoutFriendlyURL fetchLayoutFriendlyURL(
155                            long groupId, boolean privateLayout, String friendlyURL,
156                            String languageId)
157                    throws SystemException {
158    
159                    return layoutFriendlyURLPersistence.fetchByG_P_F_L(
160                            groupId, privateLayout, friendlyURL, languageId);
161            }
162    
163            @Override
164            public LayoutFriendlyURL fetchLayoutFriendlyURL(
165                            long plid, String languageId)
166                    throws SystemException {
167    
168                    return fetchLayoutFriendlyURL(plid, languageId, true);
169            }
170    
171            @Override
172            public LayoutFriendlyURL fetchLayoutFriendlyURL(
173                            long plid, String languageId, boolean useDefault)
174                    throws SystemException {
175    
176                    LayoutFriendlyURL layoutFriendlyURL =
177                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
178    
179                    if ((layoutFriendlyURL == null) && !useDefault) {
180                            return null;
181                    }
182    
183                    if (layoutFriendlyURL == null) {
184                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByP_L(
185                                    plid, LocaleUtil.toLanguageId(LocaleUtil.getSiteDefault()));
186                    }
187    
188                    if (layoutFriendlyURL == null) {
189                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByPlid_First(
190                                    plid, null);
191                    }
192    
193                    return layoutFriendlyURL;
194            }
195    
196            @Override
197            public LayoutFriendlyURL getLayoutFriendlyURL(long plid, String languageId)
198                    throws PortalException, SystemException {
199    
200                    return getLayoutFriendlyURL(plid, languageId, true);
201            }
202    
203            @Override
204            public LayoutFriendlyURL getLayoutFriendlyURL(
205                            long plid, String languageId, boolean useDefault)
206                    throws PortalException, SystemException {
207    
208                    LayoutFriendlyURL layoutFriendlyURL =
209                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
210    
211                    if ((layoutFriendlyURL == null) && !useDefault) {
212                            throw new NoSuchLayoutFriendlyURLException();
213                    }
214    
215                    if (layoutFriendlyURL == null) {
216                            layoutFriendlyURL = layoutFriendlyURLPersistence.fetchByP_L(
217                                    plid, LocaleUtil.toLanguageId(LocaleUtil.getSiteDefault()));
218                    }
219    
220                    if (layoutFriendlyURL == null) {
221                            layoutFriendlyURL = layoutFriendlyURLPersistence.findByPlid_First(
222                                    plid, null);
223                    }
224    
225                    return layoutFriendlyURL;
226            }
227    
228            @Override
229            public List<LayoutFriendlyURL> getLayoutFriendlyURLs(long plid)
230                    throws SystemException {
231    
232                    return layoutFriendlyURLPersistence.findByPlid(plid);
233            }
234    
235            @Override
236            public List<LayoutFriendlyURL> getLayoutFriendlyURLs(
237                            long plid, String friendlyURL, int start, int end)
238                    throws SystemException {
239    
240                    return layoutFriendlyURLPersistence.findByP_F(
241                            plid, friendlyURL, start, end);
242            }
243    
244            @Override
245            public LayoutFriendlyURL updateLayoutFriendlyURL(
246                            long userId, long companyId, long groupId, long plid,
247                            boolean privateLayout, String friendlyURL, String languageId,
248                            ServiceContext serviceContext)
249                    throws PortalException, SystemException {
250    
251                    LayoutFriendlyURL layoutFriendlyURL =
252                            layoutFriendlyURLPersistence.fetchByP_L(plid, languageId);
253    
254                    if (layoutFriendlyURL == null) {
255                            return addLayoutFriendlyURL(
256                                    userId, companyId, groupId, plid, privateLayout, friendlyURL,
257                                    languageId, serviceContext);
258                    }
259    
260                    layoutFriendlyURL.setFriendlyURL(friendlyURL);
261    
262                    return layoutFriendlyURLPersistence.update(layoutFriendlyURL);
263            }
264    
265            @Override
266            public List<LayoutFriendlyURL> updateLayoutFriendlyURLs(
267                            long userId, long companyId, long groupId, long plid,
268                            boolean privateLayout, Map<Locale, String> friendlyURLMap,
269                            ServiceContext serviceContext)
270                    throws PortalException, SystemException {
271    
272                    List<LayoutFriendlyURL> layoutFriendlyURLs =
273                            new ArrayList<LayoutFriendlyURL>();
274    
275                    Locale[] locales = LanguageUtil.getAvailableLocales(groupId);
276    
277                    for (Locale locale : locales) {
278                            String friendlyURL = friendlyURLMap.get(locale);
279                            String languageId = LocaleUtil.toLanguageId(locale);
280    
281                            if (Validator.isNull(friendlyURL)) {
282                                    deleteLayoutFriendlyURL(plid, languageId);
283                            }
284                            else {
285                                    LayoutFriendlyURL layoutFriendlyURL = updateLayoutFriendlyURL(
286                                            userId, companyId, groupId, plid, privateLayout,
287                                            friendlyURL, languageId, serviceContext);
288    
289                                    layoutFriendlyURLs.add(layoutFriendlyURL);
290                            }
291                    }
292    
293                    return layoutFriendlyURLs;
294            }
295    
296    }