001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.SortedArrayList;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.model.Repository;
025    import com.liferay.portal.repository.liferayrepository.util.LiferayBase;
026    import com.liferay.portal.service.RepositoryService;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
029    import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
030    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
033    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
034    import com.liferay.portlet.documentlibrary.model.DLFolder;
035    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
036    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
037    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
038    import com.liferay.portlet.documentlibrary.service.DLFileEntryService;
039    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
040    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService;
041    import com.liferay.portlet.documentlibrary.service.DLFileVersionService;
042    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
043    import com.liferay.portlet.documentlibrary.service.DLFolderService;
044    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
045    import com.liferay.portlet.dynamicdatamapping.storage.Field;
046    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
047    
048    import java.util.ArrayList;
049    import java.util.HashMap;
050    import java.util.List;
051    import java.util.Set;
052    
053    /**
054     * @author Alexander Chow
055     */
056    public abstract class LiferayRepositoryBase extends LiferayBase {
057    
058            public LiferayRepositoryBase(
059                    RepositoryService repositoryService,
060                    DLAppHelperLocalService dlAppHelperLocalService,
061                    DLFileEntryLocalService dlFileEntryLocalService,
062                    DLFileEntryService dlFileEntryService,
063                    DLFileVersionLocalService dlFileVersionLocalService,
064                    DLFileVersionService dlFileVersionService,
065                    DLFolderLocalService dlFolderLocalService,
066                    DLFolderService dlFolderService, long repositoryId) {
067    
068                    this.repositoryService = repositoryService;
069                    this.dlAppHelperLocalService = dlAppHelperLocalService;
070                    this.dlFileEntryLocalService = dlFileEntryLocalService;
071                    this.dlFileEntryService = dlFileEntryService;
072                    this.dlFileVersionLocalService = dlFileVersionLocalService;
073                    this.dlFileVersionService = dlFileVersionService;
074                    this.dlFolderLocalService = dlFolderLocalService;
075                    this.dlFolderService = dlFolderService;
076    
077                    initByRepositoryId(repositoryId);
078            }
079    
080            public LiferayRepositoryBase(
081                    RepositoryService repositoryService,
082                    DLAppHelperLocalService dlAppHelperLocalService,
083                    DLFileEntryLocalService dlFileEntryLocalService,
084                    DLFileEntryService dlFileEntryService,
085                    DLFileVersionLocalService dlFileVersionLocalService,
086                    DLFileVersionService dlFileVersionService,
087                    DLFolderLocalService dlFolderLocalService,
088                    DLFolderService dlFolderService, long folderId, long fileEntryId,
089                    long fileVersionId) {
090    
091                    this.repositoryService = repositoryService;
092                    this.dlAppHelperLocalService = dlAppHelperLocalService;
093                    this.dlFileEntryLocalService = dlFileEntryLocalService;
094                    this.dlFileEntryService = dlFileEntryService;
095                    this.dlFileVersionLocalService = dlFileVersionLocalService;
096                    this.dlFileVersionService = dlFileVersionService;
097                    this.dlFolderLocalService = dlFolderLocalService;
098                    this.dlFolderService = dlFolderService;
099    
100                    if (folderId != 0) {
101                            initByFolderId(folderId);
102                    }
103                    else if (fileEntryId != 0) {
104                            initByFileEntryId(fileEntryId);
105                    }
106                    else if (fileVersionId != 0) {
107                            initByFileVersionId(fileVersionId);
108                    }
109            }
110    
111            public long getRepositoryId() {
112                    return _repositoryId;
113            }
114    
115            protected void addFileEntryResources(
116                            DLFileEntry dlFileEntry, ServiceContext serviceContext)
117                    throws PortalException, SystemException {
118    
119                    if (serviceContext.getAddGroupPermissions() ||
120                            serviceContext.getAddGuestPermissions()) {
121    
122                            dlFileEntryLocalService.addFileEntryResources(
123                                    dlFileEntry, serviceContext.getAddGroupPermissions(),
124                                    serviceContext.getAddGuestPermissions());
125                    }
126                    else {
127                            dlFileEntryLocalService.addFileEntryResources(
128                                    dlFileEntry, serviceContext.getGroupPermissions(),
129                                    serviceContext.getGuestPermissions());
130                    }
131            }
132    
133            protected HashMap<String, Fields> getFieldsMap(
134                            ServiceContext serviceContext, long fileEntryTypeId)
135                    throws PortalException, SystemException {
136    
137                    HashMap<String, Fields> fieldsMap = new HashMap<String, Fields>();
138    
139                    if (fileEntryTypeId <= 0) {
140                            return fieldsMap;
141                    }
142    
143                    DLFileEntryType fileEntryType =
144                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
145    
146                    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
147    
148                    for (DDMStructure ddmStructure : ddmStructures) {
149                            String namespace = String.valueOf(ddmStructure.getStructureId());
150    
151                            Set<String> fieldNames = ddmStructure.getFieldNames();
152    
153                            Fields fields = (Fields)serviceContext.getAttribute(
154                                    Fields.class.getName() + ddmStructure.getStructureId());
155    
156                            if (fields == null) {
157                                    fields = new Fields();
158    
159                                    for (String name : fieldNames) {
160                                            Field field = new Field();
161    
162                                            field.setName(name);
163    
164                                            String value = ParamUtil.getString(
165                                                    serviceContext, namespace + name);
166    
167                                            field.setValue(value);
168    
169                                            fields.put(field);
170                                    }
171                            }
172    
173                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
174                    }
175    
176                    return fieldsMap;
177            }
178    
179            protected long getGroupId() {
180                    return _groupId;
181            }
182    
183            protected SortedArrayList<Long> getLongList(
184                    ServiceContext serviceContext, String name) {
185    
186                    String value = ParamUtil.getString(serviceContext, name);
187    
188                    if (value == null) {
189                            return new SortedArrayList<Long>();
190                    }
191    
192                    long[] longArray = StringUtil.split(value, 0L);
193    
194                    SortedArrayList<Long> longList = new SortedArrayList<Long>();
195    
196                    for (long longValue : longArray) {
197                            longList.add(longValue);
198                    }
199    
200                    return longList;
201            }
202    
203            protected void initByFileEntryId(long fileEntryId) {
204                    try {
205                            DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
206                                    fileEntryId);
207    
208                            initByRepositoryId(dlFileEntry.getRepositoryId());
209                    }
210                    catch (Exception e) {
211                            if (_log.isTraceEnabled()) {
212                                    if (e instanceof NoSuchFileEntryException) {
213                                            _log.trace(e.getMessage());
214                                    }
215                                    else {
216                                            _log.trace(e, e);
217                                    }
218                            }
219                    }
220            }
221    
222            protected void initByFileVersionId(long fileVersionId) {
223                    try {
224                            DLFileVersion dlFileVersion =
225                                    dlFileVersionLocalService.getFileVersion(fileVersionId);
226    
227                            initByRepositoryId(dlFileVersion.getRepositoryId());
228                    }
229                    catch (Exception e) {
230                            if (_log.isTraceEnabled()) {
231                                    if (e instanceof NoSuchFileVersionException) {
232                                            _log.trace(e.getMessage());
233                                    }
234                                    else {
235                                            _log.trace(e, e);
236                                    }
237                            }
238                    }
239            }
240    
241            protected void initByFolderId(long folderId) {
242                    try {
243                            DLFolder dlFolder = dlFolderLocalService.getFolder(folderId);
244    
245                            initByRepositoryId(dlFolder.getRepositoryId());
246                    }
247                    catch (Exception e) {
248                            if (_log.isTraceEnabled()) {
249                                    if (e instanceof NoSuchFolderException) {
250                                            _log.trace(e.getMessage());
251                                    }
252                                    else {
253                                            _log.trace(e, e);
254                                    }
255                            }
256                    }
257            }
258    
259            protected void initByRepositoryId(long repositoryId) {
260                    _repositoryId = repositoryId;
261                    _groupId = repositoryId;
262    
263                    try {
264                            Repository repository = repositoryService.getRepository(
265                                    repositoryId);
266    
267                            _repositoryId = repository.getRepositoryId();
268                            _groupId = repository.getGroupId();
269                            _dlFolderId = repository.getDlFolderId();
270                    }
271                    catch (Exception e) {
272                    }
273            }
274    
275            protected boolean isDefaultRepository() {
276                    if (_groupId == _repositoryId) {
277                            return true;
278                    }
279                    else {
280                            return false;
281                    }
282            }
283    
284            protected long toFolderId(long folderId) {
285                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
286                            return _dlFolderId;
287                    }
288                    else {
289                            return folderId;
290                    }
291            }
292    
293            protected List<Long> toFolderIds(List<Long> folderIds) {
294                    List<Long> toFolderIds = new ArrayList<Long>(folderIds.size());
295    
296                    for (long folderId : folderIds) {
297                            toFolderIds.add(toFolderId(folderId));
298                    }
299    
300                    return toFolderIds;
301            }
302    
303            protected DLAppHelperLocalService dlAppHelperLocalService;
304            protected DLFileEntryLocalService dlFileEntryLocalService;
305            protected DLFileEntryService dlFileEntryService;
306            protected DLFileVersionLocalService dlFileVersionLocalService;
307            protected DLFileVersionService dlFileVersionService;
308            protected DLFolderLocalService dlFolderLocalService;
309            protected DLFolderService dlFolderService;
310            protected RepositoryService repositoryService;
311    
312            private static Log _log = LogFactoryUtil.getLog(
313                    LiferayRepositoryBase.class);
314    
315            private long _dlFolderId;
316            private long _groupId;
317            private long _repositoryId;
318    
319    }