001
014
015 package com.liferay.portlet.asset.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.ArrayUtil;
020 import com.liferay.portal.model.User;
021 import com.liferay.portlet.asset.NoSuchLinkException;
022 import com.liferay.portlet.asset.model.AssetEntry;
023 import com.liferay.portlet.asset.model.AssetLink;
024 import com.liferay.portlet.asset.model.AssetLinkConstants;
025 import com.liferay.portlet.asset.service.base.AssetLinkLocalServiceBaseImpl;
026
027 import java.util.ArrayList;
028 import java.util.Collections;
029 import java.util.Date;
030 import java.util.List;
031
032
042 public class AssetLinkLocalServiceImpl extends AssetLinkLocalServiceBaseImpl {
043
044
062 @Override
063 public AssetLink addLink(
064 long userId, long entryId1, long entryId2, int type, int weight)
065 throws PortalException, SystemException {
066
067 User user = userPersistence.findByPrimaryKey(userId);
068 Date now = new Date();
069
070 long linkId = counterLocalService.increment();
071
072 AssetLink link = assetLinkPersistence.create(linkId);
073
074 link.setCompanyId(user.getCompanyId());
075 link.setUserId(user.getUserId());
076 link.setUserName(user.getFullName());
077 link.setCreateDate(now);
078 link.setEntryId1(entryId1);
079 link.setEntryId2(entryId2);
080 link.setType(type);
081 link.setWeight(weight);
082
083 assetLinkPersistence.update(link);
084
085 if (AssetLinkConstants.isTypeBi(type)) {
086 long linkId2 = counterLocalService.increment();
087
088 AssetLink link2 = assetLinkPersistence.create(linkId2);
089
090 link2.setCompanyId(user.getCompanyId());
091 link2.setUserId(user.getUserId());
092 link2.setUserName(user.getFullName());
093 link2.setCreateDate(now);
094 link2.setEntryId1(entryId2);
095 link2.setEntryId2(entryId1);
096 link2.setType(type);
097 link2.setWeight(weight);
098
099 assetLinkPersistence.update(link2);
100 }
101
102 return link;
103 }
104
105
111 @Override
112 public void deleteLink(AssetLink link) throws SystemException {
113 if (AssetLinkConstants.isTypeBi(link.getType())) {
114 try {
115 assetLinkPersistence.removeByE_E_T(
116 link.getEntryId2(), link.getEntryId1(), link.getType());
117 }
118 catch (NoSuchLinkException nsle) {
119 }
120 }
121
122 assetLinkPersistence.remove(link);
123 }
124
125
132 @Override
133 public void deleteLink(long linkId)
134 throws PortalException, SystemException {
135
136 AssetLink link = assetLinkPersistence.findByPrimaryKey(linkId);
137
138 deleteLink(link);
139 }
140
141
147 @Override
148 public void deleteLinks(long entryId) throws SystemException {
149 for (AssetLink link : assetLinkPersistence.findByE1(entryId)) {
150 deleteLink(link);
151 }
152
153 for (AssetLink link : assetLinkPersistence.findByE2(entryId)) {
154 deleteLink(link);
155 }
156 }
157
158
165 @Override
166 public void deleteLinks(long entryId1, long entryId2)
167 throws SystemException {
168
169 List<AssetLink> links = assetLinkPersistence.findByE_E(
170 entryId1, entryId2);
171
172 for (AssetLink link : links) {
173 deleteLink(link);
174 }
175 }
176
177
184 @Override
185 public List<AssetLink> getDirectLinks(long entryId) throws SystemException {
186 return getDirectLinks(entryId, true);
187 }
188
189 @Override
190 public List<AssetLink> getDirectLinks(
191 long entryId, boolean excludeInvisibleLinks)
192 throws SystemException {
193
194 List<AssetLink> assetLinks = assetLinkPersistence.findByE1(entryId);
195
196 return filterAssetLinks(assetLinks, excludeInvisibleLinks);
197 }
198
199
214 @Override
215 public List<AssetLink> getDirectLinks(long entryId, int typeId)
216 throws SystemException {
217
218 return getDirectLinks(entryId, typeId, true);
219 }
220
221 @Override
222 public List<AssetLink> getDirectLinks(
223 long entryId, int typeId, boolean excludeInvisibleLinks)
224 throws SystemException {
225
226 List<AssetLink> assetLinks = assetLinkPersistence.findByE1_T(
227 entryId, typeId);
228
229 return filterAssetLinks(assetLinks, excludeInvisibleLinks);
230 }
231
232
241 @Override
242 public List<AssetLink> getLinks(long entryId) throws SystemException {
243 List<AssetLink> e1Links = assetLinkPersistence.findByE1(entryId);
244 List<AssetLink> e2Links = assetLinkPersistence.findByE2(entryId);
245
246 List<AssetLink> links = new ArrayList<AssetLink>(
247 e1Links.size() + e2Links.size());
248
249 links.addAll(e1Links);
250 links.addAll(e2Links);
251
252 return links;
253 }
254
255
270 @Override
271 public List<AssetLink> getLinks(long entryId, int typeId)
272 throws SystemException {
273
274 List<AssetLink> e1Links = assetLinkPersistence.findByE1_T(
275 entryId, typeId);
276 List<AssetLink> e2Links = assetLinkPersistence.findByE2_T(
277 entryId, typeId);
278
279 List<AssetLink> links = new ArrayList<AssetLink>(
280 e1Links.size() + e2Links.size());
281
282 links.addAll(e1Links);
283 links.addAll(e2Links);
284
285 return links;
286 }
287
288
303 @Override
304 public List<AssetLink> getReverseLinks(long entryId, int typeId)
305 throws SystemException {
306
307 return assetLinkPersistence.findByE2_T(entryId, typeId);
308 }
309
310 @Override
311 public AssetLink updateLink(
312 long userId, long entryId1, long entryId2, int typeId, int weight)
313 throws PortalException, SystemException {
314
315 AssetLink assetLink = assetLinkPersistence.fetchByE_E_T(
316 entryId1, entryId2, typeId);
317
318 if (assetLink == null) {
319 return addLink(userId, entryId1, entryId2, typeId, weight);
320 }
321
322 assetLink.setWeight(weight);
323
324 assetLinkPersistence.update(assetLink);
325
326 return assetLink;
327 }
328
329
355 @Override
356 public void updateLinks(
357 long userId, long entryId, long[] linkEntryIds, int typeId)
358 throws PortalException, SystemException {
359
360 if (linkEntryIds == null) {
361 return;
362 }
363
364 List<AssetLink> links = getLinks(entryId, typeId);
365
366 for (AssetLink link : links) {
367 if (((link.getEntryId1() == entryId) &&
368 !ArrayUtil.contains(linkEntryIds, link.getEntryId2())) ||
369 ((link.getEntryId2() == entryId) &&
370 !ArrayUtil.contains(linkEntryIds, link.getEntryId1()))) {
371
372 deleteLink(link);
373 }
374 }
375
376 for (long assetLinkEntryId : linkEntryIds) {
377 if (assetLinkEntryId != entryId) {
378 AssetLink link = assetLinkPersistence.fetchByE_E_T(
379 entryId, assetLinkEntryId, typeId);
380
381 if (link == null) {
382 addLink(userId, entryId, assetLinkEntryId, typeId, 0);
383 }
384 }
385 }
386 }
387
388 protected List<AssetLink> filterAssetLinks(
389 List<AssetLink> assetLinks, boolean excludeInvisibleLinks)
390 throws SystemException {
391
392 if (assetLinks.isEmpty() || !excludeInvisibleLinks) {
393 return assetLinks;
394 }
395
396 List<AssetLink> filteredAssetLinks = new ArrayList<AssetLink>(
397 assetLinks.size());
398
399 for (AssetLink assetLink : assetLinks) {
400 AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
401 assetLink.getEntryId2());
402
403 if ((assetEntry != null) && assetEntry.isVisible()) {
404 filteredAssetLinks.add(assetLink);
405 }
406 }
407
408 assetLinks = Collections.unmodifiableList(filteredAssetLinks);
409
410 return assetLinks;
411 }
412
413 }