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.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.ArrayUtil;
021 import com.liferay.portal.model.User;
022 import com.liferay.portlet.asset.NoSuchLinkException;
023 import com.liferay.portlet.asset.model.AssetEntry;
024 import com.liferay.portlet.asset.model.AssetLink;
025 import com.liferay.portlet.asset.model.AssetLinkConstants;
026 import com.liferay.portlet.asset.service.base.AssetLinkLocalServiceBaseImpl;
027
028 import java.util.ArrayList;
029 import java.util.Collections;
030 import java.util.Date;
031 import java.util.List;
032
033
043 public class AssetLinkLocalServiceImpl extends AssetLinkLocalServiceBaseImpl {
044
045
062 @Override
063 public AssetLink addLink(
064 long userId, long entryId1, long entryId2, int type, int weight)
065 throws PortalException {
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
110 @Override
111 public void deleteLink(AssetLink link) {
112 if (AssetLinkConstants.isTypeBi(link.getType())) {
113 try {
114 assetLinkPersistence.removeByE_E_T(
115 link.getEntryId2(), link.getEntryId1(), link.getType());
116 }
117 catch (NoSuchLinkException nsle) {
118 if (_log.isWarnEnabled()) {
119 _log.warn("Unable to delete asset link", nsle);
120 }
121 }
122 }
123
124 assetLinkPersistence.remove(link);
125 }
126
127
133 @Override
134 public void deleteLink(long linkId) throws PortalException {
135 AssetLink link = assetLinkPersistence.findByPrimaryKey(linkId);
136
137 deleteLink(link);
138 }
139
140
145 @Override
146 public void deleteLinks(long entryId) {
147 for (AssetLink link : assetLinkPersistence.findByE1(entryId)) {
148 deleteLink(link);
149 }
150
151 for (AssetLink link : assetLinkPersistence.findByE2(entryId)) {
152 deleteLink(link);
153 }
154 }
155
156
162 @Override
163 public void deleteLinks(long entryId1, long entryId2) {
164 List<AssetLink> links = assetLinkPersistence.findByE_E(
165 entryId1, entryId2);
166
167 for (AssetLink link : links) {
168 deleteLink(link);
169 }
170 }
171
172
178 @Override
179 public List<AssetLink> getDirectLinks(long entryId) {
180 List<AssetLink> assetLinks = assetLinkPersistence.findByE1(entryId);
181
182 if (!assetLinks.isEmpty()) {
183 List<AssetLink> filteredAssetLinks = new ArrayList<>(
184 assetLinks.size());
185
186 for (AssetLink assetLink : assetLinks) {
187 AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
188 assetLink.getEntryId2());
189
190 if ((assetEntry != null) && assetEntry.isVisible()) {
191 filteredAssetLinks.add(assetLink);
192 }
193 }
194
195 assetLinks = Collections.unmodifiableList(filteredAssetLinks);
196 }
197
198 return assetLinks;
199 }
200
201
215 @Override
216 public List<AssetLink> getDirectLinks(long entryId, int typeId) {
217 List<AssetLink> assetLinks = assetLinkPersistence.findByE1_T(
218 entryId, typeId);
219
220 if (!assetLinks.isEmpty()) {
221 List<AssetLink> filteredAssetLinks = new ArrayList<>(
222 assetLinks.size());
223
224 for (AssetLink assetLink : assetLinks) {
225 AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
226 assetLink.getEntryId2());
227
228 if ((assetEntry != null) && assetEntry.isVisible()) {
229 filteredAssetLinks.add(assetLink);
230 }
231 }
232
233 assetLinks = Collections.unmodifiableList(filteredAssetLinks);
234 }
235
236 return assetLinks;
237 }
238
239
247 @Override
248 public List<AssetLink> getLinks(long entryId) {
249 List<AssetLink> e1Links = assetLinkPersistence.findByE1(entryId);
250 List<AssetLink> e2Links = assetLinkPersistence.findByE2(entryId);
251
252 List<AssetLink> links = new ArrayList<>(
253 e1Links.size() + e2Links.size());
254
255 links.addAll(e1Links);
256 links.addAll(e2Links);
257
258 return links;
259 }
260
261
275 @Override
276 public List<AssetLink> getLinks(long entryId, int typeId) {
277 List<AssetLink> e1Links = assetLinkPersistence.findByE1_T(
278 entryId, typeId);
279 List<AssetLink> e2Links = assetLinkPersistence.findByE2_T(
280 entryId, typeId);
281
282 List<AssetLink> links = new ArrayList<>(
283 e1Links.size() + e2Links.size());
284
285 links.addAll(e1Links);
286 links.addAll(e2Links);
287
288 return links;
289 }
290
291
305 @Override
306 public List<AssetLink> getReverseLinks(long entryId, int typeId) {
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 {
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
354 @Override
355 public void updateLinks(
356 long userId, long entryId, long[] linkEntryIds, int typeId)
357 throws PortalException {
358
359 if (linkEntryIds == null) {
360 return;
361 }
362
363 List<AssetLink> links = getLinks(entryId, typeId);
364
365 for (AssetLink link : links) {
366 if (((link.getEntryId1() == entryId) &&
367 !ArrayUtil.contains(linkEntryIds, link.getEntryId2())) ||
368 ((link.getEntryId2() == entryId) &&
369 !ArrayUtil.contains(linkEntryIds, link.getEntryId1()))) {
370
371 deleteLink(link);
372 }
373 }
374
375 for (long assetLinkEntryId : linkEntryIds) {
376 if (assetLinkEntryId != entryId) {
377 AssetLink link = assetLinkPersistence.fetchByE_E_T(
378 entryId, assetLinkEntryId, typeId);
379
380 if (link == null) {
381 addLink(userId, entryId, assetLinkEntryId, typeId, 0);
382 }
383 }
384 }
385 }
386
387 private static final Log _log = LogFactoryUtil.getLog(
388 AssetLinkLocalServiceImpl.class);
389
390 }