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.taglib.ui;
016    
017    import com.liferay.portal.kernel.servlet.taglib.TagSupport;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portlet.expando.model.ExpandoBridge;
027    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
028    import com.liferay.portlet.expando.model.ExpandoTableConstants;
029    import com.liferay.portlet.expando.service.permission.ExpandoColumnPermissionUtil;
030    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
031    
032    import java.io.Serializable;
033    
034    import java.util.Enumeration;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.jsp.JspException;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class CustomAttributesAvailableTag extends TagSupport {
043    
044            @Override
045            public int doStartTag() throws JspException {
046                    try {
047                            HttpServletRequest request =
048                                    (HttpServletRequest)pageContext.getRequest();
049    
050                            ThemeDisplay themeDisplay =
051                                    (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
052    
053                            long companyId = _companyId;
054    
055                            if (companyId == 0) {
056                                    companyId = themeDisplay.getCompanyId();
057                            }
058    
059                            ExpandoBridge expandoBridge = null;
060    
061                            if (_classPK == 0) {
062                                    expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
063                                            companyId, _className);
064                            }
065                            else {
066                                    expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
067                                            companyId, _className, _classPK);
068                            }
069    
070                            Enumeration<String> enu = expandoBridge.getAttributeNames();
071    
072                            if (!enu.hasMoreElements()) {
073                                    return SKIP_BODY;
074                            }
075    
076                            if (_classPK == 0) {
077                                    return EVAL_BODY_INCLUDE;
078                            }
079    
080                            PermissionChecker permissionChecker =
081                                    themeDisplay.getPermissionChecker();
082    
083                            while (enu.hasMoreElements()) {
084                                    String attributeName = enu.nextElement();
085    
086                                    Serializable value = expandoBridge.getAttribute(attributeName);
087    
088                                    if (Validator.isNull(value)) {
089                                            continue;
090                                    }
091    
092                                    UnicodeProperties properties =
093                                            expandoBridge.getAttributeProperties(attributeName);
094    
095                                    boolean propertyHidden = GetterUtil.getBoolean(
096                                            properties.get(ExpandoColumnConstants.PROPERTY_HIDDEN));
097                                    boolean propertyVisibleWithUpdatePermission =
098                                            GetterUtil.getBoolean(
099                                                    properties.get(
100                                                            ExpandoColumnConstants.
101                                                                    PROPERTY_VISIBLE_WITH_UPDATE_PERMISSION));
102    
103                                    if (_editable && propertyVisibleWithUpdatePermission) {
104                                            if (ExpandoColumnPermissionUtil.contains(
105                                                            permissionChecker, companyId, _className,
106                                                            ExpandoTableConstants.DEFAULT_TABLE_NAME,
107                                                            attributeName, ActionKeys.UPDATE)) {
108    
109                                                    propertyHidden = false;
110                                            }
111                                            else {
112                                                    propertyHidden = true;
113                                            }
114                                    }
115    
116                                    if (!propertyHidden &&
117                                            ExpandoColumnPermissionUtil.contains(
118                                                    permissionChecker, companyId, _className,
119                                                    ExpandoTableConstants.DEFAULT_TABLE_NAME, attributeName,
120                                                    ActionKeys.VIEW)) {
121    
122                                            return EVAL_BODY_INCLUDE;
123                                    }
124                            }
125    
126                            return SKIP_BODY;
127                    }
128                    catch (Exception e) {
129                            throw new JspException(e);
130                    }
131                    finally {
132                            if (!ServerDetector.isResin()) {
133                                    _className = null;
134                                    _classPK = 0;
135                                    _companyId = 0;
136                                    _editable = false;
137                            }
138                    }
139            }
140    
141            public void setClassName(String className) {
142                    _className = className;
143            }
144    
145            public void setClassPK(long classPK) {
146                    _classPK = classPK;
147            }
148    
149            public void setCompanyId(long companyId) {
150                    _companyId = companyId;
151            }
152    
153            public void setEditable(boolean editable) {
154                    _editable = editable;
155            }
156    
157            private String _className;
158            private long _classPK;
159            private long _companyId;
160            private boolean _editable;
161    
162    }