001    /**
002     * Copyright (c) 2000-2010 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            public PortletItem addPortletItem(
038                            long userId, long groupId, String name, String portletId,
039                            String className)
040                    throws PortalException, SystemException {
041    
042                    User user = userPersistence.findByPrimaryKey(userId);
043                    long classNameId = PortalUtil.getClassNameId(className);
044                    Date now = new Date();
045    
046                    validate(name);
047    
048                    long portletItemId = counterLocalService.increment();
049    
050                    PortletItem portletItem = portletItemPersistence.create(
051                            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, false);
064    
065                    return portletItem;
066            }
067    
068            public PortletItem getPortletItem(long portletItemId)
069                    throws PortalException, SystemException {
070    
071                    return portletItemPersistence.findByPrimaryKey(portletItemId);
072            }
073    
074            public PortletItem getPortletItem(
075                            long groupId, String name, String portletId, String className)
076                    throws PortalException, SystemException {
077    
078                    long classNameId = PortalUtil.getClassNameId(className);
079    
080                    return portletItemPersistence.findByG_N_P_C(
081                            groupId, name, portletId, classNameId);
082            }
083    
084            public List<PortletItem> getPortletItems(long groupId, String className)
085                    throws SystemException {
086    
087                    long classNameId = PortalUtil.getClassNameId(className);
088    
089                    return portletItemPersistence.findByG_C(groupId, classNameId);
090            }
091    
092            public List<PortletItem> getPortletItems(
093                            long groupId, String portletId, String className)
094                    throws SystemException {
095    
096                    long classNameId = PortalUtil.getClassNameId(className);
097    
098                    return portletItemPersistence.findByG_P_C(
099                            groupId, portletId, classNameId);
100            }
101    
102            public PortletItem updatePortletItem(
103                            long userId, long groupId, String name, String portletId,
104                            String className)
105                    throws PortalException, SystemException {
106    
107                    PortletItem portletItem = null;
108    
109                    try {
110                            User user = userPersistence.findByPrimaryKey(userId);
111    
112                            portletItem = getPortletItem(
113                                    groupId, name, portletId, PortletPreferences.class.getName());
114    
115                            portletItem.setUserId(userId);
116                            portletItem.setUserName(user.getFullName());
117                            portletItem.setModifiedDate(new Date());
118    
119                            portletItemPersistence.update(portletItem, false);
120                    }
121                    catch (NoSuchPortletItemException nsste) {
122                            portletItem = addPortletItem(
123                                    userId, groupId, name, portletId,
124                                    PortletPreferences.class.getName());
125                    }
126    
127                    return portletItem;
128            }
129    
130            protected void validate(String name) throws PortalException {
131                    if (Validator.isNull(name)) {
132                            throw new PortletItemNameException();
133                    }
134            }
135    
136    }