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