001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.model.User;
020    import com.liferay.portlet.asset.CategoryPropertyKeyException;
021    import com.liferay.portlet.asset.CategoryPropertyValueException;
022    import com.liferay.portlet.asset.model.AssetCategoryProperty;
023    import com.liferay.portlet.asset.service.base.AssetCategoryPropertyLocalServiceBaseImpl;
024    import com.liferay.portlet.asset.util.AssetUtil;
025    
026    import java.util.Date;
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Jorge Ferrer
032     */
033    public class AssetCategoryPropertyLocalServiceImpl
034            extends AssetCategoryPropertyLocalServiceBaseImpl {
035    
036            public AssetCategoryProperty addCategoryProperty(
037                            long userId, long categoryId, String key, String value)
038                    throws PortalException, SystemException {
039    
040                    User user = userPersistence.findByPrimaryKey(userId);
041                    Date now = new Date();
042    
043                    validate(key, value);
044    
045                    long categoryPropertyId = counterLocalService.increment();
046    
047                    AssetCategoryProperty categoryProperty =
048                            assetCategoryPropertyPersistence.create(categoryPropertyId);
049    
050                    categoryProperty.setCompanyId(user.getCompanyId());
051                    categoryProperty.setUserId(user.getUserId());
052                    categoryProperty.setUserName(user.getFullName());
053                    categoryProperty.setCreateDate(now);
054                    categoryProperty.setModifiedDate(now);
055                    categoryProperty.setCategoryId(categoryId);
056                    categoryProperty.setKey(key);
057                    categoryProperty.setValue(value);
058    
059                    assetCategoryPropertyPersistence.update(categoryProperty, false);
060    
061                    return categoryProperty;
062            }
063    
064            public void deleteCategoryProperties(long entryId) throws SystemException {
065                    List<AssetCategoryProperty> categoryProperties =
066                            assetCategoryPropertyPersistence.findByCategoryId(entryId);
067    
068                    for (AssetCategoryProperty categoryProperty : categoryProperties) {
069                            deleteCategoryProperty(categoryProperty);
070                    }
071            }
072    
073            public void deleteCategoryProperty(AssetCategoryProperty categoryProperty)
074                    throws SystemException {
075    
076                    assetCategoryPropertyPersistence.remove(categoryProperty);
077            }
078    
079            public void deleteCategoryProperty(long categoryPropertyId)
080                    throws PortalException, SystemException {
081    
082                    AssetCategoryProperty categoryProperty =
083                            assetCategoryPropertyPersistence.findByPrimaryKey(
084                                    categoryPropertyId);
085    
086                    deleteCategoryProperty(categoryProperty);
087            }
088    
089            public List<AssetCategoryProperty> getCategoryProperties()
090                    throws SystemException {
091    
092                    return assetCategoryPropertyPersistence.findAll();
093            }
094    
095            public List<AssetCategoryProperty> getCategoryProperties(long entryId)
096                    throws SystemException {
097    
098                    return assetCategoryPropertyPersistence.findByCategoryId(entryId);
099            }
100    
101            public AssetCategoryProperty getCategoryProperty(long categoryPropertyId)
102                    throws PortalException, SystemException {
103    
104                    return assetCategoryPropertyPersistence.findByPrimaryKey(
105                            categoryPropertyId);
106            }
107    
108            public AssetCategoryProperty getCategoryProperty(
109                            long categoryId, String key)
110                    throws PortalException, SystemException {
111    
112                    return assetCategoryPropertyPersistence.findByCA_K(categoryId, key);
113            }
114    
115            public List<AssetCategoryProperty> getCategoryPropertyValues(
116                            long groupId, String key)
117                    throws SystemException {
118    
119                    return assetCategoryPropertyFinder.findByG_K(groupId, key);
120            }
121    
122            public AssetCategoryProperty updateCategoryProperty(
123                            long categoryPropertyId, String key, String value)
124                    throws PortalException, SystemException {
125    
126                    validate(key, value);
127    
128                    AssetCategoryProperty categoryProperty =
129                            assetCategoryPropertyPersistence.findByPrimaryKey(
130                                    categoryPropertyId);
131    
132                    categoryProperty.setModifiedDate(new Date());
133                    categoryProperty.setKey(key);
134                    categoryProperty.setValue(value);
135    
136                    assetCategoryPropertyPersistence.update(categoryProperty, false);
137    
138                    return categoryProperty;
139            }
140    
141            protected void validate(String key, String value) throws PortalException {
142                    if (!AssetUtil.isValidWord(key)) {
143                            throw new CategoryPropertyKeyException();
144                    }
145    
146                    if (!AssetUtil.isValidWord(value)) {
147                            throw new CategoryPropertyValueException();
148                    }
149            }
150    
151    }