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