1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
31  import com.liferay.portlet.documentlibrary.model.DLFolder;
32  import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
33  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
34  
35  import java.io.File;
36  import java.io.InputStream;
37  
38  import java.rmi.RemoteException;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="DLFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
50  
51      public DLFolder addFolder(
52              long plid, long parentFolderId, String name, String description,
53              boolean addCommunityPermissions, boolean addGuestPermissions)
54          throws PortalException, SystemException {
55  
56          DLFolderPermission.check(
57              getPermissionChecker(), plid, parentFolderId,
58              ActionKeys.ADD_FOLDER);
59  
60          return dlFolderLocalService.addFolder(
61              getUserId(), plid, parentFolderId, name, description,
62              addCommunityPermissions, addGuestPermissions);
63      }
64  
65      public DLFolder addFolder(
66              long plid, long parentFolderId, String name, String description,
67              String[] communityPermissions, String[] guestPermissions)
68          throws PortalException, SystemException {
69  
70          DLFolderPermission.check(
71              getPermissionChecker(), plid, parentFolderId,
72              ActionKeys.ADD_FOLDER);
73  
74          return dlFolderLocalService.addFolder(
75              getUserId(), plid, parentFolderId, name, description,
76              communityPermissions, guestPermissions);
77      }
78  
79      public DLFolder copyFolder(
80              long plid, long sourceFolderId, long parentFolderId, String name,
81              String description, boolean addCommunityPermissions,
82              boolean addGuestPermissions)
83          throws PortalException, RemoteException, SystemException {
84  
85          DLFolder srcFolder = getFolder(sourceFolderId);
86  
87          DLFolder destFolder = addFolder(
88              plid, parentFolderId, name, description, addCommunityPermissions,
89              addGuestPermissions);
90  
91          copyFolder(
92              srcFolder, destFolder, addCommunityPermissions,
93              addGuestPermissions);
94  
95          return destFolder;
96      }
97  
98      public void deleteFolder(long folderId)
99          throws PortalException, SystemException {
100 
101         DLFolderPermission.check(
102             getPermissionChecker(), folderId, ActionKeys.DELETE);
103 
104         dlFolderLocalService.deleteFolder(folderId);
105     }
106 
107     public void deleteFolder(long groupId, long parentFolderId, String name)
108         throws PortalException, SystemException {
109 
110         DLFolder folder = getFolder(groupId, parentFolderId, name);
111 
112         deleteFolder(folder.getFolderId());
113     }
114 
115     public DLFolder getFolder(long folderId)
116         throws PortalException, SystemException {
117 
118         DLFolderPermission.check(
119             getPermissionChecker(), folderId, ActionKeys.VIEW);
120 
121         return dlFolderLocalService.getFolder(folderId);
122     }
123 
124     public DLFolder getFolder(long groupId, long parentFolderId, String name)
125         throws PortalException, SystemException {
126 
127         DLFolder folder = dlFolderLocalService.getFolder(
128             groupId, parentFolderId, name);
129 
130         DLFolderPermission.check(
131             getPermissionChecker(), folder, ActionKeys.VIEW);
132 
133         return folder;
134     }
135 
136     public long getFolderId(long groupId, long parentFolderId, String name)
137         throws PortalException, SystemException {
138 
139         DLFolder folder = getFolder(groupId, parentFolderId, name);
140 
141         return folder.getFolderId();
142     }
143 
144     public List<DLFolder> getFolders(long groupId, long parentFolderId)
145         throws PortalException, SystemException {
146 
147         List<DLFolder> folders = dlFolderLocalService.getFolders(
148             groupId, parentFolderId);
149 
150         folders = ListUtil.copy(folders);
151 
152         Iterator<DLFolder> itr = folders.iterator();
153 
154         while (itr.hasNext()) {
155             DLFolder folder = itr.next();
156 
157             if (!DLFolderPermission.contains(
158                     getPermissionChecker(), folder.getFolderId(),
159                     ActionKeys.VIEW)) {
160 
161                 itr.remove();
162             }
163         }
164 
165         return folders;
166     }
167 
168     public void reIndexSearch(long companyId)
169         throws PortalException, SystemException {
170 
171         if (!getPermissionChecker().isOmniadmin()) {
172             throw new PrincipalException();
173         }
174 
175         String[] ids = new String[] {String.valueOf(companyId)};
176 
177         dlFolderLocalService.reIndex(ids);
178     }
179 
180     public DLFolder updateFolder(
181             long folderId, long parentFolderId, String name, String description)
182         throws PortalException, SystemException {
183 
184         DLFolderPermission.check(
185             getPermissionChecker(), folderId, ActionKeys.UPDATE);
186 
187         return dlFolderLocalService.updateFolder(
188             folderId, parentFolderId, name, description);
189     }
190 
191     protected void copyFolder(
192             DLFolder srcFolder, DLFolder destFolder,
193             boolean addCommunityPermissions, boolean addGuestPermissions)
194         throws PortalException, RemoteException, SystemException {
195 
196         List<DLFileEntry> srcFileEntries = dlFileEntryService.getFileEntries(
197             srcFolder.getFolderId());
198 
199         for (DLFileEntry srcFileEntry : srcFileEntries) {
200             String name = srcFileEntry.getName();
201             String title = srcFileEntry.getTitleWithExtension();
202             String description = srcFileEntry.getDescription();
203             String[] tagsEntries = null;
204             String extraSettings = srcFileEntry.getExtraSettings();
205 
206             File file = null;
207 
208             try {
209                 file = FileUtil.createTempFile(FileUtil.getExtension(name));
210 
211                 InputStream is = dlLocalService.getFileAsStream(
212                     srcFolder.getCompanyId(), srcFolder.getFolderId(), name);
213 
214                 FileUtil.write(file, is);
215             }
216             catch (Exception e) {
217                 _log.error(e, e);
218 
219                 continue;
220             }
221 
222             dlFileEntryService.addFileEntry(
223                 destFolder.getFolderId(), name, title, description, tagsEntries,
224                 extraSettings, file, addCommunityPermissions,
225                 addGuestPermissions);
226 
227             file.delete();
228         }
229 
230         long destPlid = layoutLocalService.getDefaultPlid(
231             destFolder.getGroupId());
232 
233         List<DLFolder> srcSubfolders = getFolders(
234             srcFolder.getGroupId(), srcFolder.getFolderId());
235 
236         for (DLFolder srcSubfolder : srcSubfolders) {
237             String name = srcSubfolder.getName();
238             String description = srcSubfolder.getDescription();
239 
240             DLFolder destSubfolder = addFolder(
241                 destPlid, destFolder.getFolderId(), name,
242                 description, addCommunityPermissions, addGuestPermissions);
243 
244             copyFolder(
245                 srcSubfolder, destSubfolder, addCommunityPermissions,
246                 addGuestPermissions);
247         }
248     }
249 
250     private static Log _log = LogFactoryUtil.getLog(DLFolderServiceImpl.class);
251 
252 }