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.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchPortletItemException;
018    import com.liferay.portal.PortletItemNameException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.PortletItem;
022    import com.liferay.portal.model.PortletPreferences;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.base.PortletItemLocalServiceBaseImpl;
025    
026    import java.util.Date;
027    import java.util.List;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public class PortletItemLocalServiceImpl
033            extends PortletItemLocalServiceBaseImpl {
034    
035            @Override
036            public PortletItem addPortletItem(
037                            long userId, long groupId, String name, String portletId,
038                            String className)
039                    throws PortalException {
040    
041                    User user = userPersistence.findByPrimaryKey(userId);
042                    long classNameId = classNameLocalService.getClassNameId(className);
043                    Date now = new Date();
044    
045                    validate(name);
046    
047                    long portletItemId = counterLocalService.increment();
048    
049                    PortletItem portletItem = portletItemPersistence.create(portletItemId);
050    
051                    portletItem.setGroupId(groupId);
052                    portletItem.setCompanyId(user.getCompanyId());
053                    portletItem.setUserId(user.getUserId());
054                    portletItem.setUserName(user.getFullName());
055                    portletItem.setCreateDate(now);
056                    portletItem.setModifiedDate(now);
057                    portletItem.setName(name);
058                    portletItem.setPortletId(portletId);
059                    portletItem.setClassNameId(classNameId);
060    
061                    portletItemPersistence.update(portletItem);
062    
063                    return portletItem;
064            }
065    
066            @Override
067            public PortletItem getPortletItem(
068                            long groupId, String name, String portletId, String className)
069                    throws PortalException {
070    
071                    long classNameId = classNameLocalService.getClassNameId(className);
072    
073                    return portletItemPersistence.findByG_N_P_C(
074                            groupId, name, portletId, classNameId);
075            }
076    
077            @Override
078            public List<PortletItem> getPortletItems(long groupId, String className) {
079                    long classNameId = classNameLocalService.getClassNameId(className);
080    
081                    return portletItemPersistence.findByG_C(groupId, classNameId);
082            }
083    
084            @Override
085            public List<PortletItem> getPortletItems(
086                    long groupId, String portletId, String className) {
087    
088                    long classNameId = classNameLocalService.getClassNameId(className);
089    
090                    return portletItemPersistence.findByG_P_C(
091                            groupId, portletId, classNameId);
092            }
093    
094            @Override
095            public PortletItem updatePortletItem(
096                            long userId, long groupId, String name, String portletId,
097                            String className)
098                    throws PortalException {
099    
100                    PortletItem portletItem = null;
101    
102                    try {
103                            User user = userPersistence.findByPrimaryKey(userId);
104    
105                            portletItem = getPortletItem(
106                                    groupId, name, portletId, PortletPreferences.class.getName());
107    
108                            portletItem.setUserId(userId);
109                            portletItem.setUserName(user.getFullName());
110                            portletItem.setModifiedDate(new Date());
111    
112                            portletItemPersistence.update(portletItem);
113                    }
114                    catch (NoSuchPortletItemException nspie) {
115                            portletItem = addPortletItem(
116                                    userId, groupId, name, portletId,
117                                    PortletPreferences.class.getName());
118                    }
119    
120                    return portletItem;
121            }
122    
123            protected void validate(String name) throws PortalException {
124                    if (Validator.isNull(name)) {
125                            throw new PortletItemNameException();
126                    }
127            }
128    
129    }