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.dynamicdatamapping.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.xml.DocumentException;
021    import com.liferay.portal.kernel.xml.SAXReaderUtil;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.dynamicdatamapping.ContentException;
025    import com.liferay.portlet.dynamicdatamapping.ContentNameException;
026    import com.liferay.portlet.dynamicdatamapping.ContentXmlException;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMContent;
028    import com.liferay.portlet.dynamicdatamapping.service.base.DDMContentLocalServiceBaseImpl;
029    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
030    
031    import java.util.Date;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Eduardo Lundgren
037     */
038    public class DDMContentLocalServiceImpl extends DDMContentLocalServiceBaseImpl {
039    
040            @Override
041            public DDMContent addContent(
042                            long userId, long groupId, String name, String description,
043                            String xml, ServiceContext serviceContext)
044                    throws PortalException, SystemException {
045    
046                    User user = userPersistence.findByPrimaryKey(userId);
047    
048                    try {
049                            xml = DDMXMLUtil.formatXML(xml);
050                    }
051                    catch (Exception e) {
052                            throw new ContentXmlException(e);
053                    }
054    
055                    Date now = new Date();
056    
057                    validate(name, xml);
058    
059                    long contentId = counterLocalService.increment();
060    
061                    DDMContent content = ddmContentPersistence.create(contentId);
062    
063                    content.setUuid(serviceContext.getUuid());
064                    content.setGroupId(serviceContext.getScopeGroupId());
065                    content.setCompanyId(user.getCompanyId());
066                    content.setUserId(user.getUserId());
067                    content.setUserName(user.getFullName());
068                    content.setCreateDate(serviceContext.getCreateDate(now));
069                    content.setModifiedDate(serviceContext.getModifiedDate(now));
070                    content.setName(name);
071                    content.setDescription(description);
072                    content.setXml(xml);
073    
074                    ddmContentPersistence.update(content, false);
075    
076                    return content;
077            }
078    
079            @Override
080            public void deleteContent(DDMContent content) throws SystemException {
081                    ddmContentPersistence.remove(content);
082            }
083    
084            @Override
085            public void deleteContents(long groupId) throws SystemException {
086                    List<DDMContent> contents = ddmContentPersistence.findByGroupId(
087                            groupId);
088    
089                    for (DDMContent content : contents) {
090                            deleteContent(content);
091                    }
092            }
093    
094            @Override
095            public DDMContent getContent(long contentId)
096                    throws PortalException, SystemException {
097    
098                    return ddmContentPersistence.findByPrimaryKey(contentId);
099            }
100    
101            @Override
102            public List<DDMContent> getContents() throws SystemException {
103                    return ddmContentPersistence.findAll();
104            }
105    
106            @Override
107            public List<DDMContent> getContents(long groupId) throws SystemException {
108                    return ddmContentPersistence.findByGroupId(groupId);
109            }
110    
111            @Override
112            public List<DDMContent> getContents(long groupId, int start, int end)
113                    throws SystemException {
114    
115                    return ddmContentPersistence.findByGroupId(groupId, start, end);
116            }
117    
118            @Override
119            public int getContentsCount(long groupId) throws SystemException {
120                    return ddmContentPersistence.countByGroupId(groupId);
121            }
122    
123            @Override
124            public DDMContent updateContent(
125                            long contentId, String name, String description, String xml,
126                            ServiceContext serviceContext)
127                    throws PortalException, SystemException {
128    
129                    try {
130                            xml = DDMXMLUtil.formatXML(xml);
131                    }
132                    catch (Exception e) {
133                            throw new ContentXmlException();
134                    }
135    
136                    validate(name, xml);
137    
138                    DDMContent content = ddmContentPersistence.findByPrimaryKey(contentId);
139    
140                    content.setModifiedDate(serviceContext.getModifiedDate(null));
141                    content.setName(name);
142                    content.setDescription(description);
143                    content.setXml(xml);
144    
145                    ddmContentPersistence.update(content, false);
146    
147                    return content;
148            }
149    
150            protected void validate(String name, String xml) throws PortalException {
151                    if (Validator.isNull(name)) {
152                            throw new ContentNameException();
153                    }
154    
155                    if (Validator.isNull(xml)) {
156                            throw new ContentException();
157                    }
158    
159                    try {
160                            SAXReaderUtil.read(xml);
161                    }
162                    catch (DocumentException de) {
163                            throw new ContentException();
164                    }
165            }
166    
167    }