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.portlet.dynamicdatamapping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ResourceConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
026    import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
028    import com.liferay.portlet.dynamicdatamapping.service.base.DDMTemplateLocalServiceBaseImpl;
029    
030    import java.util.ArrayList;
031    import java.util.Date;
032    import java.util.List;
033    import java.util.Locale;
034    import java.util.Map;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Eduardo Lundgren
039     */
040    public class DDMTemplateLocalServiceImpl
041            extends DDMTemplateLocalServiceBaseImpl {
042    
043            @Override
044            public DDMTemplate addTemplate(
045                            long userId, long groupId, long structureId,
046                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
047                            String type, String mode, String language, String script,
048                            ServiceContext serviceContext)
049                    throws PortalException, SystemException {
050    
051                    // Template
052    
053                    User user = userPersistence.findByPrimaryKey(userId);
054                    Date now = new Date();
055    
056                    validate(nameMap, script);
057    
058                    long templateId = counterLocalService.increment();
059    
060                    DDMTemplate template = ddmTemplatePersistence.create(templateId);
061    
062                    template.setUuid(serviceContext.getUuid());
063                    template.setGroupId(groupId);
064                    template.setCompanyId(user.getCompanyId());
065                    template.setUserId(user.getUserId());
066                    template.setUserName(user.getFullName());
067                    template.setCreateDate(serviceContext.getCreateDate(now));
068                    template.setModifiedDate(serviceContext.getModifiedDate(now));
069                    template.setStructureId(structureId);
070                    template.setNameMap(nameMap);
071                    template.setDescriptionMap(descriptionMap);
072                    template.setType(type);
073                    template.setMode(mode);
074                    template.setLanguage(language);
075                    template.setScript(script);
076    
077                    ddmTemplatePersistence.update(template, false);
078    
079                    // Resources
080    
081                    if (serviceContext.isAddGroupPermissions() ||
082                            serviceContext.isAddGuestPermissions()) {
083    
084                            addTemplateResources(
085                                    template, serviceContext.isAddGroupPermissions(),
086                                    serviceContext.isAddGuestPermissions());
087                    }
088                    else {
089                            addTemplateResources(
090                                    template, serviceContext.getGroupPermissions(),
091                                    serviceContext.getGuestPermissions());
092                    }
093    
094                    return template;
095            }
096    
097            @Override
098            public void addTemplateResources(
099                            DDMTemplate template, boolean addGroupPermissions,
100                            boolean addGuestPermissions)
101                    throws PortalException, SystemException {
102    
103                    resourceLocalService.addResources(
104                            template.getCompanyId(), template.getGroupId(),
105                            template.getUserId(), DDMTemplate.class.getName(),
106                            template.getTemplateId(), false, addGroupPermissions,
107                            addGuestPermissions);
108            }
109    
110            @Override
111            public void addTemplateResources(
112                            DDMTemplate template, String[] groupPermissions,
113                            String[] guestPermissions)
114                    throws PortalException, SystemException {
115    
116                    resourceLocalService.addModelResources(
117                            template.getCompanyId(), template.getGroupId(),
118                            template.getUserId(), DDMTemplate.class.getName(),
119                            template.getTemplateId(), groupPermissions, guestPermissions);
120            }
121    
122            @Override
123            public List<DDMTemplate> copyTemplates(
124                            long userId, long structureId, long newStructureId, String type,
125                            ServiceContext serviceContext)
126                    throws PortalException, SystemException {
127    
128                    List<DDMTemplate> newTemplates = new ArrayList<DDMTemplate>();
129    
130                    List<DDMTemplate> oldTemplates = getTemplates(structureId, type);
131    
132                    for (DDMTemplate oldTemplate : oldTemplates) {
133                            DDMTemplate newTemplate = addTemplate(
134                                    userId, oldTemplate.getGroupId(), newStructureId,
135                                    oldTemplate.getNameMap(), oldTemplate.getDescriptionMap(),
136                                    oldTemplate.getType(), oldTemplate.getMode(),
137                                    oldTemplate.getLanguage(), oldTemplate.getScript(),
138                                    serviceContext);
139    
140                            newTemplates.add(newTemplate);
141                    }
142    
143                    return newTemplates;
144            }
145    
146            @Override
147            public void deleteTemplate(DDMTemplate template)
148                    throws PortalException, SystemException {
149    
150                    // Template
151    
152                    ddmTemplatePersistence.remove(template);
153    
154                    // Resources
155    
156                    resourceLocalService.deleteResource(
157                            template.getCompanyId(), DDMTemplate.class.getName(),
158                            ResourceConstants.SCOPE_INDIVIDUAL, template.getTemplateId());
159            }
160    
161            @Override
162            public void deleteTemplate(long templateId)
163                    throws PortalException, SystemException {
164    
165                    DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey(
166                            templateId);
167    
168                    deleteTemplate(template);
169            }
170    
171            @Override
172            public void deleteTemplates(long groupId)
173                    throws PortalException, SystemException {
174    
175                    List<DDMTemplate> templates = ddmTemplatePersistence.findByGroupId(
176                            groupId);
177    
178                    for (DDMTemplate template : templates) {
179                            deleteTemplate(template);
180                    }
181            }
182    
183            @Override
184            public DDMTemplate getTemplate(long templateId)
185                    throws PortalException, SystemException {
186    
187                    return ddmTemplatePersistence.findByPrimaryKey(templateId);
188            }
189    
190            @Override
191            public List<DDMTemplate> getTemplates(long structureId)
192                    throws SystemException {
193    
194                    return ddmTemplatePersistence.findByStructureId(structureId);
195            }
196    
197            @Override
198            public List<DDMTemplate> getTemplates(long structureId, String type)
199                    throws SystemException {
200    
201                    return ddmTemplatePersistence.findByS_T(structureId, type);
202            }
203    
204            @Override
205            public List<DDMTemplate> getTemplates(
206                            long structureId, String type, String mode)
207                    throws SystemException {
208    
209                    return ddmTemplatePersistence.findByS_T_M(structureId, type, mode);
210            }
211    
212            @Override
213            public List<DDMTemplate> search(
214                            long companyId, long groupId, long structureId, String keywords,
215                            String type, String mode, int start, int end,
216                            OrderByComparator orderByComparator)
217                    throws SystemException {
218    
219                    return ddmTemplateFinder.findByKeywords(
220                            companyId, groupId, structureId, keywords, type, mode, start, end,
221                            orderByComparator);
222            }
223    
224            @Override
225            public List<DDMTemplate> search(
226                            long companyId, long groupId, long structureId, String name,
227                            String description, String type, String mode, String language,
228                            boolean andOperator, int start, int end,
229                            OrderByComparator orderByComparator)
230                    throws SystemException {
231    
232                    return ddmTemplateFinder.findByC_G_S_N_D_T_M_L(
233                            companyId, groupId, structureId, name, description, type, mode,
234                            language, andOperator, start, end, orderByComparator);
235            }
236    
237            @Override
238            public int searchCount(
239                            long companyId, long groupId, long structureId, String keywords,
240                            String type, String mode)
241                    throws SystemException {
242    
243                    return ddmTemplateFinder.countByKeywords(
244                            companyId, groupId, structureId, keywords, type, mode);
245            }
246    
247            @Override
248            public int searchCount(
249                            long companyId, long groupId, long structureId, String name,
250                            String description, String type, String mode, String language,
251                            boolean andOperator)
252                    throws SystemException {
253    
254                    return ddmTemplateFinder.countByC_G_S_N_D_T_M_L(
255                            companyId, groupId, structureId, name, description, type, mode,
256                            language, andOperator);
257            }
258    
259            @Override
260            public DDMTemplate updateTemplate(
261                            long templateId, Map<Locale, String> nameMap,
262                            Map<Locale, String> descriptionMap, String type, String mode,
263                            String language, String script, ServiceContext serviceContext)
264                    throws PortalException, SystemException {
265    
266                    validate(nameMap, script);
267    
268                    DDMTemplate template = ddmTemplateLocalService.getDDMTemplate(
269                            templateId);
270    
271                    template.setModifiedDate(serviceContext.getModifiedDate(null));
272                    template.setNameMap(nameMap);
273                    template.setDescriptionMap(descriptionMap);
274                    template.setType(type);
275                    template.setMode(mode);
276                    template.setLanguage(language);
277                    template.setScript(script);
278    
279                    ddmTemplatePersistence.update(template, false);
280    
281                    return template;
282            }
283    
284            protected void validate(Map<Locale, String> nameMap, String script)
285                    throws PortalException {
286    
287                    validateName(nameMap);
288    
289                    if (Validator.isNull(script)) {
290                            throw new TemplateScriptException();
291                    }
292            }
293    
294            protected void validateName(Map<Locale, String> nameMap)
295                    throws PortalException {
296    
297                    String name = nameMap.get(LocaleUtil.getDefault());
298    
299                    if (Validator.isNull(name)) {
300                            throw new TemplateNameException();
301                    }
302            }
303    
304    }