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