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.portlet.directory.asset;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
018    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.service.permission.UserPermissionUtil;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.asset.model.BaseAssetRenderer;
027    
028    import java.util.Locale;
029    
030    import javax.portlet.PortletRequest;
031    import javax.portlet.PortletURL;
032    import javax.portlet.RenderRequest;
033    import javax.portlet.RenderResponse;
034    
035    /**
036     * @author Michael C. Han
037     * @author Sergio Gonz??lez
038     */
039    public class UserAssetRenderer extends BaseAssetRenderer {
040    
041            public UserAssetRenderer(User user) {
042                    _user = user;
043            }
044    
045            @Override
046            public String getClassName() {
047                    return User.class.getName();
048            }
049    
050            @Override
051            public long getClassPK() {
052                    return _user.getPrimaryKey();
053            }
054    
055            @Override
056            public String getDiscussionPath() {
057                    return null;
058            }
059    
060            @Override
061            public long getGroupId() {
062                    return 0;
063            }
064    
065            @Override
066            public String getSummary(Locale locale) {
067                    return _user.getComments();
068            }
069    
070            @Override
071            public String getTitle(Locale locale) {
072                    return _user.getFullName();
073            }
074    
075            @Override
076            public PortletURL getURLEdit(
077                            LiferayPortletRequest liferayPortletRequest,
078                            LiferayPortletResponse liferayPortletResponse)
079                    throws Exception {
080    
081                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
082                            getControlPanelPlid(liferayPortletRequest), PortletKeys.USERS_ADMIN,
083                            PortletRequest.RENDER_PHASE);
084    
085                    portletURL.setParameter("struts_action", "/users_admin/edit_user");
086                    portletURL.setParameter("p_u_i_d", String.valueOf(_user.getUserId()));
087    
088                    return portletURL;
089            }
090    
091            @Override
092            public String getUrlTitle() {
093                    return _user.getScreenName();
094            }
095    
096            @Override
097            public String getURLViewInContext(
098                    LiferayPortletRequest liferayPortletRequest,
099                    LiferayPortletResponse liferayPortletResponse,
100                    String noSuchEntryRedirect) {
101    
102                    ThemeDisplay themeDisplay =
103                            (ThemeDisplay)liferayPortletRequest.getAttribute(
104                                    WebKeys.THEME_DISPLAY);
105    
106                    try {
107                            return _user.getDisplayURL(themeDisplay);
108                    }
109                    catch (Exception e) {
110                    }
111    
112                    return noSuchEntryRedirect;
113            }
114    
115            @Override
116            public long getUserId() {
117                    return _user.getUserId();
118            }
119    
120            @Override
121            public String getUserName() {
122                    return _user.getFullName();
123            }
124    
125            @Override
126            public String getUuid() {
127                    return _user.getUuid();
128            }
129    
130            @Override
131            public boolean hasEditPermission(PermissionChecker permissionChecker) {
132                    return UserPermissionUtil.contains(
133                            permissionChecker, _user.getUserId(), ActionKeys.UPDATE);
134            }
135    
136            @Override
137            public boolean hasViewPermission(PermissionChecker permissionChecker) {
138                    return UserPermissionUtil.contains(
139                            permissionChecker, _user.getUserId(), ActionKeys.VIEW);
140            }
141    
142            @Override
143            public boolean isPrintable() {
144                    return false;
145            }
146    
147            @Override
148            public String render(
149                            RenderRequest renderRequest, RenderResponse renderResponse,
150                            String template)
151                    throws Exception {
152    
153                    if (template.equals(TEMPLATE_ABSTRACT) ||
154                            template.equals(TEMPLATE_FULL_CONTENT)) {
155    
156                            renderRequest.setAttribute(WebKeys.USER, _user);
157    
158                            return "/html/portlet/directory/asset/abstract.jsp";
159                    }
160                    else {
161                            return null;
162                    }
163            }
164    
165            @Override
166            protected String getIconPath(ThemeDisplay themeDisplay) {
167                    return themeDisplay.getPathThemeImages() + "/common/user_icon.png";
168            }
169    
170            private User _user;
171    
172    }