001    /**
002     * Copyright (c) 2000-present 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.repository.liferayrepository;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.capabilities.Capability;
019    import com.liferay.portal.kernel.repository.capabilities.CapabilityProvider;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.SortedArrayList;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.service.RepositoryLocalService;
024    import com.liferay.portal.service.RepositoryService;
025    import com.liferay.portal.service.ResourceLocalService;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
030    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
031    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
032    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
033    import com.liferay.portlet.documentlibrary.service.DLFileEntryService;
034    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalService;
035    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
036    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService;
037    import com.liferay.portlet.documentlibrary.service.DLFileVersionService;
038    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
039    import com.liferay.portlet.documentlibrary.service.DLFolderService;
040    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
041    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
042    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
043    
044    import java.util.ArrayList;
045    import java.util.HashMap;
046    import java.util.List;
047    
048    /**
049     * @author Alexander Chow
050     */
051    public abstract class LiferayRepositoryBase implements CapabilityProvider {
052    
053            public LiferayRepositoryBase(
054                    RepositoryLocalService repositoryLocalService,
055                    RepositoryService repositoryService,
056                    DLAppHelperLocalService dlAppHelperLocalService,
057                    DLFileEntryLocalService dlFileEntryLocalService,
058                    DLFileEntryService dlFileEntryService,
059                    DLFileEntryTypeLocalService dlFileEntryTypeLocalService,
060                    DLFileVersionLocalService dlFileVersionLocalService,
061                    DLFileVersionService dlFileVersionService,
062                    DLFolderLocalService dlFolderLocalService,
063                    DLFolderService dlFolderService,
064                    ResourceLocalService resourceLocalService, long groupId,
065                    long repositoryId, long dlFolderId) {
066    
067                    this.repositoryLocalService = repositoryLocalService;
068                    this.repositoryService = repositoryService;
069                    this.dlAppHelperLocalService = dlAppHelperLocalService;
070                    this.dlFileEntryLocalService = dlFileEntryLocalService;
071                    this.dlFileEntryService = dlFileEntryService;
072                    this.dlFileEntryTypeLocalService = dlFileEntryTypeLocalService;
073                    this.dlFileVersionLocalService = dlFileVersionLocalService;
074                    this.dlFileVersionService = dlFileVersionService;
075                    this.dlFolderLocalService = dlFolderLocalService;
076                    this.dlFolderService = dlFolderService;
077                    this.resourceLocalService = resourceLocalService;
078                    _groupId = groupId;
079                    _repositoryId = repositoryId;
080                    _dlFolderId = dlFolderId;
081            }
082    
083            @Override
084            public <T extends Capability> T getCapability(Class<T> capabilityClass) {
085                    throw new IllegalArgumentException(
086                            String.format(
087                                    "Capability %s is not supported by repository %s",
088                                    capabilityClass.getName(), getRepositoryId()));
089            }
090    
091            public long getRepositoryId() {
092                    return _repositoryId;
093            }
094    
095            @Override
096            public <T extends Capability> boolean isCapabilityProvided(
097                    Class<T> capabilityClass) {
098    
099                    return false;
100            }
101    
102            protected void addFileEntryResources(
103                            DLFileEntry dlFileEntry, ServiceContext serviceContext)
104                    throws PortalException {
105    
106                    if (serviceContext.isAddGroupPermissions() ||
107                            serviceContext.isAddGuestPermissions()) {
108    
109                            resourceLocalService.addResources(
110                                    dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
111                                    dlFileEntry.getUserId(), DLFileEntry.class.getName(),
112                                    dlFileEntry.getFileEntryId(), false,
113                                    serviceContext.isAddGroupPermissions(),
114                                    serviceContext.isAddGuestPermissions());
115                    }
116                    else {
117                            if (serviceContext.isDeriveDefaultPermissions()) {
118                                    serviceContext.deriveDefaultPermissions(
119                                            dlFileEntry.getRepositoryId(),
120                                            DLFileEntryConstants.getClassName());
121                            }
122    
123                            resourceLocalService.addModelResources(
124                                    dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
125                                    dlFileEntry.getUserId(), DLFileEntry.class.getName(),
126                                    dlFileEntry.getFileEntryId(),
127                                    serviceContext.getGroupPermissions(),
128                                    serviceContext.getGuestPermissions());
129                    }
130            }
131    
132            protected long getDefaultFileEntryTypeId(
133                            ServiceContext serviceContext, long folderId)
134                    throws PortalException {
135    
136                    folderId = dlFolderLocalService.getFolderId(
137                            serviceContext.getCompanyId(), folderId);
138    
139                    return dlFileEntryTypeLocalService.getDefaultFileEntryTypeId(folderId);
140            }
141    
142            protected HashMap<String, Fields> getFieldsMap(
143                            ServiceContext serviceContext, long fileEntryTypeId)
144                    throws PortalException {
145    
146                    HashMap<String, Fields> fieldsMap = new HashMap<String, Fields>();
147    
148                    if (fileEntryTypeId <= 0) {
149                            return fieldsMap;
150                    }
151    
152                    DLFileEntryType fileEntryType =
153                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
154    
155                    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
156    
157                    for (DDMStructure ddmStructure : ddmStructures) {
158                            String namespace = String.valueOf(ddmStructure.getStructureId());
159    
160                            Fields fields = (Fields)serviceContext.getAttribute(
161                                    Fields.class.getName() + ddmStructure.getStructureId());
162    
163                            if (fields == null) {
164                                    fields = DDMUtil.getFields(
165                                            ddmStructure.getStructureId(), namespace, serviceContext);
166                            }
167    
168                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
169                    }
170    
171                    return fieldsMap;
172            }
173    
174            protected long getGroupId() {
175                    return _groupId;
176            }
177    
178            protected SortedArrayList<Long> getLongList(
179                    ServiceContext serviceContext, String name) {
180    
181                    String value = ParamUtil.getString(serviceContext, name);
182    
183                    if (value == null) {
184                            return new SortedArrayList<Long>();
185                    }
186    
187                    long[] longArray = StringUtil.split(value, 0L);
188    
189                    SortedArrayList<Long> longList = new SortedArrayList<Long>();
190    
191                    for (long longValue : longArray) {
192                            longList.add(longValue);
193                    }
194    
195                    return longList;
196            }
197    
198            protected boolean isDefaultRepository() {
199                    if (_groupId == _repositoryId) {
200                            return true;
201                    }
202                    else {
203                            return false;
204                    }
205            }
206    
207            protected long toFolderId(long folderId) {
208                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
209                            return _dlFolderId;
210                    }
211                    else {
212                            return folderId;
213                    }
214            }
215    
216            protected List<Long> toFolderIds(List<Long> folderIds) {
217                    List<Long> toFolderIds = new ArrayList<Long>(folderIds.size());
218    
219                    for (long folderId : folderIds) {
220                            toFolderIds.add(toFolderId(folderId));
221                    }
222    
223                    return toFolderIds;
224            }
225    
226            protected DLAppHelperLocalService dlAppHelperLocalService;
227            protected DLFileEntryLocalService dlFileEntryLocalService;
228            protected DLFileEntryService dlFileEntryService;
229            protected DLFileEntryTypeLocalService dlFileEntryTypeLocalService;
230            protected DLFileVersionLocalService dlFileVersionLocalService;
231            protected DLFileVersionService dlFileVersionService;
232            protected DLFolderLocalService dlFolderLocalService;
233            protected DLFolderService dlFolderService;
234            protected RepositoryLocalService repositoryLocalService;
235            protected RepositoryService repositoryService;
236            protected ResourceLocalService resourceLocalService;
237    
238            private final long _dlFolderId;
239            private long _groupId;
240            private final long _repositoryId;
241    
242    }