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.OrderByComparator;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
023    import com.liferay.portlet.dynamicdatamapping.service.base.DDMStructureServiceBaseImpl;
024    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMPermission;
025    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMStructurePermission;
026    import com.liferay.portlet.dynamicdatamapping.util.DDMDisplay;
027    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
028    
029    import java.util.List;
030    import java.util.Locale;
031    import java.util.Map;
032    
033    /**
034     * Provides the remote service for accessing, adding, deleting, and updating
035     * dynamic data mapping (DDM) structures. Its methods include permission checks.
036     *
037     * @author Brian Wing Shun Chan
038     * @author Bruno Basto
039     * @author Marcellus Tavares
040     * @see    com.liferay.portlet.dynamicdatamapping.service.impl.DDMStructureLocalServiceImpl
041     */
042    public class DDMStructureServiceImpl extends DDMStructureServiceBaseImpl {
043    
044            /**
045             * Adds a structure referencing a default parent structure, using the portal
046             * property <code>dynamic.data.lists.storage.type</code> storage type and
047             * default structure type.
048             *
049             * @param  userId the primary key of the structure's creator/owner
050             * @param  groupId the primary key of the group
051             * @param  classNameId the primary key of the class name for the structure's
052             *         related model
053             * @param  nameMap the structure's locales and localized names
054             * @param  descriptionMap the structure's locales and localized descriptions
055             * @param  xsd the structure's XML schema definition
056             * @param  serviceContext the service context to be applied. Can set the
057             *         UUID, creation date, modification date, guest permissions, and
058             *         group permissions for the structure.
059             * @return the structure
060             * @throws PortalException if a user with the primary key could not be
061             *         found, if the user did not have permission to add the structure,
062             *         if the XSD was not well-formed, or if a portal exception occurred
063             * @throws SystemException if a system exception occurred
064             */
065            @Override
066            public DDMStructure addStructure(
067                            long userId, long groupId, long classNameId,
068                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
069                            String xsd, ServiceContext serviceContext)
070                    throws PortalException, SystemException {
071    
072                    DDMDisplay ddmDisplay = DDMUtil.getDDMDisplay(serviceContext);
073    
074                    DDMPermission.check(
075                            getPermissionChecker(), serviceContext.getScopeGroupId(),
076                            ddmDisplay.getResourceName(), ddmDisplay.getAddStructureActionId());
077    
078                    return ddmStructureLocalService.addStructure(
079                            getUserId(), groupId, classNameId, nameMap, descriptionMap, xsd,
080                            serviceContext);
081            }
082    
083            /**
084             * Adds a structure referencing its parent structure.
085             *
086             * @param  groupId the primary key of the group
087             * @param  parentStructureId the primary key of the parent structure
088             *         (optionally {@link
089             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants#DEFAULT_PARENT_STRUCTURE_ID})
090             * @param  classNameId the primary key of the class name for the structure's
091             *         related model
092             * @param  structureKey the unique string identifying the structure
093             *         (optionally <code>null</code>)
094             * @param  nameMap the structure's locales and localized names
095             * @param  descriptionMap the structure's locales and localized descriptions
096             * @param  xsd the structure's XML schema definition
097             * @param  storageType the structure's storage type. It can be "xml" or
098             *         "expando". For more information, see {@link
099             *         com.liferay.portlet.dynamicdatamapping.storage.StorageType}.
100             * @param  type the structure's type. For more information, see {@link
101             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants}.
102             * @param  serviceContext the service context to be applied. Can set the
103             *         UUID, creation date, modification date, guest permissions, and
104             *         group permissions for the structure.
105             * @return the structure
106             * @throws PortalException if the user did not have permission to add the
107             *         structure, if the XSD is not well formed, or if a portal
108             *         exception occurred
109             * @throws SystemException if a system exception occurred
110             */
111            @Override
112            public DDMStructure addStructure(
113                            long groupId, long parentStructureId, long classNameId,
114                            String structureKey, Map<Locale, String> nameMap,
115                            Map<Locale, String> descriptionMap, String xsd, String storageType,
116                            int type, ServiceContext serviceContext)
117                    throws PortalException, SystemException {
118    
119                    DDMDisplay ddmDisplay = DDMUtil.getDDMDisplay(serviceContext);
120    
121                    DDMPermission.check(
122                            getPermissionChecker(), serviceContext.getScopeGroupId(),
123                            ddmDisplay.getResourceName(), ddmDisplay.getAddStructureActionId());
124    
125                    return ddmStructureLocalService.addStructure(
126                            getUserId(), groupId, parentStructureId, classNameId, structureKey,
127                            nameMap, descriptionMap, xsd, storageType, type, serviceContext);
128            }
129    
130            /**
131             * Adds a structure referencing the parent structure by its structure key.
132             * In case the parent structure is not found, it uses the default parent
133             * structure ID.
134             *
135             * @param  userId the primary key of the structure's creator/owner
136             * @param  groupId the primary key of the group
137             * @param  parentStructureKey the unique string identifying the structure
138             * @param  classNameId the primary key of the class name for the structure's
139             *         related model
140             * @param  structureKey unique string identifying the structure (optionally
141             *         <code>null</code>)
142             * @param  nameMap the structure's locales and localized names
143             * @param  descriptionMap the structure's locales and localized descriptions
144             * @param  xsd the XML schema definition of the structure
145             * @param  storageType the storage type of the structure. It can be XML or
146             *         expando. For more information, see {@link
147             *         com.liferay.portlet.dynamicdatamapping.storage.StorageType}.
148             * @param  type the structure's type. For more information, see {@link
149             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants}.
150             * @param  serviceContext the service context to be applied. Must have the
151             *         <code>ddmResource</code> attribute to check permissions. Can set
152             *         the UUID, creation date, modification date, guest permissions,
153             *         and group permissions for the structure.
154             * @return the structure
155             * @throws PortalException if a user with the primary key could not be
156             *         found, if the user did not have permission to add the structure,
157             *         if the XSD was not well-formed, or if a portal exception occurred
158             * @throws SystemException if a system exception occurred
159             */
160            @Override
161            public DDMStructure addStructure(
162                            long userId, long groupId, String parentStructureKey,
163                            long classNameId, String structureKey, Map<Locale, String> nameMap,
164                            Map<Locale, String> descriptionMap, String xsd, String storageType,
165                            int type, ServiceContext serviceContext)
166                    throws PortalException, SystemException {
167    
168                    DDMDisplay ddmDisplay = DDMUtil.getDDMDisplay(serviceContext);
169    
170                    DDMPermission.check(
171                            getPermissionChecker(), serviceContext.getScopeGroupId(),
172                            ddmDisplay.getResourceName(), ddmDisplay.getAddStructureActionId());
173    
174                    return ddmStructureLocalService.addStructure(
175                            userId, groupId, parentStructureKey, classNameId, structureKey,
176                            nameMap, descriptionMap, xsd, storageType, type, serviceContext);
177            }
178    
179            /**
180             * Copies a structure, creating a new structure with all the values
181             * extracted from the original one. The new structure supports a new name
182             * and description.
183             *
184             * @param  structureId the primary key of the structure to be copied
185             * @param  nameMap the new structure's locales and localized names
186             * @param  descriptionMap the new structure's locales and localized
187             *         descriptions
188             * @param  serviceContext the service context to be applied. Can set the
189             *         UUID, creation date, modification date, guest permissions, and
190             *         group permissions for the structure.
191             * @return the new structure
192             * @throws PortalException if the user did not have permission to add the
193             *         structure or if a portal exception occurred
194             * @throws SystemException if a system exception occurred
195             */
196            @Override
197            public DDMStructure copyStructure(
198                            long structureId, Map<Locale, String> nameMap,
199                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
200                    throws PortalException, SystemException {
201    
202                    DDMDisplay ddmDisplay = DDMUtil.getDDMDisplay(serviceContext);
203    
204                    DDMPermission.check(
205                            getPermissionChecker(), serviceContext.getScopeGroupId(),
206                            ddmDisplay.getResourceName(), ddmDisplay.getAddStructureActionId());
207    
208                    return ddmStructureLocalService.copyStructure(
209                            getUserId(), structureId, nameMap, descriptionMap, serviceContext);
210            }
211    
212            @Override
213            public DDMStructure copyStructure(
214                            long structureId, ServiceContext serviceContext)
215                    throws PortalException, SystemException {
216    
217                    DDMDisplay ddmDisplay = DDMUtil.getDDMDisplay(serviceContext);
218    
219                    DDMPermission.check(
220                            getPermissionChecker(), serviceContext.getScopeGroupId(),
221                            ddmDisplay.getResourceName(), ddmDisplay.getAddStructureActionId());
222    
223                    return ddmStructureLocalService.copyStructure(
224                            getUserId(), structureId, serviceContext);
225            }
226    
227            /**
228             * Deletes the structure and its resources.
229             *
230             * <p>
231             * Before deleting the structure, the system verifies whether the structure
232             * is required by another entity. If it is needed, an exception is thrown.
233             * </p>
234             *
235             * @param  structureId the primary key of the structure to be deleted
236             * @throws PortalException if the user did not have permission to delete the
237             *         structure or if a portal exception occurred
238             * @throws SystemException if a system exception occurred
239             */
240            @Override
241            public void deleteStructure(long structureId)
242                    throws PortalException, SystemException {
243    
244                    DDMStructurePermission.check(
245                            getPermissionChecker(), structureId, ActionKeys.DELETE);
246    
247                    ddmStructureLocalService.deleteStructure(structureId);
248            }
249    
250            /**
251             * Returns the structure matching the class name ID, structure key, and
252             * group.
253             *
254             * @param  groupId the primary key of the group
255             * @param  classNameId the primary key of the class name for the structure's
256             *         related model
257             * @param  structureKey the unique string identifying the structure
258             * @return the matching structure, or <code>null</code> if a matching
259             *         structure could not be found
260             * @throws PortalException if the user did not have permission to view the
261             *         structure or if a portal exception occurred
262             * @throws SystemException if a system exception occurred
263             */
264            @Override
265            public DDMStructure fetchStructure(
266                            long groupId, long classNameId, String structureKey)
267                    throws PortalException, SystemException {
268    
269                    DDMStructure ddmStructure = ddmStructurePersistence.fetchByG_C_S(
270                            groupId, classNameId, structureKey);
271    
272                    if (ddmStructure != null) {
273                            DDMStructurePermission.check(
274                                    getPermissionChecker(), ddmStructure, ActionKeys.VIEW);
275                    }
276    
277                    return ddmStructure;
278            }
279    
280            /**
281             * Returns the structure with the ID.
282             *
283             * @param  structureId the primary key of the structure
284             * @return the structure with the ID
285             * @throws PortalException if the user did not have permission to view the
286             *         structure or if a structure with the ID could not be found
287             * @throws SystemException if a system exception occurred
288             */
289            @Override
290            public DDMStructure getStructure(long structureId)
291                    throws PortalException, SystemException {
292    
293                    DDMStructurePermission.check(
294                            getPermissionChecker(), structureId, ActionKeys.VIEW);
295    
296                    return ddmStructurePersistence.findByPrimaryKey(structureId);
297            }
298    
299            /**
300             * Returns the structure matching the class name ID, structure key, and
301             * group.
302             *
303             * @param  groupId the primary key of the structure's group
304             * @param  classNameId the primary key of the class name for the structure's
305             *         related model
306             * @param  structureKey the unique string identifying the structure
307             * @return the matching structure
308             * @throws PortalException if the user did not have permission to view the
309             *         structure or if a matching structure could not be found
310             * @throws SystemException if a system exception occurred
311             */
312            @Override
313            public DDMStructure getStructure(
314                            long groupId, long classNameId, String structureKey)
315                    throws PortalException, SystemException {
316    
317                    DDMStructurePermission.check(
318                            getPermissionChecker(), groupId, classNameId, structureKey,
319                            ActionKeys.VIEW);
320    
321                    return ddmStructureLocalService.getStructure(
322                            groupId, classNameId, structureKey);
323            }
324    
325            /**
326             * Returns the structure matching the class name ID, structure key, and
327             * group, optionally in the global scope.
328             *
329             * <p>
330             * This method first searches in the group. If the structure is still not
331             * found and <code>includeGlobalStructures</code> is set to
332             * <code>true</code>, this method searches the global group.
333             * </p>
334             *
335             * @param  groupId the primary key of the structure's group
336             * @param  classNameId the primary key of the class name for the structure's
337             *         related model
338             * @param  structureKey the unique string identifying the structure
339             * @param  includeGlobalStructures whether to include the global scope in
340             *         the search
341             * @return the matching structure
342             * @throws PortalException if the user did not have permission to view the
343             *         structure or if a matching structure could not be found
344             * @throws SystemException if a system exception occurred
345             */
346            @Override
347            public DDMStructure getStructure(
348                            long groupId, long classNameId, String structureKey,
349                            boolean includeGlobalStructures)
350                    throws PortalException, SystemException {
351    
352                    DDMStructurePermission.check(
353                            getPermissionChecker(), groupId, classNameId, structureKey,
354                            ActionKeys.VIEW);
355    
356                    return ddmStructureLocalService.getStructure(
357                            groupId, classNameId, structureKey, includeGlobalStructures);
358            }
359    
360            /**
361             * Returns all the structures in the group that the user has permission to
362             * view.
363             *
364             * @param  groupId the primary key of the group
365             * @return the structures in the group that the user has permission to view
366             * @throws SystemException if a system exception occurred
367             */
368            @Override
369            public List<DDMStructure> getStructures(long groupId)
370                    throws SystemException {
371    
372                    return ddmStructurePersistence.filterFindByGroupId(groupId);
373            }
374    
375            /**
376             * Returns all the structures in the groups that the user has permission to
377             * view.
378             *
379             * @param  groupIds the primary key of the groups
380             * @return the structures in the groups that the user has permission to view
381             * @throws SystemException if a system exception occurred
382             */
383            @Override
384            public List<DDMStructure> getStructures(long[] groupIds)
385                    throws SystemException {
386    
387                    return ddmStructurePersistence.filterFindByGroupId(groupIds);
388            }
389    
390            /**
391             * Returns an ordered range of all the structures matching the groups and
392             * class name IDs, and matching the keywords in the structure names and
393             * descriptions.
394             *
395             * <p>
396             * Useful when paginating results. Returns a maximum of <code>end -
397             * start</code> instances. <code>start</code> and <code>end</code> are not
398             * primary keys, they are indexes in the result set. Thus, <code>0</code>
399             * refers to the first result in the set. Setting both <code>start</code>
400             * and <code>end</code> to {@link
401             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
402             * result set.
403             * </p>
404             *
405             * @param  companyId the primary key of the structure's company
406             * @param  groupIds the primary keys of the groups
407             * @param  classNameIds the primary keys of the class names of the models
408             *         the structures are related to
409             * @param  keywords the keywords (space separated), which may occur in the
410             *         structure's name or description (optionally <code>null</code>)
411             * @param  start the lower bound of the range of structures to return
412             * @param  end the upper bound of the range of structures to return (not
413             *         inclusive)
414             * @param  orderByComparator the comparator to order the structures
415             *         (optionally <code>null</code>)
416             * @return the range of matching structures ordered by the comparator
417             * @throws SystemException if a system exception occurred
418             */
419            @Override
420            public List<DDMStructure> search(
421                            long companyId, long[] groupIds, long[] classNameIds,
422                            String keywords, int start, int end,
423                            OrderByComparator orderByComparator)
424                    throws SystemException {
425    
426                    return ddmStructureFinder.filterFindByKeywords(
427                            companyId, groupIds, classNameIds, keywords, start, end,
428                            orderByComparator);
429            }
430    
431            /**
432             * Returns an ordered range of all the structures matching the groups, class
433             * name IDs, name keyword, description keyword, storage type, and type.
434             *
435             * <p>
436             * Useful when paginating results. Returns a maximum of <code>end -
437             * start</code> instances. <code>start</code> and <code>end</code> are not
438             * primary keys, they are indexes in the result set. Thus, <code>0</code>
439             * refers to the first result in the set. Setting both <code>start</code>
440             * and <code>end</code> to {@link
441             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
442             * result set.
443             * </p>
444             *
445             * @param  companyId the primary key of the structure's company
446             * @param  groupIds the primary keys of the groups
447             * @param  classNameIds the primary keys of the class names of the models
448             *         the structures are related to
449             * @param  name the name keywords
450             * @param  description the description keywords
451             * @param  storageType the structure's storage type. It can be "xml" or
452             *         "expando". For more information, see {@link
453             *         com.liferay.portlet.dynamicdatamapping.storage.StorageType}.
454             * @param  type the structure's type. For more information, see {@link
455             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants}.
456             * @param  andOperator whether every field must match its keywords, or just
457             *         one field
458             * @param  start the lower bound of the range of structures to return
459             * @param  end the upper bound of the range of structures to return (not
460             *         inclusive)
461             * @param  orderByComparator the comparator to order the structures
462             *         (optionally <code>null</code>)
463             * @return the range of matching structures ordered by the comparator
464             * @throws SystemException if a system exception occurred
465             */
466            @Override
467            public List<DDMStructure> search(
468                            long companyId, long[] groupIds, long[] classNameIds, String name,
469                            String description, String storageType, int type,
470                            boolean andOperator, int start, int end,
471                            OrderByComparator orderByComparator)
472                    throws SystemException {
473    
474                    return ddmStructureFinder.filterFindByC_G_C_N_D_S_T(
475                            companyId, groupIds, classNameIds, name, description, storageType,
476                            type, andOperator, start, end, orderByComparator);
477            }
478    
479            /**
480             * Returns the number of structures matching the groups and class name IDs,
481             * and matching the keywords in the structure names and descriptions.
482             *
483             * @param  companyId the primary key of the structure's company
484             * @param  groupIds the primary keys of the groups
485             * @param  classNameIds the primary keys of the class names of the models
486             *         the structures are related to
487             * @param  keywords the keywords (space separated), which may occur in the
488             *         structure's name or description (optionally <code>null</code>)
489             * @return the number of matching structures
490             * @throws SystemException if a system exception occurred
491             */
492            @Override
493            public int searchCount(
494                            long companyId, long[] groupIds, long[] classNameIds,
495                            String keywords)
496                    throws SystemException {
497    
498                    return ddmStructureFinder.filterCountByKeywords(
499                            companyId, groupIds, classNameIds, keywords);
500            }
501    
502            /**
503             * Returns the number of structures matching the groups, class name IDs,
504             * name keyword, description keyword, storage type, and type
505             *
506             * @param  companyId the primary key of the structure's company
507             * @param  groupIds the primary keys of the groups
508             * @param  classNameIds the primary keys of the class names of the models
509             *         the structure's are related to
510             * @param  name the name keywords
511             * @param  description the description keywords
512             * @param  storageType the structure's storage type. It can be "xml" or
513             *         "expando". For more information, see {@link
514             *         com.liferay.portlet.dynamicdatamapping.storage.StorageType}.
515             * @param  type the structure's type. For more information, see {@link
516             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants}.
517             * @param  andOperator whether every field must match its keywords, or just
518             *         one field
519             * @return the number of matching structures
520             * @throws SystemException if a system exception occurred
521             */
522            @Override
523            public int searchCount(
524                            long companyId, long[] groupIds, long[] classNameIds, String name,
525                            String description, String storageType, int type,
526                            boolean andOperator)
527                    throws SystemException {
528    
529                    return ddmStructureFinder.filterCountByC_G_C_N_D_S_T(
530                            companyId, groupIds, classNameIds, name, description, storageType,
531                            type, andOperator);
532            }
533    
534            /**
535             * Updates the structure matching the class name ID, structure key, and
536             * group, replacing its old parent structure, name map, description map, and
537             * XSD with new ones.
538             *
539             * @param  groupId the primary key of the group
540             * @param  parentStructureId the primary key of the new parent structure
541             * @param  classNameId the primary key of the class name for the structure's
542             *         related model
543             * @param  structureKey the unique string identifying the structure
544             * @param  nameMap the structure's new locales and localized names
545             * @param  descriptionMap the structure's new locales and localized
546             *         description
547             * @param  xsd the structure's new XML schema definition
548             * @param  serviceContext the service context to be applied. Can set the
549             *         modification date.
550             * @return the updated structure
551             * @throws PortalException if the user did not have permission to update the
552             *         structure or if a portal exception occurred
553             * @throws SystemException if a system exception occurred
554             */
555            @Override
556            public DDMStructure updateStructure(
557                            long groupId, long parentStructureId, long classNameId,
558                            String structureKey, Map<Locale, String> nameMap,
559                            Map<Locale, String> descriptionMap, String xsd,
560                            ServiceContext serviceContext)
561                    throws PortalException, SystemException {
562    
563                    DDMStructurePermission.check(
564                            getPermissionChecker(), groupId, classNameId, structureKey,
565                            ActionKeys.UPDATE);
566    
567                    return ddmStructureLocalService.updateStructure(
568                            groupId, parentStructureId, classNameId, structureKey, nameMap,
569                            descriptionMap, xsd, serviceContext);
570            }
571    
572            /**
573             * Updates the structure matching the structure ID, replacing the old parent
574             * structure ID, name map, description map, and XSD with the new values.
575             *
576             * @param  structureId the primary key of the structure
577             * @param  parentStructureId the new parent structure primary key
578             * @param  nameMap the structure's new locales and localized names
579             * @param  descriptionMap the structure's new locales and localized
580             *         description
581             * @param  xsd the new XML schema definition of the structure
582             * @param  serviceContext the service context to be applied. Can set the
583             *         modification date.
584             * @return the updated structure
585             * @throws PortalException if the user did not have permission to update the
586             *         structure or if a portal exception occurred
587             * @throws SystemException if a system exception occurred
588             */
589            @Override
590            public DDMStructure updateStructure(
591                            long structureId, long parentStructureId,
592                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
593                            String xsd, ServiceContext serviceContext)
594                    throws PortalException, SystemException {
595    
596                    DDMStructurePermission.check(
597                            getPermissionChecker(), structureId, ActionKeys.UPDATE);
598    
599                    return ddmStructureLocalService.updateStructure(
600                            structureId, parentStructureId, nameMap, descriptionMap, xsd,
601                            serviceContext);
602            }
603    
604    }