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