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