001
014
015 package com.liferay.portlet.documentlibrary.asset;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.language.LanguageUtil;
020 import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
021 import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
022 import com.liferay.portal.kernel.repository.model.FileEntry;
023 import com.liferay.portal.kernel.repository.model.FileVersion;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.ListUtil;
026 import com.liferay.portal.kernel.util.Tuple;
027 import com.liferay.portal.security.permission.ActionKeys;
028 import com.liferay.portal.security.permission.PermissionChecker;
029 import com.liferay.portal.theme.ThemeDisplay;
030 import com.liferay.portal.util.PortletKeys;
031 import com.liferay.portal.util.WebKeys;
032 import com.liferay.portlet.PortletURLFactoryUtil;
033 import com.liferay.portlet.asset.model.AssetRenderer;
034 import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
035 import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
036 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
037 import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
038 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
039 import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
040 import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeServiceUtil;
041 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
042 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryTypePermission;
043 import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
044 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
045
046 import java.util.ArrayList;
047 import java.util.HashMap;
048 import java.util.List;
049 import java.util.Locale;
050 import java.util.Map;
051
052 import javax.portlet.PortletRequest;
053 import javax.portlet.PortletURL;
054
055
061 public class DLFileEntryAssetRendererFactory extends BaseAssetRendererFactory {
062
063 public static final String TYPE = "document";
064
065 @Override
066 public AssetRenderer getAssetRenderer(long classPK, int type)
067 throws PortalException, SystemException {
068
069 FileEntry fileEntry = null;
070 FileVersion fileVersion = null;
071
072 if (type == TYPE_LATEST) {
073 fileVersion = DLAppLocalServiceUtil.getFileVersion(classPK);
074
075 fileEntry = fileVersion.getFileEntry();
076 }
077 else {
078 fileEntry = DLAppLocalServiceUtil.getFileEntry(classPK);
079
080 fileVersion = fileEntry.getFileVersion();
081 }
082
083 DLFileEntryAssetRenderer dlFileEntryAssetRenderer =
084 new DLFileEntryAssetRenderer(fileEntry, fileVersion);
085
086 dlFileEntryAssetRenderer.setAssetRendererType(type);
087
088 return dlFileEntryAssetRenderer;
089 }
090
091 @Override
092 public String getClassName() {
093 return DLFileEntry.class.getName();
094 }
095
096 @Override
097 public List<Tuple> getClassTypeFieldNames(
098 long classTypeId, Locale locale, int start, int end)
099 throws Exception {
100
101 List<Tuple> classTypeFieldNames = new ArrayList<Tuple>();
102
103 DLFileEntryType dlFileEntryType =
104 DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(classTypeId);
105
106 List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
107
108 for (DDMStructure ddmStructure : ddmStructures) {
109 classTypeFieldNames.addAll(
110 getDDMStructureFieldNames(ddmStructure, locale));
111 }
112
113 return ListUtil.subList(classTypeFieldNames, start, end);
114 }
115
116 @Override
117 public int getClassTypeFieldNamesCount(long classTypeId, Locale locale)
118 throws Exception {
119
120 List<Tuple> classTypeFieldNames = new ArrayList<Tuple>();
121
122 DLFileEntryType dlFileEntryType =
123 DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(classTypeId);
124
125 List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
126
127 for (DDMStructure ddmStructure : ddmStructures) {
128 classTypeFieldNames.addAll(
129 getDDMStructureFieldNames(ddmStructure, locale));
130 }
131
132 return classTypeFieldNames.size();
133 }
134
135 @Override
136 public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
137 throws Exception {
138
139 Map<Long, String> classTypes = new HashMap<Long, String>();
140
141 List<DLFileEntryType> dlFileEntryTypes =
142 DLFileEntryTypeServiceUtil.getFileEntryTypes(groupIds);
143
144 for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
145 classTypes.put(
146 dlFileEntryType.getFileEntryTypeId(),
147 dlFileEntryType.getName(locale));
148 }
149
150 return classTypes;
151 }
152
153 @Override
154 public String getType() {
155 return TYPE;
156 }
157
158 @Override
159 public String getTypeName(Locale locale, boolean hasSubtypes) {
160 if (hasSubtypes) {
161 return LanguageUtil.get(locale, "basic-document");
162 }
163
164 return super.getTypeName(locale, hasSubtypes);
165 }
166
167 @Override
168 public PortletURL getURLAdd(
169 LiferayPortletRequest liferayPortletRequest,
170 LiferayPortletResponse liferayPortletResponse)
171 throws PortalException, SystemException {
172
173 ThemeDisplay themeDisplay =
174 (ThemeDisplay)liferayPortletRequest.getAttribute(
175 WebKeys.THEME_DISPLAY);
176
177 if (!DLPermission.contains(
178 themeDisplay.getPermissionChecker(),
179 themeDisplay.getScopeGroupId(), ActionKeys.ADD_DOCUMENT)) {
180
181 return null;
182 }
183
184 long classTypeId = GetterUtil.getLong(
185 liferayPortletRequest.getAttribute(
186 WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
187
188 if ((classTypeId > 0) &&
189 !DLFileEntryTypePermission.contains(
190 themeDisplay.getPermissionChecker(), classTypeId,
191 ActionKeys.VIEW)) {
192
193 return null;
194 }
195
196 PortletURL portletURL = PortletURLFactoryUtil.create(
197 liferayPortletRequest, PortletKeys.DOCUMENT_LIBRARY,
198 getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
199
200 portletURL.setParameter(
201 "struts_action", "/document_library/edit_file_entry");
202 portletURL.setParameter(
203 "folderId",
204 String.valueOf(
205 AssetPublisherUtil.getRecentFolderId(
206 liferayPortletRequest, getClassName())));
207
208 return portletURL;
209 }
210
211 @Override
212 public boolean hasPermission(
213 PermissionChecker permissionChecker, long classPK, String actionId)
214 throws Exception {
215
216 return DLFileEntryPermission.contains(
217 permissionChecker, classPK, actionId);
218 }
219
220 @Override
221 public boolean isLinkable() {
222 return _LINKABLE;
223 }
224
225 @Override
226 protected String getIconPath(ThemeDisplay themeDisplay) {
227 return themeDisplay.getPathThemeImages() + "/common/clip.png";
228 }
229
230 private static final boolean _LINKABLE = true;
231
232 }