1
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.impl.DLFolderImpl;
34 import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
35
36 import java.util.Date;
37 import java.util.List;
38
39
46 public class DLFileShortcutLocalServiceImpl
47 extends DLFileShortcutLocalServiceBaseImpl {
48
49 public DLFileShortcut addFileShortcut(
50 long userId, long folderId, long toFolderId, String toName,
51 boolean addCommunityPermissions, boolean addGuestPermissions)
52 throws PortalException, SystemException {
53
54 return addFileShortcut(
55 null, userId, folderId, toFolderId, toName,
56 Boolean.valueOf(addCommunityPermissions),
57 Boolean.valueOf(addGuestPermissions), null, null);
58 }
59
60 public DLFileShortcut addFileShortcut(
61 String uuid, long userId, long folderId, long toFolderId,
62 String toName, boolean addCommunityPermissions,
63 boolean addGuestPermissions)
64 throws PortalException, SystemException {
65
66 return addFileShortcut(
67 uuid, userId, folderId, toFolderId, toName,
68 Boolean.valueOf(addCommunityPermissions),
69 Boolean.valueOf(addGuestPermissions), null, null);
70 }
71
72 public DLFileShortcut addFileShortcut(
73 long userId, long folderId, long toFolderId, String toName,
74 String[] communityPermissions, String[] guestPermissions)
75 throws PortalException, SystemException {
76
77 return addFileShortcut(
78 null, userId, folderId, toFolderId, toName, null, null,
79 communityPermissions, guestPermissions);
80 }
81
82 public DLFileShortcut addFileShortcut(
83 String uuid, long userId, long folderId, long toFolderId,
84 String toName, Boolean addCommunityPermissions,
85 Boolean addGuestPermissions, String[] communityPermissions,
86 String[] guestPermissions)
87 throws PortalException, SystemException {
88
89
91 User user = userPersistence.findByPrimaryKey(userId);
92 folderId = getFolderId(user.getCompanyId(), folderId);
93 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
94 Date now = new Date();
95
96 validate(user, toFolderId, toName);
97
98 long fileShortcutId = counterLocalService.increment();
99
100 DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
101 fileShortcutId);
102
103 fileShortcut.setUuid(uuid);
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
117 if ((addCommunityPermissions != null) &&
118 (addGuestPermissions != null)) {
119
120 addFileShortcutResources(
121 folder, fileShortcut, addCommunityPermissions.booleanValue(),
122 addGuestPermissions.booleanValue());
123 }
124 else {
125 addFileShortcutResources(
126 folder, fileShortcut, communityPermissions, guestPermissions);
127 }
128
129
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 DLFolder folder = fileShortcut.getFolder();
146
147 addFileShortcutResources(
148 folder, fileShortcut, addCommunityPermissions, addGuestPermissions);
149 }
150
151 public void addFileShortcutResources(
152 DLFolder folder, DLFileShortcut fileShortcut,
153 boolean addCommunityPermissions, boolean addGuestPermissions)
154 throws PortalException, SystemException {
155
156 resourceLocalService.addResources(
157 fileShortcut.getCompanyId(), folder.getGroupId(),
158 fileShortcut.getUserId(), DLFileShortcut.class.getName(),
159 fileShortcut.getFileShortcutId(), false, addCommunityPermissions,
160 addGuestPermissions);
161 }
162
163 public void addFileShortcutResources(
164 long fileShortcutId, String[] communityPermissions,
165 String[] guestPermissions)
166 throws PortalException, SystemException {
167
168 DLFileShortcut fileShortcut =
169 dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
170 DLFolder folder = fileShortcut.getFolder();
171
172 addFileShortcutResources(
173 folder, fileShortcut, communityPermissions, guestPermissions);
174 }
175
176 public void addFileShortcutResources(
177 DLFolder folder, DLFileShortcut fileShortcut,
178 String[] communityPermissions, String[] guestPermissions)
179 throws PortalException, SystemException {
180
181 resourceLocalService.addModelResources(
182 fileShortcut.getCompanyId(), folder.getGroupId(),
183 fileShortcut.getUserId(), DLFileShortcut.class.getName(),
184 fileShortcut.getFileShortcutId(), communityPermissions,
185 guestPermissions);
186 }
187
188 public void deleteFileShortcut(long fileShortcutId)
189 throws PortalException, SystemException {
190
191 dlFileShortcutPersistence.remove(fileShortcutId);
192 }
193
194 public void deleteFileShortcut(DLFileShortcut fileShortcut)
195 throws PortalException, SystemException {
196
197
199 resourceLocalService.deleteResource(
200 fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
201 ResourceConstants.SCOPE_INDIVIDUAL,
202 fileShortcut.getFileShortcutId());
203
204
206 dlFileShortcutPersistence.remove(fileShortcut);
207 }
208
209 public void deleteFileShortcuts(long toFolderId, String toName)
210 throws PortalException, SystemException {
211
212 List<DLFileShortcut> fileShortcuts =
213 dlFileShortcutPersistence.findByTF_TN(toFolderId, toName);
214
215 for (DLFileShortcut fileShortcut : fileShortcuts) {
216 deleteFileShortcut(fileShortcut);
217 }
218 }
219
220 public DLFileShortcut getFileShortcut(long fileShortcutId)
221 throws PortalException, SystemException {
222
223 return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
224 }
225
226 public DLFileShortcut updateFileShortcut(
227 long userId, long fileShortcutId, long folderId,
228 long toFolderId, String toName)
229 throws PortalException, SystemException {
230
231
233 User user = userPersistence.findByPrimaryKey(userId);
234 DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
235
236 validate(user, toFolderId, toName);
237
238 DLFileShortcut fileShortcut =
239 dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
240
241 fileShortcut.setModifiedDate(new Date());
242 fileShortcut.setFolderId(folderId);
243 fileShortcut.setToFolderId(toFolderId);
244 fileShortcut.setToName(toName);
245
246 dlFileShortcutPersistence.update(fileShortcut, false);
247
248
250 folder.setLastPostDate(fileShortcut.getModifiedDate());
251
252 dlFolderPersistence.update(folder, false);
253
254 return fileShortcut;
255 }
256
257 public void updateFileShortcuts(
258 long oldToFolderId, String oldToName, long newToFolderId,
259 String newToName)
260 throws SystemException {
261
262 List<DLFileShortcut> fileShortcuts =
263 dlFileShortcutPersistence.findByTF_TN(oldToFolderId, oldToName);
264
265 for (DLFileShortcut fileShortcut : fileShortcuts) {
266 fileShortcut.setToFolderId(newToFolderId);
267 fileShortcut.setToName(newToName);
268
269 dlFileShortcutPersistence.update(fileShortcut, false);
270 }
271 }
272
273 protected long getFolderId(long companyId, long folderId)
274 throws SystemException {
275
276 if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
277
278
280 DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
281
282 if ((folder == null) || (companyId != folder.getCompanyId())) {
283 folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
284 }
285 }
286
287 return folderId;
288 }
289
290 protected void validate(User user, long toFolderId, String toName)
291 throws PortalException, SystemException {
292
293 DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
294 toFolderId, toName);
295
296 if (user.getCompanyId() != fileEntry.getCompanyId()) {
297 throw new NoSuchFileEntryException();
298 }
299 }
300
301 }