001    /**
002     * Copyright (c) 2000-2013 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.portlet.dynamicdatalists.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.security.permission.PermissionChecker;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portlet.asset.model.AssetRenderer;
022    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
023    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
025    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
026    import com.liferay.portlet.dynamicdatalists.service.permission.DDLRecordSetPermission;
027    
028    /**
029     * @author Marcellus Tavares
030     */
031    public class DDLRecordAssetRendererFactory extends BaseAssetRendererFactory {
032    
033            public static final String TYPE = "record";
034    
035            @Override
036            public AssetRenderer getAssetRenderer(long classPK, int type)
037                    throws PortalException, SystemException {
038    
039                    DDLRecord record = DDLRecordLocalServiceUtil.fetchDDLRecord(classPK);
040    
041                    DDLRecordVersion recordVersion = null;
042    
043                    if (record == null) {
044                            recordVersion = DDLRecordLocalServiceUtil.getRecordVersion(classPK);
045    
046                            record = recordVersion.getRecord();
047                    }
048                    else {
049                            if (type == TYPE_LATEST) {
050                                    recordVersion = record.getLatestRecordVersion();
051                            }
052                            else if (type == TYPE_LATEST_APPROVED) {
053                                    recordVersion = record.getRecordVersion();
054                            }
055                            else {
056                                    throw new IllegalArgumentException(
057                                            "Unknown asset renderer type " + type);
058                            }
059                    }
060    
061                    DDLRecordAssetRenderer ddlRecordAssetRenderer =
062                            new DDLRecordAssetRenderer(record, recordVersion);
063    
064                    ddlRecordAssetRenderer.setAssetRendererType(type);
065    
066                    return ddlRecordAssetRenderer;
067            }
068    
069            @Override
070            public String getClassName() {
071                    return DDLRecord.class.getName();
072            }
073    
074            @Override
075            public String getType() {
076                    return TYPE;
077            }
078    
079            @Override
080            public boolean hasPermission(
081                            PermissionChecker permissionChecker, long classPK, String actionId)
082                    throws Exception {
083    
084                    DDLRecord record = DDLRecordLocalServiceUtil.getRecord(classPK);
085    
086                    return DDLRecordSetPermission.contains(
087                            permissionChecker, record.getRecordSet(), actionId);
088            }
089    
090            @Override
091            public boolean isCategorizable() {
092                    return _CATEGORIZABLE;
093            }
094    
095            @Override
096            public boolean isSelectable() {
097                    return _SELECTABLE;
098            }
099    
100            @Override
101            protected String getIconPath(ThemeDisplay themeDisplay) {
102                    return themeDisplay.getPathThemeImages() + "/common/history.png";
103            }
104    
105            private static final boolean _CATEGORIZABLE = false;
106    
107            private static final boolean _SELECTABLE = false;
108    
109    }