-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
BaseOverridingRepositoryDefiner.java
217 lines (172 loc) · 6.99 KB
/
BaseOverridingRepositoryDefiner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.adaptive.media.document.library.internal.repository.registry;
import com.liferay.adaptive.media.processor.AMAsyncProcessor;
import com.liferay.adaptive.media.processor.AMAsyncProcessorLocator;
import com.liferay.exportimport.kernel.lar.ExportImportThreadLocal;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.module.framework.ModuleServiceLifecycle;
import com.liferay.portal.kernel.repository.DocumentRepository;
import com.liferay.portal.kernel.repository.RepositoryConfiguration;
import com.liferay.portal.kernel.repository.RepositoryException;
import com.liferay.portal.kernel.repository.capabilities.Capability;
import com.liferay.portal.kernel.repository.event.RepositoryEventAware;
import com.liferay.portal.kernel.repository.event.RepositoryEventType;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.repository.model.FileVersion;
import com.liferay.portal.kernel.repository.registry.CapabilityRegistry;
import com.liferay.portal.kernel.repository.registry.RepositoryDefiner;
import com.liferay.portal.kernel.repository.registry.RepositoryEventRegistry;
import com.liferay.portal.kernel.repository.registry.RepositoryFactoryRegistry;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.repository.registry.RepositoryClassDefinition;
import com.liferay.portal.repository.registry.RepositoryClassDefinitionCatalog;
import com.liferay.portal.repository.registry.RepositoryClassDefinitionCatalogUtil;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.osgi.service.component.annotations.Reference;
/**
* @author Adolfo Pérez
* @author Sergio González
*/
public abstract class BaseOverridingRepositoryDefiner
implements RepositoryDefiner {
@Override
public String getClassName() {
return _overridenRepositoryDefiner.getClassName();
}
@Override
public RepositoryConfiguration getRepositoryConfiguration() {
return _overridenRepositoryDefiner.getRepositoryConfiguration();
}
@Override
public String getRepositoryTypeLabel(Locale locale) {
return _overridenRepositoryDefiner.getRepositoryTypeLabel(locale);
}
@Override
public boolean isExternalRepository() {
return _overridenRepositoryDefiner.isExternalRepository();
}
@Override
public void registerCapabilities(
CapabilityRegistry<DocumentRepository> capabilityRegistry) {
_overridenRepositoryDefiner.registerCapabilities(capabilityRegistry);
capabilityRegistry.addSupportedCapability(
AdaptiveMediaCapabiliy.class, new AdaptiveMediaCapabiliy());
}
@Override
public void registerRepositoryEventListeners(
RepositoryEventRegistry repositoryEventRegistry) {
_overridenRepositoryDefiner.registerRepositoryEventListeners(
repositoryEventRegistry);
}
@Override
public void registerRepositoryFactory(
RepositoryFactoryRegistry repositoryFactoryRegistry) {
_overridenRepositoryDefiner.registerRepositoryFactory(
repositoryFactoryRegistry);
}
@Reference(unbind = "-")
public void setAMAsyncProcessorLocator(
AMAsyncProcessorLocator amAsyncProcessorLocator) {
_amAsyncProcessorLocator = amAsyncProcessorLocator;
}
@Reference(target = ModuleServiceLifecycle.PORTAL_INITIALIZED, unbind = "-")
public void setModuleServiceLifecycle(
ModuleServiceLifecycle moduleServiceLifecycle) {
}
protected void initializeOverridenRepositoryDefiner(
RepositoryDefiner repositoryDefiner) {
_overridenRepositoryDefiner = repositoryDefiner;
}
protected void restoreOverridenRepositoryDefiner(String className) {
Map<String, RepositoryClassDefinition> repositoryClassDefinitions =
_getFieldValue("_repositoryClassDefinitions");
RepositoryClassDefinition repositoryClassDefinition =
RepositoryClassDefinition.fromRepositoryDefiner(
_overridenRepositoryDefiner);
repositoryClassDefinitions.put(className, repositoryClassDefinition);
}
private void _deleteAdaptiveMedia(FileEntry fileEntry) {
if (ExportImportThreadLocal.isImportInProcess()) {
return;
}
try {
AMAsyncProcessor<FileVersion, ?> amAsyncProcessor =
_amAsyncProcessorLocator.locateForClass(FileVersion.class);
List<FileVersion> fileVersions = fileEntry.getFileVersions(
WorkflowConstants.STATUS_ANY);
for (FileVersion fileVersion : fileVersions) {
amAsyncProcessor.triggerCleanUp(
fileVersion,
String.valueOf(fileVersion.getFileVersionId()));
}
}
catch (PortalException pe) {
throw new RuntimeException(pe);
}
}
private <T> T _getFieldValue(String fieldName) {
try {
RepositoryClassDefinitionCatalog repositoryClassDefinitionCatalog =
RepositoryClassDefinitionCatalogUtil.
getRepositoryClassDefinitionCatalog();
Class<? extends RepositoryClassDefinitionCatalog> clazz =
repositoryClassDefinitionCatalog.getClass();
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return (T)field.get(repositoryClassDefinitionCatalog);
}
catch (IllegalAccessException | NoSuchFieldException |
SecurityException e) {
throw new RepositoryException(e);
}
}
private void _updateAdaptiveMedia(FileEntry fileEntry) {
if (ExportImportThreadLocal.isImportInProcess()) {
return;
}
try {
AMAsyncProcessor<FileVersion, ?> amAsyncProcessor =
_amAsyncProcessorLocator.locateForClass(FileVersion.class);
FileVersion latestFileVersion = fileEntry.getLatestFileVersion(
true);
amAsyncProcessor.triggerProcess(
latestFileVersion,
String.valueOf(latestFileVersion.getFileVersionId()));
}
catch (PortalException pe) {
throw new RuntimeException(pe);
}
}
private AMAsyncProcessorLocator _amAsyncProcessorLocator;
private RepositoryDefiner _overridenRepositoryDefiner;
private class AdaptiveMediaCapabiliy
implements Capability, RepositoryEventAware {
public void registerRepositoryEventListeners(
RepositoryEventRegistry repositoryEventRegistry) {
repositoryEventRegistry.registerRepositoryEventListener(
RepositoryEventType.Add.class, FileEntry.class,
BaseOverridingRepositoryDefiner.this::_updateAdaptiveMedia);
repositoryEventRegistry.registerRepositoryEventListener(
RepositoryEventType.Delete.class, FileEntry.class,
BaseOverridingRepositoryDefiner.this::_deleteAdaptiveMedia);
repositoryEventRegistry.registerRepositoryEventListener(
RepositoryEventType.Update.class, FileEntry.class,
BaseOverridingRepositoryDefiner.this::_updateAdaptiveMedia);
}
}
}