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