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.model.ResourceConstants;
28  import com.liferay.portal.model.User;
29  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
30  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
31  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
32  import com.liferay.portlet.documentlibrary.model.DLFolder;
33  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
34  import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
35  
36  import java.util.Date;
37  import java.util.List;
38  
39  /**
40   * <a href="DLFileShortcutLocalServiceImpl.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class DLFileShortcutLocalServiceImpl
46      extends DLFileShortcutLocalServiceBaseImpl {
47  
48      public DLFileShortcut addFileShortcut(
49              long userId, long folderId, long toFolderId, String toName,
50              boolean addCommunityPermissions, boolean addGuestPermissions)
51          throws PortalException, SystemException {
52  
53          return addFileShortcut(
54              null, userId, folderId, toFolderId, toName,
55              Boolean.valueOf(addCommunityPermissions),
56              Boolean.valueOf(addGuestPermissions), null, null);
57      }
58  
59      public DLFileShortcut addFileShortcut(
60              String uuid, long userId, long folderId, long toFolderId,
61              String toName, boolean addCommunityPermissions,
62              boolean addGuestPermissions)
63          throws PortalException, SystemException {
64  
65          return addFileShortcut(
66              uuid, userId, folderId, toFolderId, toName,
67              Boolean.valueOf(addCommunityPermissions),
68              Boolean.valueOf(addGuestPermissions), null, null);
69      }
70  
71      public DLFileShortcut addFileShortcut(
72              long userId, long folderId, long toFolderId, String toName,
73              String[] communityPermissions, String[] guestPermissions)
74          throws PortalException, SystemException {
75  
76          return addFileShortcut(
77              null, userId, folderId, toFolderId, toName, null, null,
78              communityPermissions, guestPermissions);
79      }
80  
81      public DLFileShortcut addFileShortcut(
82              String uuid, long userId, long folderId, long toFolderId,
83              String toName, Boolean addCommunityPermissions,
84              Boolean addGuestPermissions, String[] communityPermissions,
85              String[] guestPermissions)
86          throws PortalException, SystemException {
87  
88          // File shortcut
89  
90          User user = userPersistence.findByPrimaryKey(userId);
91          folderId = getFolderId(user.getCompanyId(), folderId);
92          DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
93          Date now = new Date();
94  
95          validate(user, toFolderId, toName);
96  
97          long fileShortcutId = counterLocalService.increment();
98  
99          DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
100             fileShortcutId);
101 
102         fileShortcut.setUuid(uuid);
103         fileShortcut.setGroupId(folder.getGroupId());
104         fileShortcut.setCompanyId(user.getCompanyId());
105         fileShortcut.setUserId(user.getUserId());
106         fileShortcut.setUserName(user.getFullName());
107         fileShortcut.setCreateDate(now);
108         fileShortcut.setModifiedDate(now);
109         fileShortcut.setFolderId(folderId);
110         fileShortcut.setToFolderId(toFolderId);
111         fileShortcut.setToName(toName);
112 
113         dlFileShortcutPersistence.update(fileShortcut, false);
114 
115         // Resources
116 
117         if ((addCommunityPermissions != null) &&
118             (addGuestPermissions != null)) {
119 
120             addFileShortcutResources(
121                 fileShortcut, addCommunityPermissions.booleanValue(),
122                 addGuestPermissions.booleanValue());
123         }
124         else {
125             addFileShortcutResources(
126                 fileShortcut, communityPermissions, guestPermissions);
127         }
128 
129         // Folder
130 
131         folder.setLastPostDate(fileShortcut.getModifiedDate());
132 
133         dlFolderPersistence.update(folder, false);
134 
135         return fileShortcut;
136     }
137 
138     public void addFileShortcutResources(
139             long fileShortcutId, boolean addCommunityPermissions,
140             boolean addGuestPermissions)
141         throws PortalException, SystemException {
142 
143         DLFileShortcut fileShortcut =
144             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
145 
146         addFileShortcutResources(
147             fileShortcut, addCommunityPermissions, addGuestPermissions);
148     }
149 
150     public void addFileShortcutResources(
151             DLFileShortcut fileShortcut, boolean addCommunityPermissions,
152             boolean addGuestPermissions)
153         throws PortalException, SystemException {
154 
155         resourceLocalService.addResources(
156             fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
157             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
158             fileShortcut.getFileShortcutId(), false, addCommunityPermissions,
159             addGuestPermissions);
160     }
161 
162     public void addFileShortcutResources(
163             long fileShortcutId, String[] communityPermissions,
164             String[] guestPermissions)
165         throws PortalException, SystemException {
166 
167         DLFileShortcut fileShortcut =
168             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
169 
170         addFileShortcutResources(
171             fileShortcut, communityPermissions, guestPermissions);
172     }
173 
174     public void addFileShortcutResources(
175             DLFileShortcut fileShortcut, String[] communityPermissions,
176             String[] guestPermissions)
177         throws PortalException, SystemException {
178 
179         resourceLocalService.addModelResources(
180             fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
181             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
182             fileShortcut.getFileShortcutId(), communityPermissions,
183             guestPermissions);
184     }
185 
186     public void deleteFileShortcut(long fileShortcutId)
187         throws PortalException, SystemException {
188 
189         dlFileShortcutPersistence.remove(fileShortcutId);
190     }
191 
192     public void deleteFileShortcut(DLFileShortcut fileShortcut)
193         throws PortalException, SystemException {
194 
195         // Resources
196 
197         resourceLocalService.deleteResource(
198             fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
199             ResourceConstants.SCOPE_INDIVIDUAL,
200             fileShortcut.getFileShortcutId());
201 
202         // File shortcut
203 
204         dlFileShortcutPersistence.remove(fileShortcut);
205     }
206 
207     public void deleteFileShortcuts(long toFolderId, String toName)
208         throws PortalException, SystemException {
209 
210         List<DLFileShortcut> fileShortcuts =
211             dlFileShortcutPersistence.findByTF_TN(toFolderId, toName);
212 
213         for (DLFileShortcut fileShortcut : fileShortcuts) {
214             deleteFileShortcut(fileShortcut);
215         }
216     }
217 
218     public DLFileShortcut getFileShortcut(long fileShortcutId)
219         throws PortalException, SystemException {
220 
221         return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
222     }
223 
224     public DLFileShortcut updateFileShortcut(
225             long userId, long fileShortcutId, long folderId,
226             long toFolderId, String toName)
227         throws PortalException, SystemException {
228 
229         // File shortcut
230 
231         User user = userPersistence.findByPrimaryKey(userId);
232         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
233 
234         validate(user, toFolderId, toName);
235 
236         DLFileShortcut fileShortcut =
237             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
238 
239         fileShortcut.setModifiedDate(new Date());
240         fileShortcut.setFolderId(folderId);
241         fileShortcut.setToFolderId(toFolderId);
242         fileShortcut.setToName(toName);
243 
244         dlFileShortcutPersistence.update(fileShortcut, false);
245 
246         // Folder
247 
248         folder.setLastPostDate(fileShortcut.getModifiedDate());
249 
250         dlFolderPersistence.update(folder, false);
251 
252         return fileShortcut;
253     }
254 
255     public void updateFileShortcuts(
256             long oldToFolderId, String oldToName, long newToFolderId,
257             String newToName)
258         throws SystemException {
259 
260         List<DLFileShortcut> fileShortcuts =
261             dlFileShortcutPersistence.findByTF_TN(oldToFolderId, oldToName);
262 
263         for (DLFileShortcut fileShortcut : fileShortcuts) {
264             fileShortcut.setToFolderId(newToFolderId);
265             fileShortcut.setToName(newToName);
266 
267             dlFileShortcutPersistence.update(fileShortcut, false);
268         }
269     }
270 
271     protected long getFolderId(long companyId, long folderId)
272         throws SystemException {
273 
274         if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
275 
276             // Ensure folder exists and belongs to the proper company
277 
278             DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
279 
280             if ((folder == null) || (companyId != folder.getCompanyId())) {
281                 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
282             }
283         }
284 
285         return folderId;
286     }
287 
288     protected void validate(User user, long toFolderId, String toName)
289         throws PortalException, SystemException {
290 
291         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
292             toFolderId, toName);
293 
294         if (user.getCompanyId() != fileEntry.getCompanyId()) {
295             throw new NoSuchFileEntryException();
296         }
297     }
298 
299 }