001    /**
002     * Copyright (c) 2000-2013 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            @Override
037            public AssetCategoryProperty addCategoryProperty(
038                            long userId, long categoryId, String key, String value)
039                    throws PortalException, SystemException {
040    
041                    User user = userPersistence.findByPrimaryKey(userId);
042                    Date now = new Date();
043    
044                    validate(key, value);
045    
046                    long categoryPropertyId = counterLocalService.increment();
047    
048                    AssetCategoryProperty categoryProperty =
049                            assetCategoryPropertyPersistence.create(categoryPropertyId);
050    
051                    categoryProperty.setCompanyId(user.getCompanyId());
052                    categoryProperty.setUserId(user.getUserId());
053                    categoryProperty.setUserName(user.getFullName());
054                    categoryProperty.setCreateDate(now);
055                    categoryProperty.setModifiedDate(now);
056                    categoryProperty.setCategoryId(categoryId);
057                    categoryProperty.setKey(key);
058                    categoryProperty.setValue(value);
059    
060                    assetCategoryPropertyPersistence.update(categoryProperty, false);
061    
062                    return categoryProperty;
063            }
064    
065            @Override
066            public void deleteCategoryProperties(long entryId) throws SystemException {
067                    List<AssetCategoryProperty> categoryProperties =
068                            assetCategoryPropertyPersistence.findByCategoryId(entryId);
069    
070                    for (AssetCategoryProperty categoryProperty : categoryProperties) {
071                            deleteCategoryProperty(categoryProperty);
072                    }
073            }
074    
075            @Override
076            public void deleteCategoryProperty(AssetCategoryProperty categoryProperty)
077                    throws SystemException {
078    
079                    assetCategoryPropertyPersistence.remove(categoryProperty);
080            }
081    
082            @Override
083            public void deleteCategoryProperty(long categoryPropertyId)
084                    throws PortalException, SystemException {
085    
086                    AssetCategoryProperty categoryProperty =
087                            assetCategoryPropertyPersistence.findByPrimaryKey(
088                                    categoryPropertyId);
089    
090                    deleteCategoryProperty(categoryProperty);
091            }
092    
093            @Override
094            public List<AssetCategoryProperty> getCategoryProperties()
095                    throws SystemException {
096    
097                    return assetCategoryPropertyPersistence.findAll();
098            }
099    
100            @Override
101            public List<AssetCategoryProperty> getCategoryProperties(long entryId)
102                    throws SystemException {
103    
104                    return assetCategoryPropertyPersistence.findByCategoryId(entryId);
105            }
106    
107            @Override
108            public AssetCategoryProperty getCategoryProperty(long categoryPropertyId)
109                    throws PortalException, SystemException {
110    
111                    return assetCategoryPropertyPersistence.findByPrimaryKey(
112                            categoryPropertyId);
113            }
114    
115            @Override
116            public AssetCategoryProperty getCategoryProperty(
117                            long categoryId, String key)
118                    throws PortalException, SystemException {
119    
120                    return assetCategoryPropertyPersistence.findByCA_K(categoryId, key);
121            }
122    
123            @Override
124            public List<AssetCategoryProperty> getCategoryPropertyValues(
125                            long groupId, String key)
126                    throws SystemException {
127    
128                    return assetCategoryPropertyFinder.findByG_K(groupId, key);
129            }
130    
131            @Override
132            public AssetCategoryProperty updateCategoryProperty(
133                            long categoryPropertyId, String key, String value)
134                    throws PortalException, SystemException {
135    
136                    validate(key, value);
137    
138                    AssetCategoryProperty categoryProperty =
139                            assetCategoryPropertyPersistence.findByPrimaryKey(
140                                    categoryPropertyId);
141    
142                    categoryProperty.setModifiedDate(new Date());
143                    categoryProperty.setKey(key);
144                    categoryProperty.setValue(value);
145    
146                    assetCategoryPropertyPersistence.update(categoryProperty, false);
147    
148                    return categoryProperty;
149            }
150    
151            protected void validate(String key, String value) throws PortalException {
152                    if (!AssetUtil.isValidWord(key)) {
153                            throw new CategoryPropertyKeyException();
154                    }
155    
156                    if (!AssetUtil.isValidWord(value)) {
157                            throw new CategoryPropertyValueException();
158                    }
159            }
160    
161    }