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.dynamicdatalists.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.systemevent.SystemEvent;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.ResourceConstants;
024    import com.liferay.portal.model.SystemEventConstants;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.dynamicdatalists.RecordSetDDMStructureIdException;
029    import com.liferay.portlet.dynamicdatalists.RecordSetDuplicateRecordSetKeyException;
030    import com.liferay.portlet.dynamicdatalists.RecordSetNameException;
031    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
032    import com.liferay.portlet.dynamicdatalists.service.base.DDLRecordSetLocalServiceBaseImpl;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034    
035    import java.util.Date;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * Provides the local service for accessing, adding, deleting, and updating
042     * dynamic data list (DDL) record sets.
043     *
044     * @author Brian Wing Shun Chan
045     * @author Marcellus Tavares
046     */
047    public class DDLRecordSetLocalServiceImpl
048            extends DDLRecordSetLocalServiceBaseImpl {
049    
050            @Override
051            public DDLRecordSet addRecordSet(
052                            long userId, long groupId, long ddmStructureId, String recordSetKey,
053                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
054                            int minDisplayRows, int scope, ServiceContext serviceContext)
055                    throws PortalException, SystemException {
056    
057                    // Record set
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060    
061                    if (Validator.isNull(recordSetKey)) {
062                            recordSetKey = String.valueOf(counterLocalService.increment());
063                    }
064    
065                    Date now = new Date();
066    
067                    validate(groupId, ddmStructureId, recordSetKey, nameMap);
068    
069                    long recordSetId = counterLocalService.increment();
070    
071                    DDLRecordSet recordSet = ddlRecordSetPersistence.create(recordSetId);
072    
073                    recordSet.setUuid(serviceContext.getUuid());
074                    recordSet.setGroupId(groupId);
075                    recordSet.setCompanyId(user.getCompanyId());
076                    recordSet.setUserId(user.getUserId());
077                    recordSet.setUserName(user.getFullName());
078                    recordSet.setCreateDate(serviceContext.getCreateDate(now));
079                    recordSet.setModifiedDate(serviceContext.getModifiedDate(now));
080                    recordSet.setDDMStructureId(ddmStructureId);
081                    recordSet.setRecordSetKey(recordSetKey);
082                    recordSet.setNameMap(nameMap);
083                    recordSet.setDescriptionMap(descriptionMap);
084                    recordSet.setMinDisplayRows(minDisplayRows);
085                    recordSet.setScope(scope);
086    
087                    ddlRecordSetPersistence.update(recordSet);
088    
089                    // Resources
090    
091                    if (serviceContext.isAddGroupPermissions() ||
092                            serviceContext.isAddGuestPermissions()) {
093    
094                            addRecordSetResources(
095                                    recordSet, serviceContext.isAddGroupPermissions(),
096                                    serviceContext.isAddGuestPermissions());
097                    }
098                    else {
099                            addRecordSetResources(
100                                    recordSet, serviceContext.getGroupPermissions(),
101                                    serviceContext.getGuestPermissions());
102                    }
103    
104                    // Dynamic data mapping structure link
105    
106                    long classNameId = PortalUtil.getClassNameId(DDLRecordSet.class);
107    
108                    ddmStructureLinkLocalService.addStructureLink(
109                            classNameId, recordSetId, ddmStructureId, serviceContext);
110    
111                    return recordSet;
112            }
113    
114            @Override
115            public void addRecordSetResources(
116                            DDLRecordSet recordSet, boolean addGroupPermissions,
117                            boolean addGuestPermissions)
118                    throws PortalException, SystemException {
119    
120                    resourceLocalService.addResources(
121                            recordSet.getCompanyId(), recordSet.getGroupId(),
122                            recordSet.getUserId(), DDLRecordSet.class.getName(),
123                            recordSet.getRecordSetId(), false, addGroupPermissions,
124                            addGuestPermissions);
125            }
126    
127            @Override
128            public void addRecordSetResources(
129                            DDLRecordSet recordSet, String[] groupPermissions,
130                            String[] guestPermissions)
131                    throws PortalException, SystemException {
132    
133                    resourceLocalService.addModelResources(
134                            recordSet.getCompanyId(), recordSet.getGroupId(),
135                            recordSet.getUserId(), DDLRecordSet.class.getName(),
136                            recordSet.getRecordSetId(), groupPermissions, guestPermissions);
137            }
138    
139            @Override
140            @SystemEvent(
141                    action = SystemEventConstants.ACTION_SKIP,
142                    type = SystemEventConstants.TYPE_DELETE)
143            public void deleteRecordSet(DDLRecordSet recordSet)
144                    throws PortalException, SystemException {
145    
146                    // Record set
147    
148                    ddlRecordSetPersistence.remove(recordSet);
149    
150                    // Resources
151    
152                    resourceLocalService.deleteResource(
153                            recordSet.getCompanyId(), DDLRecordSet.class.getName(),
154                            ResourceConstants.SCOPE_INDIVIDUAL, recordSet.getRecordSetId());
155    
156                    // Records
157    
158                    ddlRecordLocalService.deleteRecords(recordSet.getRecordSetId());
159    
160                    // Dynamic data mapping structure link
161    
162                    ddmStructureLinkLocalService.deleteClassStructureLink(
163                            recordSet.getRecordSetId());
164    
165                    // Workflow
166    
167                    workflowDefinitionLinkLocalService.deleteWorkflowDefinitionLink(
168                            recordSet.getCompanyId(), recordSet.getGroupId(),
169                            DDLRecordSet.class.getName(), recordSet.getRecordSetId(), 0);
170            }
171    
172            @Override
173            public void deleteRecordSet(long recordSetId)
174                    throws PortalException, SystemException {
175    
176                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
177                            recordSetId);
178    
179                    deleteRecordSet(recordSet);
180            }
181    
182            @Override
183            public void deleteRecordSet(long groupId, String recordSetKey)
184                    throws PortalException, SystemException {
185    
186                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByG_R(
187                            groupId, recordSetKey);
188    
189                    deleteRecordSet(recordSet);
190            }
191    
192            @Override
193            public void deleteRecordSets(long groupId)
194                    throws PortalException, SystemException {
195    
196                    List<DDLRecordSet> recordSets = ddlRecordSetPersistence.findByGroupId(
197                            groupId);
198    
199                    for (DDLRecordSet recordSet : recordSets) {
200                            deleteRecordSet(recordSet);
201                    }
202            }
203    
204            @Override
205            public DDLRecordSet fetchRecordSet(long recordSetId)
206                    throws SystemException {
207    
208                    return ddlRecordSetPersistence.fetchByPrimaryKey(recordSetId);
209            }
210    
211            @Override
212            public DDLRecordSet fetchRecordSet(long groupId, String recordSetKey)
213                    throws SystemException {
214    
215                    return ddlRecordSetPersistence.fetchByG_R(groupId, recordSetKey);
216            }
217    
218            @Override
219            public DDLRecordSet getRecordSet(long recordSetId)
220                    throws PortalException, SystemException {
221    
222                    return ddlRecordSetPersistence.findByPrimaryKey(recordSetId);
223            }
224    
225            @Override
226            public DDLRecordSet getRecordSet(long groupId, String recordSetKey)
227                    throws PortalException, SystemException {
228    
229                    return ddlRecordSetPersistence.findByG_R(groupId, recordSetKey);
230            }
231    
232            @Override
233            public List<DDLRecordSet> getRecordSets(long groupId)
234                    throws SystemException {
235    
236                    return ddlRecordSetPersistence.findByGroupId(groupId);
237            }
238    
239            @Override
240            public int getRecordSetsCount(long groupId) throws SystemException {
241                    return ddlRecordSetPersistence.countByGroupId(groupId);
242            }
243    
244            @Override
245            public List<DDLRecordSet> search(
246                            long companyId, long groupId, String keywords, int scope, int start,
247                            int end, OrderByComparator orderByComparator)
248                    throws SystemException {
249    
250                    return ddlRecordSetFinder.findByKeywords(
251                            companyId, groupId, keywords, scope, start, end, orderByComparator);
252            }
253    
254            @Override
255            public List<DDLRecordSet> search(
256                            long companyId, long groupId, String name, String description,
257                            int scope, boolean andOperator, int start, int end,
258                            OrderByComparator orderByComparator)
259                    throws SystemException {
260    
261                    return ddlRecordSetFinder.findByC_G_N_D_S(
262                            companyId, groupId, name, description, scope, andOperator, start,
263                            end, orderByComparator);
264            }
265    
266            @Override
267            public int searchCount(
268                            long companyId, long groupId, String keywords, int scope)
269                    throws SystemException {
270    
271                    return ddlRecordSetFinder.countByKeywords(
272                            companyId, groupId, keywords, scope);
273            }
274    
275            @Override
276            public int searchCount(
277                            long companyId, long groupId, String name, String description,
278                            int scope, boolean andOperator)
279                    throws SystemException {
280    
281                    return ddlRecordSetFinder.countByC_G_N_D_S(
282                            companyId, groupId, name, description, scope, andOperator);
283            }
284    
285            @Override
286            public DDLRecordSet updateMinDisplayRows(
287                            long recordSetId, int minDisplayRows, ServiceContext serviceContext)
288                    throws PortalException, SystemException {
289    
290                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
291                            recordSetId);
292    
293                    recordSet.setModifiedDate(serviceContext.getModifiedDate(null));
294                    recordSet.setMinDisplayRows(minDisplayRows);
295    
296                    ddlRecordSetPersistence.update(recordSet);
297    
298                    return recordSet;
299            }
300    
301            @Override
302            public DDLRecordSet updateRecordSet(
303                            long recordSetId, long ddmStructureId, Map<Locale, String> nameMap,
304                            Map<Locale, String> descriptionMap, int minDisplayRows,
305                            ServiceContext serviceContext)
306                    throws PortalException, SystemException {
307    
308                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
309                            recordSetId);
310    
311                    return doUpdateRecordSet(
312                            ddmStructureId, nameMap, descriptionMap, minDisplayRows,
313                            serviceContext, recordSet);
314            }
315    
316            @Override
317            public DDLRecordSet updateRecordSet(
318                            long groupId, long ddmStructureId, String recordSetKey,
319                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
320                            int minDisplayRows, ServiceContext serviceContext)
321                    throws PortalException, SystemException {
322    
323                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByG_R(
324                            groupId, recordSetKey);
325    
326                    return doUpdateRecordSet(
327                            ddmStructureId, nameMap, descriptionMap, minDisplayRows,
328                            serviceContext, recordSet);
329            }
330    
331            protected DDLRecordSet doUpdateRecordSet(
332                            long ddmStructureId, Map<Locale, String> nameMap,
333                            Map<Locale, String> descriptionMap, int minDisplayRows,
334                            ServiceContext serviceContext, DDLRecordSet recordSet)
335                    throws PortalException, SystemException {
336    
337                    validateDDMStructureId(ddmStructureId);
338                    validateName(nameMap);
339    
340                    recordSet.setModifiedDate(serviceContext.getModifiedDate(null));
341                    recordSet.setDDMStructureId(ddmStructureId);
342                    recordSet.setNameMap(nameMap);
343                    recordSet.setDescriptionMap(descriptionMap);
344                    recordSet.setMinDisplayRows(minDisplayRows);
345    
346                    ddlRecordSetPersistence.update(recordSet);
347    
348                    return recordSet;
349            }
350    
351            protected void validate(
352                            long groupId, long ddmStructureId, String recordSetKey,
353                            Map<Locale, String> nameMap)
354                    throws PortalException, SystemException {
355    
356                    validateDDMStructureId(ddmStructureId);
357    
358                    if (Validator.isNotNull(recordSetKey)) {
359                            DDLRecordSet recordSet = ddlRecordSetPersistence.fetchByG_R(
360                                    groupId, recordSetKey);
361    
362                            if (recordSet != null) {
363                                    RecordSetDuplicateRecordSetKeyException rsdrske =
364                                            new RecordSetDuplicateRecordSetKeyException();
365    
366                                    rsdrske.setRecordSetKey(recordSet.getRecordSetKey());
367    
368                                    throw rsdrske;
369                            }
370                    }
371    
372                    validateName(nameMap);
373            }
374    
375            protected void validateDDMStructureId(long ddmStructureId)
376                    throws PortalException, SystemException {
377    
378                    DDMStructure ddmStructure = ddmStructurePersistence.fetchByPrimaryKey(
379                            ddmStructureId);
380    
381                    if (ddmStructure == null) {
382                            throw new RecordSetDDMStructureIdException();
383                    }
384            }
385    
386            protected void validateName(Map<Locale, String> nameMap)
387                    throws PortalException {
388    
389                    String name = nameMap.get(LocaleUtil.getSiteDefault());
390    
391                    if (Validator.isNull(name)) {
392                            throw new RecordSetNameException();
393                    }
394            }
395    
396    }