001    /**
002     * Copyright (c) 2000-2010 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.util.ServerDetector;
018    import com.liferay.portal.security.auth.CompanyThreadLocal;
019    import com.liferay.portlet.expando.model.ExpandoBridge;
020    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
021    import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
022    
023    import java.util.Collections;
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class CustomAttributesAvailableTag
030            extends ParamAndPropertyAncestorTagImpl {
031    
032            public int doStartTag() {
033                    try {
034                            long companyId = _companyId;
035    
036                            if (companyId == 0) {
037                                    companyId = CompanyThreadLocal.getCompanyId();
038                            }
039    
040                            ExpandoBridge expandoBridge =
041                                    ExpandoBridgeFactoryUtil.getExpandoBridge(
042                                            companyId, _className);
043    
044                            List<String> attributeNames = Collections.list(
045                                    expandoBridge.getAttributeNames());
046    
047                            if (attributeNames.isEmpty()) {
048                                    return EVAL_PAGE;
049                            }
050                            else {
051                                    return EVAL_BODY_INCLUDE;
052                            }
053                    }
054                    finally {
055                            if (!ServerDetector.isResin()) {
056                                    _className = null;
057                                    _companyId = 0;
058                            }
059                    }
060            }
061    
062            public void setClassName(String className) {
063                    _className = className;
064            }
065    
066            public void setCompanyId(long companyId) {
067                    _companyId = companyId;
068            }
069    
070            private String _className;
071            private long _companyId;
072    
073    }