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.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(
087                                            attributeName);
088    
089                                    if (Validator.isNull(value)) {
090                                            continue;
091                                    }
092    
093                                    UnicodeProperties properties =
094                                            expandoBridge.getAttributeProperties(attributeName);
095    
096                                    boolean propertyHidden = GetterUtil.getBoolean(
097                                            properties.get(ExpandoColumnConstants.PROPERTY_HIDDEN));
098                                    boolean propertyVisibleWithUpdatePermission =
099                                            GetterUtil.getBoolean(
100                                                    properties.get(
101                                                            ExpandoColumnConstants.
102                                                                    PROPERTY_VISIBLE_WITH_UPDATE_PERMISSION));
103    
104                                    if (_editable && propertyVisibleWithUpdatePermission) {
105                                            if (ExpandoColumnPermissionUtil.contains(
106                                                            permissionChecker, companyId, _className,
107                                                            ExpandoTableConstants.DEFAULT_TABLE_NAME,
108                                                            attributeName, ActionKeys.UPDATE)) {
109    
110                                                    propertyHidden = false;
111                                            }
112                                            else {
113                                                    propertyHidden = true;
114                                            }
115                                    }
116    
117                                    if (!propertyHidden &&
118                                            ExpandoColumnPermissionUtil.contains(
119                                                    permissionChecker, companyId, _className,
120                                                    ExpandoTableConstants.DEFAULT_TABLE_NAME,
121                                                    attributeName, ActionKeys.VIEW)) {
122    
123                                            return EVAL_BODY_INCLUDE;
124                                    }
125                            }
126    
127                            return SKIP_BODY;
128                    }
129                    catch (Exception e) {
130                            throw new JspException(e);
131                    }
132                    finally {
133                            if (!ServerDetector.isResin()) {
134                                    _className = null;
135                                    _classPK = 0;
136                                    _companyId = 0;
137                                    _editable = false;
138                            }
139                    }
140            }
141    
142            public void setClassName(String className) {
143                    _className = className;
144            }
145    
146            public void setClassPK(long classPK) {
147                    _classPK = classPK;
148            }
149    
150            public void setCompanyId(long companyId) {
151                    _companyId = companyId;
152            }
153    
154            public void setEditable(boolean editable) {
155                    _editable = editable;
156            }
157    
158            private String _className;
159            private long _classPK;
160            private long _companyId;
161            private boolean _editable;
162    
163    }