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.documentlibrary.service.impl;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
024    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
025    import com.liferay.portlet.documentlibrary.model.DLSync;
026    import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
027    import com.liferay.portlet.documentlibrary.service.base.DLSyncLocalServiceBaseImpl;
028    
029    import java.util.Date;
030    
031    /**
032     * @author Michael Young
033     */
034    public class DLSyncLocalServiceImpl extends DLSyncLocalServiceBaseImpl {
035    
036            /**
037             * @deprecated As of 6.2.0, replaced by {@link #addSync(long, String, long,
038             *             long, long, String, String, String, String)}
039             */
040            public DLSync addSync(
041                            long fileId, String fileUuid, long companyId, long repositoryId,
042                            long parentFolderId, String name, String type, String version)
043                    throws PortalException, SystemException {
044    
045                    return addSync(
046                            fileId, fileUuid, companyId, repositoryId, parentFolderId, name,
047                            StringPool.BLANK, type, version);
048            }
049    
050            public DLSync addSync(
051                            long fileId, String fileUuid, long companyId, long repositoryId,
052                            long parentFolderId, String name, String description, String type,
053                            String version)
054                    throws PortalException, SystemException {
055    
056                    if (!isDefaultRepository(fileId, parentFolderId)) {
057                            return null;
058                    }
059    
060                    Date now = new Date();
061    
062                    long syncId = counterLocalService.increment();
063    
064                    DLSync dlSync = dlSyncPersistence.create(syncId);
065    
066                    dlSync.setCompanyId(companyId);
067                    dlSync.setCreateDate(now.getTime());
068                    dlSync.setDescription(description);
069                    dlSync.setModifiedDate(now.getTime());
070                    dlSync.setFileId(fileId);
071                    dlSync.setFileUuid(fileUuid);
072                    dlSync.setRepositoryId(repositoryId);
073                    dlSync.setParentFolderId(parentFolderId);
074                    dlSync.setEvent(DLSyncConstants.EVENT_ADD);
075                    dlSync.setType(type);
076                    dlSync.setName(name);
077                    dlSync.setVersion(version);
078    
079                    dlSyncPersistence.update(dlSync);
080    
081                    return dlSync;
082            }
083    
084            /**
085             * @deprecated As of 6.2.0, replaced by {@link #updateSync(long, long,
086             *             String, String, String, String)}
087             */
088            public DLSync updateSync(
089                            long fileId, long parentFolderId, String name, String event,
090                            String version)
091                    throws PortalException, SystemException {
092    
093                    return updateSync(
094                            fileId, parentFolderId, name, StringPool.BLANK, event, version);
095            }
096    
097            public DLSync updateSync(
098                            long fileId, long parentFolderId, String name, String description,
099                            String event, String version)
100                    throws PortalException, SystemException {
101    
102                    if (!isDefaultRepository(fileId, parentFolderId)) {
103                            return null;
104                    }
105    
106                    DLSync dlSync = null;
107    
108                    if (event == DLSyncConstants.EVENT_DELETE) {
109                            dlSync = dlSyncPersistence.fetchByFileId(fileId);
110    
111                            if (dlSync == null) {
112                                    return null;
113                            }
114                    }
115                    else {
116                            dlSync = dlSyncPersistence.findByFileId(fileId);
117                    }
118    
119                    dlSync.setModifiedDate(System.currentTimeMillis());
120                    dlSync.setParentFolderId(parentFolderId);
121                    dlSync.setName(name);
122                    dlSync.setDescription(description);
123                    dlSync.setEvent(event);
124                    dlSync.setVersion(version);
125    
126                    dlSyncPersistence.update(dlSync);
127    
128                    return dlSync;
129            }
130    
131            protected boolean isDefaultRepository(long fileId, long folderId)
132                    throws PortalException, SystemException {
133    
134                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
135                            return true;
136                    }
137    
138                    try {
139                            Folder folder = dlAppLocalService.getFolder(folderId);
140    
141                            return folder.isDefaultRepository();
142                    }
143                    catch (NoSuchModelException nsme) {
144                    }
145    
146                    try {
147                            Folder folder = dlAppLocalService.getFolder(fileId);
148    
149                            return folder.isDefaultRepository();
150                    }
151                    catch (NoSuchModelException nsme) {
152                    }
153    
154                    FileEntry fileEntry = dlAppLocalService.getFileEntry(fileId);
155    
156                    if (fileEntry instanceof LiferayFileEntry) {
157                            return true;
158                    }
159    
160                    return false;
161            }
162    
163    }