1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.ListUtil;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.security.permission.ActionKeys;
25  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
26  import com.liferay.portlet.documentlibrary.model.DLFolder;
27  import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
28  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
29  
30  import java.io.File;
31  import java.io.InputStream;
32  
33  import java.rmi.RemoteException;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="DLFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
44  
45      public DLFolder addFolder(
46              long plid, long parentFolderId, String name, String description,
47              boolean addCommunityPermissions, boolean addGuestPermissions)
48          throws PortalException, SystemException {
49  
50          DLFolderPermission.check(
51              getPermissionChecker(), plid, parentFolderId,
52              ActionKeys.ADD_FOLDER);
53  
54          return dlFolderLocalService.addFolder(
55              getUserId(), plid, parentFolderId, name, description,
56              addCommunityPermissions, addGuestPermissions);
57      }
58  
59      public DLFolder addFolder(
60              long plid, long parentFolderId, String name, String description,
61              String[] communityPermissions, String[] guestPermissions)
62          throws PortalException, SystemException {
63  
64          DLFolderPermission.check(
65              getPermissionChecker(), plid, parentFolderId,
66              ActionKeys.ADD_FOLDER);
67  
68          return dlFolderLocalService.addFolder(
69              getUserId(), plid, parentFolderId, name, description,
70              communityPermissions, guestPermissions);
71      }
72  
73      public DLFolder copyFolder(
74              long plid, long sourceFolderId, long parentFolderId, String name,
75              String description, boolean addCommunityPermissions,
76              boolean addGuestPermissions)
77          throws PortalException, RemoteException, SystemException {
78  
79          DLFolder srcFolder = getFolder(sourceFolderId);
80  
81          DLFolder destFolder = addFolder(
82              plid, parentFolderId, name, description, addCommunityPermissions,
83              addGuestPermissions);
84  
85          copyFolder(
86              srcFolder, destFolder, addCommunityPermissions,
87              addGuestPermissions);
88  
89          return destFolder;
90      }
91  
92      public void deleteFolder(long folderId)
93          throws PortalException, SystemException {
94  
95          DLFolderPermission.check(
96              getPermissionChecker(), folderId, ActionKeys.DELETE);
97  
98          dlFolderLocalService.deleteFolder(folderId);
99      }
100 
101     public void deleteFolder(long groupId, long parentFolderId, String name)
102         throws PortalException, SystemException {
103 
104         DLFolder folder = getFolder(groupId, parentFolderId, name);
105 
106         deleteFolder(folder.getFolderId());
107     }
108 
109     public DLFolder getFolder(long folderId)
110         throws PortalException, SystemException {
111 
112         DLFolderPermission.check(
113             getPermissionChecker(), folderId, ActionKeys.VIEW);
114 
115         return dlFolderLocalService.getFolder(folderId);
116     }
117 
118     public DLFolder getFolder(long groupId, long parentFolderId, String name)
119         throws PortalException, SystemException {
120 
121         DLFolder folder = dlFolderLocalService.getFolder(
122             groupId, parentFolderId, name);
123 
124         DLFolderPermission.check(
125             getPermissionChecker(), folder, ActionKeys.VIEW);
126 
127         return folder;
128     }
129 
130     public long getFolderId(long groupId, long parentFolderId, String name)
131         throws PortalException, SystemException {
132 
133         DLFolder folder = getFolder(groupId, parentFolderId, name);
134 
135         return folder.getFolderId();
136     }
137 
138     public List<DLFolder> getFolders(long groupId, long parentFolderId)
139         throws PortalException, SystemException {
140 
141         List<DLFolder> folders = dlFolderLocalService.getFolders(
142             groupId, parentFolderId);
143 
144         folders = ListUtil.copy(folders);
145 
146         Iterator<DLFolder> itr = folders.iterator();
147 
148         while (itr.hasNext()) {
149             DLFolder folder = itr.next();
150 
151             if (!DLFolderPermission.contains(
152                     getPermissionChecker(), folder.getFolderId(),
153                     ActionKeys.VIEW)) {
154 
155                 itr.remove();
156             }
157         }
158 
159         return folders;
160     }
161 
162     public void reIndexSearch(long companyId)
163         throws PortalException, SystemException {
164 
165         if (!getPermissionChecker().isOmniadmin()) {
166             throw new PrincipalException();
167         }
168 
169         String[] ids = new String[] {String.valueOf(companyId)};
170 
171         dlFolderLocalService.reIndex(ids);
172     }
173 
174     public DLFolder updateFolder(
175             long folderId, long parentFolderId, String name, String description)
176         throws PortalException, SystemException {
177 
178         DLFolderPermission.check(
179             getPermissionChecker(), folderId, ActionKeys.UPDATE);
180 
181         return dlFolderLocalService.updateFolder(
182             folderId, parentFolderId, name, description);
183     }
184 
185     protected void copyFolder(
186             DLFolder srcFolder, DLFolder destFolder,
187             boolean addCommunityPermissions, boolean addGuestPermissions)
188         throws PortalException, RemoteException, SystemException {
189 
190         List<DLFileEntry> srcFileEntries = dlFileEntryService.getFileEntries(
191             srcFolder.getFolderId());
192 
193         for (DLFileEntry srcFileEntry : srcFileEntries) {
194             String name = srcFileEntry.getName();
195             String title = srcFileEntry.getTitleWithExtension();
196             String description = srcFileEntry.getDescription();
197             String[] tagsEntries = null;
198             String extraSettings = srcFileEntry.getExtraSettings();
199 
200             File file = null;
201 
202             try {
203                 file = FileUtil.createTempFile(FileUtil.getExtension(name));
204 
205                 InputStream is = dlLocalService.getFileAsStream(
206                     srcFolder.getCompanyId(), srcFolder.getFolderId(), name);
207 
208                 FileUtil.write(file, is);
209             }
210             catch (Exception e) {
211                 _log.error(e, e);
212 
213                 continue;
214             }
215 
216             dlFileEntryService.addFileEntry(
217                 destFolder.getFolderId(), name, title, description, tagsEntries,
218                 extraSettings, file, addCommunityPermissions,
219                 addGuestPermissions);
220 
221             file.delete();
222         }
223 
224         long destPlid = layoutLocalService.getDefaultPlid(
225             destFolder.getGroupId());
226 
227         List<DLFolder> srcSubfolders = getFolders(
228             srcFolder.getGroupId(), srcFolder.getFolderId());
229 
230         for (DLFolder srcSubfolder : srcSubfolders) {
231             String name = srcSubfolder.getName();
232             String description = srcSubfolder.getDescription();
233 
234             DLFolder destSubfolder = addFolder(
235                 destPlid, destFolder.getFolderId(), name,
236                 description, addCommunityPermissions, addGuestPermissions);
237 
238             copyFolder(
239                 srcSubfolder, destSubfolder, addCommunityPermissions,
240                 addGuestPermissions);
241         }
242     }
243 
244     private static Log _log = LogFactoryUtil.getLog(DLFolderServiceImpl.class);
245 
246 }