001    /**
002     * Copyright (c) 2000-present 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.dynamicdatamapping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portlet.dynamicdatamapping.ContentException;
022    import com.liferay.portlet.dynamicdatamapping.ContentNameException;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMContent;
024    import com.liferay.portlet.dynamicdatamapping.service.base.DDMContentLocalServiceBaseImpl;
025    
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Eduardo Lundgren
031     */
032    public class DDMContentLocalServiceImpl extends DDMContentLocalServiceBaseImpl {
033    
034            @Override
035            public DDMContent addContent(
036                            long userId, long groupId, String name, String description,
037                            String data, ServiceContext serviceContext)
038                    throws PortalException {
039    
040                    User user = userPersistence.findByPrimaryKey(userId);
041    
042                    validate(name, data);
043    
044                    long contentId = counterLocalService.increment();
045    
046                    DDMContent content = ddmContentPersistence.create(contentId);
047    
048                    content.setUuid(serviceContext.getUuid());
049                    content.setGroupId(serviceContext.getScopeGroupId());
050                    content.setCompanyId(user.getCompanyId());
051                    content.setUserId(user.getUserId());
052                    content.setUserName(user.getFullName());
053                    content.setName(name);
054                    content.setDescription(description);
055                    content.setData(data);
056    
057                    ddmContentPersistence.update(content);
058    
059                    return content;
060            }
061    
062            @Override
063            public void deleteContent(DDMContent content) {
064                    ddmContentPersistence.remove(content);
065            }
066    
067            @Override
068            public void deleteContents(long groupId) {
069                    List<DDMContent> contents = ddmContentPersistence.findByGroupId(
070                            groupId);
071    
072                    for (DDMContent content : contents) {
073                            deleteContent(content);
074                    }
075            }
076    
077            @Override
078            public DDMContent getContent(long contentId) throws PortalException {
079                    return ddmContentPersistence.findByPrimaryKey(contentId);
080            }
081    
082            @Override
083            public List<DDMContent> getContents() {
084                    return ddmContentPersistence.findAll();
085            }
086    
087            @Override
088            public List<DDMContent> getContents(long groupId) {
089                    return ddmContentPersistence.findByGroupId(groupId);
090            }
091    
092            @Override
093            public List<DDMContent> getContents(long groupId, int start, int end) {
094                    return ddmContentPersistence.findByGroupId(groupId, start, end);
095            }
096    
097            @Override
098            public int getContentsCount(long groupId) {
099                    return ddmContentPersistence.countByGroupId(groupId);
100            }
101    
102            @Override
103            public DDMContent updateContent(
104                            long contentId, String name, String description, String data,
105                            ServiceContext serviceContext)
106                    throws PortalException {
107    
108                    validate(name, data);
109    
110                    DDMContent content = ddmContentPersistence.findByPrimaryKey(contentId);
111    
112                    content.setName(name);
113                    content.setDescription(description);
114                    content.setData(data);
115    
116                    ddmContentPersistence.update(content);
117    
118                    return content;
119            }
120    
121            protected void validate(String name, String data) throws PortalException {
122                    if (Validator.isNull(name)) {
123                            throw new ContentNameException();
124                    }
125    
126                    if (Validator.isNull(data)) {
127                            throw new ContentException();
128                    }
129            }
130    
131    }