1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.asset.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.model.User;
20  import com.liferay.portlet.asset.CategoryPropertyKeyException;
21  import com.liferay.portlet.asset.CategoryPropertyValueException;
22  import com.liferay.portlet.asset.model.AssetCategoryProperty;
23  import com.liferay.portlet.asset.service.base.AssetCategoryPropertyLocalServiceBaseImpl;
24  import com.liferay.portlet.asset.util.AssetUtil;
25  
26  import java.util.Date;
27  import java.util.List;
28  
29  /**
30   * <a href="AssetCategoryPropertyLocalServiceImpl.java.html"><b><i>View Source
31   * </i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Jorge Ferrer
35   */
36  public class AssetCategoryPropertyLocalServiceImpl
37      extends AssetCategoryPropertyLocalServiceBaseImpl {
38  
39      public AssetCategoryProperty addCategoryProperty(
40              long userId, long categoryId, String key, String value)
41          throws PortalException, SystemException {
42  
43          User user = userPersistence.findByPrimaryKey(userId);
44          Date now = new Date();
45  
46          validate(key, value);
47  
48          long categoryPropertyId = counterLocalService.increment();
49  
50          AssetCategoryProperty categoryProperty =
51              assetCategoryPropertyPersistence.create(categoryPropertyId);
52  
53          categoryProperty.setCompanyId(user.getCompanyId());
54          categoryProperty.setUserId(user.getUserId());
55          categoryProperty.setUserName(user.getFullName());
56          categoryProperty.setCreateDate(now);
57          categoryProperty.setModifiedDate(now);
58          categoryProperty.setCategoryId(categoryId);
59          categoryProperty.setKey(key);
60          categoryProperty.setValue(value);
61  
62          assetCategoryPropertyPersistence.update(categoryProperty, false);
63  
64          return categoryProperty;
65      }
66  
67      public void deleteCategoryProperties(long entryId) throws SystemException {
68          List<AssetCategoryProperty> categoryProperties =
69              assetCategoryPropertyPersistence.findByCategoryId(entryId);
70  
71          for (AssetCategoryProperty categoryProperty : categoryProperties) {
72              deleteCategoryProperty(categoryProperty);
73          }
74      }
75  
76      public void deleteCategoryProperty(AssetCategoryProperty categoryProperty)
77          throws SystemException {
78  
79          assetCategoryPropertyPersistence.remove(categoryProperty);
80      }
81  
82      public void deleteCategoryProperty(long categoryPropertyId)
83          throws PortalException, SystemException {
84  
85          AssetCategoryProperty categoryProperty =
86              assetCategoryPropertyPersistence.findByPrimaryKey(
87                  categoryPropertyId);
88  
89          deleteCategoryProperty(categoryProperty);
90      }
91  
92      public List<AssetCategoryProperty> getCategoryProperties()
93          throws SystemException {
94  
95          return assetCategoryPropertyPersistence.findAll();
96      }
97  
98      public List<AssetCategoryProperty> getCategoryProperties(long entryId)
99          throws SystemException {
100 
101         return assetCategoryPropertyPersistence.findByCategoryId(entryId);
102     }
103 
104     public AssetCategoryProperty getCategoryProperty(long categoryPropertyId)
105         throws PortalException, SystemException {
106 
107         return assetCategoryPropertyPersistence.findByPrimaryKey(
108             categoryPropertyId);
109     }
110 
111     public AssetCategoryProperty getCategoryProperty(
112             long categoryId, String key)
113         throws PortalException, SystemException {
114 
115         return assetCategoryPropertyPersistence.findByCA_K(categoryId, key);
116     }
117 
118     public List<AssetCategoryProperty> getCategoryPropertyValues(
119             long groupId, String key)
120         throws SystemException {
121 
122         return assetCategoryPropertyFinder.findByG_K(groupId, key);
123     }
124 
125     public AssetCategoryProperty updateCategoryProperty(
126             long categoryPropertyId, String key, String value)
127         throws PortalException, SystemException {
128 
129         validate(key, value);
130 
131         AssetCategoryProperty categoryProperty =
132             assetCategoryPropertyPersistence.findByPrimaryKey(
133                 categoryPropertyId);
134 
135         categoryProperty.setModifiedDate(new Date());
136         categoryProperty.setKey(key);
137         categoryProperty.setValue(value);
138 
139         assetCategoryPropertyPersistence.update(categoryProperty, false);
140 
141         return categoryProperty;
142     }
143 
144     protected void validate(String key, String value) throws PortalException {
145         if (!AssetUtil.isValidWord(key)) {
146             throw new CategoryPropertyKeyException();
147         }
148 
149         if (!AssetUtil.isValidWord(value)) {
150             throw new CategoryPropertyValueException();
151         }
152     }
153 
154 }