001    /**
002     * Copyright (c) 2000-2011 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.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 SKIP_BODY;
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    }