001    /**
002     * Copyright (c) 2000-2012 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            public DDMContent addContent(
041                            long userId, long groupId, String name, String description,
042                            String xml, ServiceContext serviceContext)
043                    throws PortalException, SystemException {
044    
045                    User user = userPersistence.findByPrimaryKey(userId);
046    
047                    try {
048                            xml = DDMXMLUtil.formatXML(xml);
049                    }
050                    catch (Exception e) {
051                            throw new ContentXmlException(e);
052                    }
053    
054                    Date now = new Date();
055    
056                    validate(name, xml);
057    
058                    long contentId = counterLocalService.increment();
059    
060                    DDMContent content = ddmContentPersistence.create(contentId);
061    
062                    content.setUuid(serviceContext.getUuid());
063                    content.setGroupId(serviceContext.getScopeGroupId());
064                    content.setCompanyId(user.getCompanyId());
065                    content.setUserId(user.getUserId());
066                    content.setUserName(user.getFullName());
067                    content.setCreateDate(serviceContext.getCreateDate(now));
068                    content.setModifiedDate(serviceContext.getModifiedDate(now));
069                    content.setName(name);
070                    content.setDescription(description);
071                    content.setXml(xml);
072    
073                    ddmContentPersistence.update(content, false);
074    
075                    return content;
076            }
077    
078            public void deleteContent(DDMContent content) throws SystemException {
079                    ddmContentPersistence.remove(content);
080            }
081    
082            public void deleteContents(long groupId) throws SystemException {
083                    List<DDMContent> contents = ddmContentPersistence.findByGroupId(
084                            groupId);
085    
086                    for (DDMContent content : contents) {
087                            deleteContent(content);
088                    }
089            }
090    
091            public DDMContent getContent(long contentId)
092                    throws PortalException, SystemException {
093    
094                    return ddmContentPersistence.findByPrimaryKey(contentId);
095            }
096    
097            public List<DDMContent> getContents() throws SystemException {
098                    return ddmContentPersistence.findAll();
099            }
100    
101            public List<DDMContent> getContents(long groupId) throws SystemException {
102                    return ddmContentPersistence.findByGroupId(groupId);
103            }
104    
105            public List<DDMContent> getContents(long groupId, int start, int end)
106                    throws SystemException {
107    
108                    return ddmContentPersistence.findByGroupId(groupId, start, end);
109            }
110    
111            public int getContentsCount(long groupId) throws SystemException {
112                    return ddmContentPersistence.countByGroupId(groupId);
113            }
114    
115            public DDMContent updateContent(
116                            long contentId, String name, String description, String xml,
117                            ServiceContext serviceContext)
118                    throws PortalException, SystemException {
119    
120                    try {
121                            xml = DDMXMLUtil.formatXML(xml);
122                    }
123                    catch (Exception e) {
124                            throw new ContentXmlException();
125                    }
126    
127                    validate(name, xml);
128    
129                    DDMContent content = ddmContentPersistence.findByPrimaryKey(contentId);
130    
131                    content.setModifiedDate(serviceContext.getModifiedDate(null));
132                    content.setName(name);
133                    content.setDescription(description);
134                    content.setXml(xml);
135    
136                    ddmContentPersistence.update(content, false);
137    
138                    return content;
139            }
140    
141            protected void validate(String name, String xml) throws PortalException {
142                    if (Validator.isNull(name)) {
143                            throw new ContentNameException();
144                    }
145    
146                    if (Validator.isNull(xml)) {
147                            throw new ContentException();
148                    }
149    
150                    try {
151                            SAXReaderUtil.read(xml);
152                    }
153                    catch (DocumentException de) {
154                            throw new ContentException();
155                    }
156            }
157    
158    }