001
014
015 package com.liferay.portlet.documentlibrary.util;
016
017 import com.liferay.portal.kernel.lar.PortletDataContext;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.repository.model.FileEntry;
021 import com.liferay.portal.kernel.repository.model.FileVersion;
022 import com.liferay.portal.kernel.util.InstancePool;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.kernel.util.PropsUtil;
025 import com.liferay.portal.kernel.xml.Element;
026 import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
027 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028
029 import java.util.List;
030 import java.util.concurrent.CopyOnWriteArrayList;
031
032
035 public class DLProcessorRegistryImpl implements DLProcessorRegistry {
036
037 public void cleanUp(FileEntry fileEntry) {
038 if (!DLProcessorThreadLocal.isEnabled()) {
039 return;
040 }
041
042 for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
043 DLProcessor dlProcessor = (DLProcessor)InstancePool.get(
044 dlProcessorClassName);
045
046 dlProcessor.cleanUp(fileEntry);
047 }
048
049 for (DLProcessor dlProcessor : _dlProcessors) {
050 dlProcessor.cleanUp(fileEntry);
051 }
052 }
053
054 public void cleanUp(FileVersion fileVersion) {
055 if (!DLProcessorThreadLocal.isEnabled()) {
056 return;
057 }
058
059 for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
060 DLProcessor dlProcessor = (DLProcessor)InstancePool.get(
061 dlProcessorClassName);
062
063 dlProcessor.cleanUp(fileVersion);
064 }
065
066 for (DLProcessor dlProcessor : _dlProcessors) {
067 dlProcessor.cleanUp(fileVersion);
068 }
069 }
070
071 public void exportGeneratedFiles(
072 PortletDataContext portletDataContext, FileEntry fileEntry,
073 Element fileEntryElement)
074 throws Exception {
075
076 if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
077 return;
078 }
079
080 FileVersion latestFileVersion = _getLatestFileVersion(fileEntry);
081
082 if (latestFileVersion == null) {
083 return;
084 }
085
086 for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
087 DLProcessor dlProcessor = (DLProcessor)InstancePool.get(
088 dlProcessorClassName);
089
090 if (dlProcessor.isSupported(latestFileVersion)) {
091 dlProcessor.exportGeneratedFiles(
092 portletDataContext, fileEntry, fileEntryElement);
093 }
094 }
095
096 for (DLProcessor dlProcessor : _dlProcessors) {
097 if (dlProcessor.isSupported(latestFileVersion)) {
098 dlProcessor.exportGeneratedFiles(
099 portletDataContext, fileEntry, fileEntryElement);
100 }
101 }
102 }
103
104 public void importGeneratedFiles(
105 PortletDataContext portletDataContext, FileEntry fileEntry,
106 FileEntry importedFileEntry, Element fileEntryElement)
107 throws Exception {
108
109 if ((importedFileEntry == null) || (importedFileEntry.getSize() == 0)) {
110 return;
111 }
112
113 FileVersion fileVersion = importedFileEntry.getFileVersion();
114
115 if (fileVersion == null) {
116 return;
117 }
118
119 for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
120 DLProcessor dlProcessor = (DLProcessor)InstancePool.get(
121 dlProcessorClassName);
122
123 if (dlProcessor.isSupported(fileVersion)) {
124 dlProcessor.importGeneratedFiles(
125 portletDataContext, fileEntry, importedFileEntry,
126 fileEntryElement);
127 }
128 }
129
130 for (DLProcessor dlProcessor : _dlProcessors) {
131 if (dlProcessor.isSupported(fileVersion)) {
132 dlProcessor.importGeneratedFiles(
133 portletDataContext, fileEntry, importedFileEntry,
134 fileEntryElement);
135 }
136 }
137 }
138
139 public void register(DLProcessor dlProcessor) {
140 _dlProcessors.add(dlProcessor);
141 }
142
143 public void trigger(FileEntry fileEntry) {
144 if (!DLProcessorThreadLocal.isEnabled()) {
145 return;
146 }
147
148 if ((fileEntry == null) || (fileEntry.getSize() == 0)) {
149 return;
150 }
151
152 FileVersion latestFileVersion = _getLatestFileVersion(fileEntry);
153
154 if (latestFileVersion == null) {
155 return;
156 }
157
158 for (String dlProcessorClassName : _DL_FILE_ENTRY_PROCESSORS) {
159 DLProcessor dlProcessor = (DLProcessor)InstancePool.get(
160 dlProcessorClassName);
161
162 if (dlProcessor.isSupported(latestFileVersion)) {
163 dlProcessor.trigger(latestFileVersion);
164 }
165 }
166
167 for (DLProcessor dlProcessor : _dlProcessors) {
168 if (dlProcessor.isSupported(latestFileVersion)) {
169 dlProcessor.trigger(latestFileVersion);
170 }
171 }
172 }
173
174 public void unregister(DLProcessor dlProcessor) {
175 _dlProcessors.remove(dlProcessor);
176 }
177
178 private FileVersion _getLatestFileVersion(FileEntry fileEntry) {
179 FileVersion latestFileVersion = null;
180
181 try {
182 if (fileEntry.getModel() instanceof DLFileEntry) {
183 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
184
185 latestFileVersion = new LiferayFileVersion(
186 dlFileEntry.getLatestFileVersion(false));
187 }
188 else {
189 latestFileVersion = fileEntry.getLatestFileVersion();
190 }
191
192 return latestFileVersion;
193 }
194 catch (Exception e) {
195 _log.error(e, e);
196
197 return null;
198 }
199 }
200
201 private static final String[] _DL_FILE_ENTRY_PROCESSORS =
202 PropsUtil.getArray(PropsKeys.DL_FILE_ENTRY_PROCESSORS);
203
204 private static Log _log = LogFactoryUtil.getLog(
205 DLProcessorRegistryImpl.class);
206
207 private List<DLProcessor> _dlProcessors =
208 new CopyOnWriteArrayList<DLProcessor>();
209
210 }