001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
069            public PortletItem getPortletItem(long portletItemId)
070                    throws PortalException, SystemException {
071    
072                    return portletItemPersistence.findByPrimaryKey(portletItemId);
073            }
074    
075            public PortletItem getPortletItem(
076                            long groupId, String name, String portletId, String className)
077                    throws PortalException, SystemException {
078    
079                    long classNameId = PortalUtil.getClassNameId(className);
080    
081                    return portletItemPersistence.findByG_N_P_C(
082                            groupId, name, portletId, classNameId);
083            }
084    
085            public List<PortletItem> getPortletItems(long groupId, String className)
086                    throws SystemException {
087    
088                    long classNameId = PortalUtil.getClassNameId(className);
089    
090                    return portletItemPersistence.findByG_C(groupId, classNameId);
091            }
092    
093            public List<PortletItem> getPortletItems(
094                            long groupId, String portletId, String className)
095                    throws SystemException {
096    
097                    long classNameId = PortalUtil.getClassNameId(className);
098    
099                    return portletItemPersistence.findByG_P_C(
100                            groupId, portletId, classNameId);
101            }
102    
103            public PortletItem updatePortletItem(
104                            long userId, long groupId, String name, String portletId,
105                            String className)
106                    throws PortalException, SystemException {
107    
108                    PortletItem portletItem = null;
109    
110                    try {
111                            User user = userPersistence.findByPrimaryKey(userId);
112    
113                            portletItem = getPortletItem(
114                                    groupId, name, portletId, PortletPreferences.class.getName());
115    
116                            portletItem.setUserId(userId);
117                            portletItem.setUserName(user.getFullName());
118                            portletItem.setModifiedDate(new Date());
119    
120                            portletItemPersistence.update(portletItem, false);
121                    }
122                    catch (NoSuchPortletItemException nsste) {
123                            portletItem = addPortletItem(
124                                    userId, groupId, name, portletId,
125                                    PortletPreferences.class.getName());
126                    }
127    
128                    return portletItem;
129            }
130    
131            protected void validate(String name) throws PortalException {
132                    if (Validator.isNull(name)) {
133                            throw new PortletItemNameException();
134                    }
135            }
136    
137    }