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.repository.liferayrepository;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.SortedArrayList;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.repository.liferayrepository.util.LiferayBase;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.RepositoryLocalService;
025    import com.liferay.portal.service.RepositoryService;
026    import com.liferay.portal.service.ResourceLocalService;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
031    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
032    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
033    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
034    import com.liferay.portlet.documentlibrary.service.DLFileEntryService;
035    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalService;
036    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
037    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService;
038    import com.liferay.portlet.documentlibrary.service.DLFileVersionService;
039    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
040    import com.liferay.portlet.documentlibrary.service.DLFolderService;
041    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
042    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
043    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
044    
045    import java.util.ArrayList;
046    import java.util.HashMap;
047    import java.util.List;
048    
049    /**
050     * @author Alexander Chow
051     */
052    public abstract class LiferayRepositoryBase extends LiferayBase {
053    
054            public LiferayRepositoryBase(
055                    RepositoryLocalService repositoryLocalService,
056                    RepositoryService repositoryService,
057                    DLAppHelperLocalService dlAppHelperLocalService,
058                    DLFileEntryLocalService dlFileEntryLocalService,
059                    DLFileEntryService dlFileEntryService,
060                    DLFileEntryTypeLocalService dlFileEntryTypeLocalService,
061                    DLFileVersionLocalService dlFileVersionLocalService,
062                    DLFileVersionService dlFileVersionService,
063                    DLFolderLocalService dlFolderLocalService,
064                    DLFolderService dlFolderService,
065                    ResourceLocalService resourceLocalService, long repositoryId) {
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    
079                    initByRepositoryId(repositoryId);
080            }
081    
082            public LiferayRepositoryBase(
083                            RepositoryLocalService repositoryLocalService,
084                            RepositoryService repositoryService,
085                            DLAppHelperLocalService dlAppHelperLocalService,
086                            DLFileEntryLocalService dlFileEntryLocalService,
087                            DLFileEntryService dlFileEntryService,
088                            DLFileEntryTypeLocalService dlFileEntryTypeLocalService,
089                            DLFileVersionLocalService dlFileVersionLocalService,
090                            DLFileVersionService dlFileVersionService,
091                            DLFolderLocalService dlFolderLocalService,
092                            DLFolderService dlFolderService,
093                            ResourceLocalService resourceLocalService, long folderId,
094                            long fileEntryId, long fileVersionId)
095                    throws PrincipalException {
096    
097                    this.repositoryLocalService = repositoryLocalService;
098                    this.repositoryService = repositoryService;
099                    this.dlAppHelperLocalService = dlAppHelperLocalService;
100                    this.dlFileEntryLocalService = dlFileEntryLocalService;
101                    this.dlFileEntryService = dlFileEntryService;
102                    this.dlFileEntryTypeLocalService = dlFileEntryTypeLocalService;
103                    this.dlFileVersionLocalService = dlFileVersionLocalService;
104                    this.dlFileVersionService = dlFileVersionService;
105                    this.dlFolderLocalService = dlFolderLocalService;
106                    this.dlFolderService = dlFolderService;
107                    this.resourceLocalService = resourceLocalService;
108    
109                    if (folderId != 0) {
110                            initByFolderId(folderId);
111                    }
112                    else if (fileEntryId != 0) {
113                            initByFileEntryId(fileEntryId);
114                    }
115                    else if (fileVersionId != 0) {
116                            initByFileVersionId(fileVersionId);
117                    }
118            }
119    
120            public long getRepositoryId() {
121                    return _repositoryId;
122            }
123    
124            protected void addFileEntryResources(
125                            DLFileEntry dlFileEntry, ServiceContext serviceContext)
126                    throws PortalException, SystemException {
127    
128                    if (serviceContext.isAddGroupPermissions() ||
129                            serviceContext.isAddGuestPermissions()) {
130    
131                            resourceLocalService.addResources(
132                                    dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
133                                    dlFileEntry.getUserId(), DLFileEntry.class.getName(),
134                                    dlFileEntry.getFileEntryId(), false,
135                                    serviceContext.isAddGroupPermissions(),
136                                    serviceContext.isAddGuestPermissions());
137                    }
138                    else {
139                            if (serviceContext.isDeriveDefaultPermissions()) {
140                                    serviceContext.deriveDefaultPermissions(
141                                            dlFileEntry.getRepositoryId(),
142                                            DLFileEntryConstants.getClassName());
143                            }
144    
145                            resourceLocalService.addModelResources(
146                                    dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
147                                    dlFileEntry.getUserId(), DLFileEntry.class.getName(),
148                                    dlFileEntry.getFileEntryId(),
149                                    serviceContext.getGroupPermissions(),
150                                    serviceContext.getGuestPermissions());
151                    }
152            }
153    
154            protected long getDefaultFileEntryTypeId(
155                            ServiceContext serviceContext, long folderId)
156                    throws PortalException, SystemException {
157    
158                    folderId = dlFolderLocalService.getFolderId(
159                            serviceContext.getCompanyId(), folderId);
160    
161                    return dlFileEntryTypeLocalService.getDefaultFileEntryTypeId(folderId);
162            }
163    
164            protected HashMap<String, Fields> getFieldsMap(
165                            ServiceContext serviceContext, long fileEntryTypeId)
166                    throws PortalException, SystemException {
167    
168                    HashMap<String, Fields> fieldsMap = new HashMap<String, Fields>();
169    
170                    if (fileEntryTypeId <= 0) {
171                            return fieldsMap;
172                    }
173    
174                    DLFileEntryType fileEntryType =
175                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
176    
177                    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
178    
179                    for (DDMStructure ddmStructure : ddmStructures) {
180                            String namespace = String.valueOf(ddmStructure.getStructureId());
181    
182                            Fields fields = (Fields)serviceContext.getAttribute(
183                                    Fields.class.getName() + ddmStructure.getStructureId());
184    
185                            if (fields == null) {
186                                    fields = DDMUtil.getFields(
187                                            ddmStructure.getStructureId(), namespace, serviceContext);
188                            }
189    
190                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
191                    }
192    
193                    return fieldsMap;
194            }
195    
196            protected long getGroupId() {
197                    return _groupId;
198            }
199    
200            protected SortedArrayList<Long> getLongList(
201                    ServiceContext serviceContext, String name) {
202    
203                    String value = ParamUtil.getString(serviceContext, name);
204    
205                    if (value == null) {
206                            return new SortedArrayList<Long>();
207                    }
208    
209                    long[] longArray = StringUtil.split(value, 0L);
210    
211                    SortedArrayList<Long> longList = new SortedArrayList<Long>();
212    
213                    for (long longValue : longArray) {
214                            longList.add(longValue);
215                    }
216    
217                    return longList;
218            }
219    
220            protected abstract void initByFileEntryId(long fileEntryId)
221                    throws PrincipalException;
222    
223            protected abstract void initByFileVersionId(long fileVersionId)
224                    throws PrincipalException;
225    
226            protected abstract void initByFolderId(long folderId)
227                    throws PrincipalException;
228    
229            protected abstract void initByRepositoryId(long repositoryId);
230    
231            protected boolean isDefaultRepository() {
232                    if (_groupId == _repositoryId) {
233                            return true;
234                    }
235                    else {
236                            return false;
237                    }
238            }
239    
240            protected void setDlFolderId(long dlFolderId) {
241                    _dlFolderId = dlFolderId;
242            }
243    
244            protected void setGroupId(long groupId) {
245                    _groupId = groupId;
246            }
247    
248            protected void setRepositoryId(long repositoryId) {
249                    _repositoryId = repositoryId;
250            }
251    
252            protected long toFolderId(long folderId) {
253                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
254                            return _dlFolderId;
255                    }
256                    else {
257                            return folderId;
258                    }
259            }
260    
261            protected List<Long> toFolderIds(List<Long> folderIds) {
262                    List<Long> toFolderIds = new ArrayList<Long>(folderIds.size());
263    
264                    for (long folderId : folderIds) {
265                            toFolderIds.add(toFolderId(folderId));
266                    }
267    
268                    return toFolderIds;
269            }
270    
271            protected DLAppHelperLocalService dlAppHelperLocalService;
272            protected DLFileEntryLocalService dlFileEntryLocalService;
273            protected DLFileEntryService dlFileEntryService;
274            protected DLFileEntryTypeLocalService dlFileEntryTypeLocalService;
275            protected DLFileVersionLocalService dlFileVersionLocalService;
276            protected DLFileVersionService dlFileVersionService;
277            protected DLFolderLocalService dlFolderLocalService;
278            protected DLFolderService dlFolderService;
279            protected RepositoryLocalService repositoryLocalService;
280            protected RepositoryService repositoryService;
281            protected ResourceLocalService resourceLocalService;
282    
283            private long _dlFolderId;
284            private long _groupId;
285            private long _repositoryId;
286    
287    }