1 | package clarin.cmdi.componentregistry.impl; |
---|
2 | |
---|
3 | import java.io.OutputStream; |
---|
4 | import java.io.UnsupportedEncodingException; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import javax.xml.bind.JAXBException; |
---|
9 | |
---|
10 | import org.apache.commons.lang.StringUtils; |
---|
11 | import org.slf4j.Logger; |
---|
12 | import org.slf4j.LoggerFactory; |
---|
13 | |
---|
14 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
15 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
16 | import clarin.cmdi.componentregistry.DeleteFailedException; |
---|
17 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
18 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
19 | import clarin.cmdi.componentregistry.components.CMDComponentType; |
---|
20 | import clarin.cmdi.componentregistry.components.CMDComponentSpec.Header; |
---|
21 | import clarin.cmdi.componentregistry.model.AbstractDescription; |
---|
22 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
23 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
24 | |
---|
25 | /** |
---|
26 | * |
---|
27 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
28 | */ |
---|
29 | public abstract class ComponentRegistryImplBase implements ComponentRegistry { |
---|
30 | |
---|
31 | private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryImplBase.class); |
---|
32 | |
---|
33 | @Override |
---|
34 | public List<ComponentDescription> getUsageInComponents(String componentId) throws ComponentRegistryException { |
---|
35 | List<ComponentDescription> result = new ArrayList<ComponentDescription>(); |
---|
36 | List<ComponentDescription> descs = getComponentDescriptions(); |
---|
37 | for (ComponentDescription desc : descs) { |
---|
38 | CMDComponentSpec spec = getMDComponent(desc.getId()); |
---|
39 | if (spec != null && findComponentId(componentId, spec.getCMDComponent())) { |
---|
40 | result.add(desc); |
---|
41 | } |
---|
42 | } |
---|
43 | return result; |
---|
44 | } |
---|
45 | |
---|
46 | @Override |
---|
47 | public List<ProfileDescription> getUsageInProfiles(String componentId) throws ComponentRegistryException { |
---|
48 | List<ProfileDescription> result = new ArrayList<ProfileDescription>(); |
---|
49 | for (ProfileDescription profileDescription : getProfileDescriptions()) { |
---|
50 | CMDComponentSpec profile = getMDProfile(profileDescription.getId()); |
---|
51 | if (profile != null && findComponentId(componentId, profile.getCMDComponent())) { |
---|
52 | result.add(profileDescription); |
---|
53 | } |
---|
54 | } |
---|
55 | return result; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * |
---|
60 | * @return List of profile descriptions ordered by name ascending, only the ones marked for showing in metadata editor |
---|
61 | * @throws ComponentRegistryException |
---|
62 | */ |
---|
63 | @Override |
---|
64 | public List<ProfileDescription> getProfileDescriptionsForMetadaEditor() throws ComponentRegistryException { |
---|
65 | // TODO: Below can also be done by accepting and passing a parameter in the ProfileDescriptionDao, should have better performance |
---|
66 | |
---|
67 | // Get all profile descriptions |
---|
68 | List<ProfileDescription> descriptionsCollection = getProfileDescriptions(); |
---|
69 | // Filter out ones that do should not be shown for metadata editor |
---|
70 | ArrayList<ProfileDescription> descriptions = new ArrayList<ProfileDescription>(descriptionsCollection.size()); |
---|
71 | for (ProfileDescription profile : descriptionsCollection) { |
---|
72 | if (((ProfileDescription) profile).isShowInEditor()) { |
---|
73 | descriptions.add((ProfileDescription) profile); |
---|
74 | } |
---|
75 | } |
---|
76 | // Return filtered list |
---|
77 | return descriptions; |
---|
78 | } |
---|
79 | |
---|
80 | /* HELPER METHODS */ |
---|
81 | protected static String stripRegistryId(String id) { |
---|
82 | return StringUtils.removeStart(id, ComponentRegistry.REGISTRY_ID); |
---|
83 | } |
---|
84 | |
---|
85 | protected static void enrichSpecHeader(CMDComponentSpec spec, AbstractDescription description) { |
---|
86 | Header header = spec.getHeader(); |
---|
87 | header.setID(description.getId()); |
---|
88 | if (StringUtils.isEmpty(header.getName())) { |
---|
89 | header.setName(description.getName()); |
---|
90 | } |
---|
91 | if (StringUtils.isEmpty(header.getDescription())) { |
---|
92 | header.setDescription(description.getDescription()); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | protected static boolean findComponentId(String componentId, List<CMDComponentType> componentReferences) { |
---|
97 | for (CMDComponentType cmdComponent : componentReferences) { |
---|
98 | if (componentId.equals(cmdComponent.getComponentId())) { |
---|
99 | return true; |
---|
100 | } else if (findComponentId(componentId, cmdComponent.getCMDComponent())) { |
---|
101 | return true; |
---|
102 | } |
---|
103 | } |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | protected static void writeXsd(CMDComponentSpec expandedSpec, OutputStream outputStream) { |
---|
108 | MDMarshaller.generateXsd(expandedSpec, outputStream); |
---|
109 | } |
---|
110 | |
---|
111 | protected static void writeXml(CMDComponentSpec spec, OutputStream outputStream) { |
---|
112 | try { |
---|
113 | MDMarshaller.marshal(spec, outputStream); |
---|
114 | } catch (UnsupportedEncodingException e) { |
---|
115 | LOG.error("Error in encoding: ", e); |
---|
116 | } catch (JAXBException e) { |
---|
117 | LOG.error("Cannot marshall spec: " + spec, e); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | protected void checkStillUsed(String componentId) throws DeleteFailedException, ComponentRegistryException { |
---|
122 | for (ProfileDescription profileDescription : getProfileDescriptions()) { |
---|
123 | CMDComponentSpec spec = getMDProfile(profileDescription.getId()); |
---|
124 | if (spec != null && findComponentId(componentId, spec.getCMDComponent())) { |
---|
125 | // Profile match - throw |
---|
126 | throw new DeleteFailedException("Component is still in use by other components or profiles. Request component usage for details."); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | for (ComponentDescription desc : getComponentDescriptions()) { |
---|
131 | CMDComponentSpec spec = getMDComponent(desc.getId()); |
---|
132 | if (spec != null && findComponentId(componentId, spec.getCMDComponent())) { |
---|
133 | // Component match -> throw |
---|
134 | throw new DeleteFailedException("Component is still in use by one or more other components. Request component usage for details."); |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|