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 List<AssetLink> assetLinks = assetLinkPersistence.findByE1(entryId);
187
188 if (!assetLinks.isEmpty()) {
189 List<AssetLink> filteredAssetLinks = new ArrayList<AssetLink>(
190 assetLinks.size());
191
192 for (AssetLink assetLink : assetLinks) {
193 AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
194 assetLink.getEntryId2());
195
196 if ((assetEntry != null) && assetEntry.isVisible()) {
197 filteredAssetLinks.add(assetLink);
198 }
199 }
200
201 assetLinks = Collections.unmodifiableList(filteredAssetLinks);
202 }
203
204 return assetLinks;
205 }
206
207
222 @Override
223 public List<AssetLink> getDirectLinks(long entryId, int typeId)
224 throws SystemException {
225
226 List<AssetLink> assetLinks = assetLinkPersistence.findByE1_T(
227 entryId, typeId);
228
229 if (!assetLinks.isEmpty()) {
230 List<AssetLink> filteredAssetLinks = new ArrayList<AssetLink>(
231 assetLinks.size());
232
233 for (AssetLink assetLink : assetLinks) {
234 AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
235 assetLink.getEntryId2());
236
237 if ((assetEntry != null) && assetEntry.isVisible()) {
238 filteredAssetLinks.add(assetLink);
239 }
240 }
241
242 assetLinks = Collections.unmodifiableList(filteredAssetLinks);
243 }
244
245 return assetLinks;
246 }
247
248
257 @Override
258 public List<AssetLink> getLinks(long entryId) throws SystemException {
259 List<AssetLink> e1Links = assetLinkPersistence.findByE1(entryId);
260 List<AssetLink> e2Links = assetLinkPersistence.findByE2(entryId);
261
262 List<AssetLink> links = new ArrayList<AssetLink>(
263 e1Links.size() + e2Links.size());
264
265 links.addAll(e1Links);
266 links.addAll(e2Links);
267
268 return links;
269 }
270
271
286 @Override
287 public List<AssetLink> getLinks(long entryId, int typeId)
288 throws SystemException {
289
290 List<AssetLink> e1Links = assetLinkPersistence.findByE1_T(
291 entryId, typeId);
292 List<AssetLink> e2Links = assetLinkPersistence.findByE2_T(
293 entryId, typeId);
294
295 List<AssetLink> links = new ArrayList<AssetLink>(
296 e1Links.size() + e2Links.size());
297
298 links.addAll(e1Links);
299 links.addAll(e2Links);
300
301 return links;
302 }
303
304
319 @Override
320 public List<AssetLink> getReverseLinks(long entryId, int typeId)
321 throws SystemException {
322
323 return assetLinkPersistence.findByE2_T(entryId, typeId);
324 }
325
326 @Override
327 public AssetLink updateLink(
328 long userId, long entryId1, long entryId2, int typeId, int weight)
329 throws PortalException, SystemException {
330
331 AssetLink assetLink = assetLinkPersistence.fetchByE_E_T(
332 entryId1, entryId2, typeId);
333
334 if (assetLink == null) {
335 return addLink(userId, entryId1, entryId2, typeId, weight);
336 }
337
338 assetLink.setWeight(weight);
339
340 assetLinkPersistence.update(assetLink);
341
342 return assetLink;
343 }
344
345
371 @Override
372 public void updateLinks(
373 long userId, long entryId, long[] linkEntryIds, int typeId)
374 throws PortalException, SystemException {
375
376 if (linkEntryIds == null) {
377 return;
378 }
379
380 List<AssetLink> links = getLinks(entryId, typeId);
381
382 for (AssetLink link : links) {
383 if (((link.getEntryId1() == entryId) &&
384 !ArrayUtil.contains(linkEntryIds, link.getEntryId2())) ||
385 ((link.getEntryId2() == entryId) &&
386 !ArrayUtil.contains(linkEntryIds, link.getEntryId1()))) {
387
388 deleteLink(link);
389 }
390 }
391
392 for (long assetLinkEntryId : linkEntryIds) {
393 if (assetLinkEntryId != entryId) {
394 AssetLink link = assetLinkPersistence.fetchByE_E_T(
395 entryId, assetLinkEntryId, typeId);
396
397 if (link == null) {
398 addLink(userId, entryId, assetLinkEntryId, typeId, 0);
399 }
400 }
401 }
402 }
403
404 }