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.util.ArrayUtil;
019 import com.liferay.portal.model.User;
020 import com.liferay.portlet.asset.NoSuchLinkException;
021 import com.liferay.portlet.asset.model.AssetEntry;
022 import com.liferay.portlet.asset.model.AssetLink;
023 import com.liferay.portlet.asset.model.AssetLinkConstants;
024 import com.liferay.portlet.asset.service.base.AssetLinkLocalServiceBaseImpl;
025
026 import java.util.ArrayList;
027 import java.util.Collections;
028 import java.util.Date;
029 import java.util.List;
030
031
041 public class AssetLinkLocalServiceImpl extends AssetLinkLocalServiceBaseImpl {
042
043
060 @Override
061 public AssetLink addLink(
062 long userId, long entryId1, long entryId2, int type, int weight)
063 throws PortalException {
064
065 User user = userPersistence.findByPrimaryKey(userId);
066 Date now = new Date();
067
068 long linkId = counterLocalService.increment();
069
070 AssetLink link = assetLinkPersistence.create(linkId);
071
072 link.setCompanyId(user.getCompanyId());
073 link.setUserId(user.getUserId());
074 link.setUserName(user.getFullName());
075 link.setCreateDate(now);
076 link.setEntryId1(entryId1);
077 link.setEntryId2(entryId2);
078 link.setType(type);
079 link.setWeight(weight);
080
081 assetLinkPersistence.update(link);
082
083 if (AssetLinkConstants.isTypeBi(type)) {
084 long linkId2 = counterLocalService.increment();
085
086 AssetLink link2 = assetLinkPersistence.create(linkId2);
087
088 link2.setCompanyId(user.getCompanyId());
089 link2.setUserId(user.getUserId());
090 link2.setUserName(user.getFullName());
091 link2.setCreateDate(now);
092 link2.setEntryId1(entryId2);
093 link2.setEntryId2(entryId1);
094 link2.setType(type);
095 link2.setWeight(weight);
096
097 assetLinkPersistence.update(link2);
098 }
099
100 return link;
101 }
102
103
108 @Override
109 public void deleteLink(AssetLink link) {
110 if (AssetLinkConstants.isTypeBi(link.getType())) {
111 try {
112 assetLinkPersistence.removeByE_E_T(
113 link.getEntryId2(), link.getEntryId1(), link.getType());
114 }
115 catch (NoSuchLinkException nsle) {
116 }
117 }
118
119 assetLinkPersistence.remove(link);
120 }
121
122
128 @Override
129 public void deleteLink(long linkId) throws PortalException {
130 AssetLink link = assetLinkPersistence.findByPrimaryKey(linkId);
131
132 deleteLink(link);
133 }
134
135
140 @Override
141 public void deleteLinks(long entryId) {
142 for (AssetLink link : assetLinkPersistence.findByE1(entryId)) {
143 deleteLink(link);
144 }
145
146 for (AssetLink link : assetLinkPersistence.findByE2(entryId)) {
147 deleteLink(link);
148 }
149 }
150
151
157 @Override
158 public void deleteLinks(long entryId1, long entryId2) {
159 List<AssetLink> links = assetLinkPersistence.findByE_E(
160 entryId1, entryId2);
161
162 for (AssetLink link : links) {
163 deleteLink(link);
164 }
165 }
166
167
173 @Override
174 public List<AssetLink> getDirectLinks(long entryId) {
175 List<AssetLink> assetLinks = assetLinkPersistence.findByE1(entryId);
176
177 if (!assetLinks.isEmpty()) {
178 List<AssetLink> filteredAssetLinks = new ArrayList<AssetLink>(
179 assetLinks.size());
180
181 for (AssetLink assetLink : assetLinks) {
182 AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
183 assetLink.getEntryId2());
184
185 if ((assetEntry != null) && assetEntry.isVisible()) {
186 filteredAssetLinks.add(assetLink);
187 }
188 }
189
190 assetLinks = Collections.unmodifiableList(filteredAssetLinks);
191 }
192
193 return assetLinks;
194 }
195
196
210 @Override
211 public List<AssetLink> getDirectLinks(long entryId, int typeId) {
212 List<AssetLink> assetLinks = assetLinkPersistence.findByE1_T(
213 entryId, typeId);
214
215 if (!assetLinks.isEmpty()) {
216 List<AssetLink> filteredAssetLinks = new ArrayList<AssetLink>(
217 assetLinks.size());
218
219 for (AssetLink assetLink : assetLinks) {
220 AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
221 assetLink.getEntryId2());
222
223 if ((assetEntry != null) && assetEntry.isVisible()) {
224 filteredAssetLinks.add(assetLink);
225 }
226 }
227
228 assetLinks = Collections.unmodifiableList(filteredAssetLinks);
229 }
230
231 return assetLinks;
232 }
233
234
242 @Override
243 public List<AssetLink> getLinks(long entryId) {
244 List<AssetLink> e1Links = assetLinkPersistence.findByE1(entryId);
245 List<AssetLink> e2Links = assetLinkPersistence.findByE2(entryId);
246
247 List<AssetLink> links = new ArrayList<AssetLink>(
248 e1Links.size() + e2Links.size());
249
250 links.addAll(e1Links);
251 links.addAll(e2Links);
252
253 return links;
254 }
255
256
270 @Override
271 public List<AssetLink> getLinks(long entryId, int typeId) {
272 List<AssetLink> e1Links = assetLinkPersistence.findByE1_T(
273 entryId, typeId);
274 List<AssetLink> e2Links = assetLinkPersistence.findByE2_T(
275 entryId, typeId);
276
277 List<AssetLink> links = new ArrayList<AssetLink>(
278 e1Links.size() + e2Links.size());
279
280 links.addAll(e1Links);
281 links.addAll(e2Links);
282
283 return links;
284 }
285
286
300 @Override
301 public List<AssetLink> getReverseLinks(long entryId, int typeId) {
302 return assetLinkPersistence.findByE2_T(entryId, typeId);
303 }
304
305 @Override
306 public AssetLink updateLink(
307 long userId, long entryId1, long entryId2, int typeId, int weight)
308 throws PortalException {
309
310 AssetLink assetLink = assetLinkPersistence.fetchByE_E_T(
311 entryId1, entryId2, typeId);
312
313 if (assetLink == null) {
314 return addLink(userId, entryId1, entryId2, typeId, weight);
315 }
316
317 assetLink.setWeight(weight);
318
319 assetLinkPersistence.update(assetLink);
320
321 return assetLink;
322 }
323
324
349 @Override
350 public void updateLinks(
351 long userId, long entryId, long[] linkEntryIds, int typeId)
352 throws PortalException {
353
354 if (linkEntryIds == null) {
355 return;
356 }
357
358 List<AssetLink> links = getLinks(entryId, typeId);
359
360 for (AssetLink link : links) {
361 if (((link.getEntryId1() == entryId) &&
362 !ArrayUtil.contains(linkEntryIds, link.getEntryId2())) ||
363 ((link.getEntryId2() == entryId) &&
364 !ArrayUtil.contains(linkEntryIds, link.getEntryId1()))) {
365
366 deleteLink(link);
367 }
368 }
369
370 for (long assetLinkEntryId : linkEntryIds) {
371 if (assetLinkEntryId != entryId) {
372 AssetLink link = assetLinkPersistence.fetchByE_E_T(
373 entryId, assetLinkEntryId, typeId);
374
375 if (link == null) {
376 addLink(userId, entryId, assetLinkEntryId, typeId, 0);
377 }
378 }
379 }
380 }
381
382 }