001    /**
002     * Copyright (c) 2000-2012 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(portletItemId);
051    
052                    portletItem.setGroupId(groupId);
053                    portletItem.setCompanyId(user.getCompanyId());
054                    portletItem.setUserId(user.getUserId());
055                    portletItem.setUserName(user.getFullName());
056                    portletItem.setCreateDate(now);
057                    portletItem.setModifiedDate(now);
058                    portletItem.setName(name);
059                    portletItem.setPortletId(portletId);
060                    portletItem.setClassNameId(classNameId);
061    
062                    portletItemPersistence.update(portletItem);
063    
064                    return portletItem;
065            }
066    
067            public PortletItem getPortletItem(
068                            long groupId, String name, String portletId, String className)
069                    throws PortalException, SystemException {
070    
071                    long classNameId = PortalUtil.getClassNameId(className);
072    
073                    return portletItemPersistence.findByG_N_P_C(
074                            groupId, name, portletId, classNameId);
075            }
076    
077            public List<PortletItem> getPortletItems(long groupId, String className)
078                    throws SystemException {
079    
080                    long classNameId = PortalUtil.getClassNameId(className);
081    
082                    return portletItemPersistence.findByG_C(groupId, classNameId);
083            }
084    
085            public List<PortletItem> getPortletItems(
086                            long groupId, String portletId, String className)
087                    throws SystemException {
088    
089                    long classNameId = PortalUtil.getClassNameId(className);
090    
091                    return portletItemPersistence.findByG_P_C(
092                            groupId, portletId, classNameId);
093            }
094    
095            public PortletItem updatePortletItem(
096                            long userId, long groupId, String name, String portletId,
097                            String className)
098                    throws PortalException, SystemException {
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    }