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.CommentMapping.Comment; |
---|
23 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
24 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
25 | |
---|
26 | /** |
---|
27 | * |
---|
28 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
29 | */ |
---|
30 | public abstract class ComponentRegistryImplBase implements ComponentRegistry { |
---|
31 | |
---|
32 | private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryImplBase.class); |
---|
33 | |
---|
34 | @Override |
---|
35 | public List<ComponentDescription> getUsageInComponents(String componentId) throws ComponentRegistryException { |
---|
36 | List<ComponentDescription> result = new ArrayList<ComponentDescription>(); |
---|
37 | List<ComponentDescription> descs = getComponentDescriptions(); |
---|
38 | for (ComponentDescription desc : descs) { |
---|
39 | CMDComponentSpec spec = getMDComponent(desc.getId()); |
---|
40 | if (spec != null && findComponentId(componentId, spec.getCMDComponent())) { |
---|
41 | result.add(desc); |
---|
42 | } |
---|
43 | } |
---|
44 | return result; |
---|
45 | } |
---|
46 | |
---|
47 | @Override |
---|
48 | public List<ProfileDescription> getUsageInProfiles(String componentId) throws ComponentRegistryException { |
---|
49 | List<ProfileDescription> result = new ArrayList<ProfileDescription>(); |
---|
50 | for (ProfileDescription profileDescription : getProfileDescriptions()) { |
---|
51 | CMDComponentSpec profile = getMDProfile(profileDescription.getId()); |
---|
52 | if (profile != null && findComponentId(componentId, profile.getCMDComponent())) { |
---|
53 | result.add(profileDescription); |
---|
54 | } |
---|
55 | } |
---|
56 | return result; |
---|
57 | } |
---|
58 | |
---|
59 | // public List<Comment> getUsageInComments(String componentId) throws ComponentRegistryException { |
---|
60 | // List<Comment> result = new ArrayList<Comment>(); |
---|
61 | // for (Comment comment : getComments()) { |
---|
62 | // CMDComponentSpec myComment = getMDComment(comment.getId()); |
---|
63 | // if (myComment != null && findComponentId(componentId, myComment.getCMDComponent())) { |
---|
64 | // result.add(comment); |
---|
65 | // System.out.println("get unsage in comments : " + myComment + "\n"); |
---|
66 | // } |
---|
67 | // } |
---|
68 | // |
---|
69 | // return result; |
---|
70 | // } |
---|
71 | |
---|
72 | /** |
---|
73 | * |
---|
74 | * @return List of profile descriptions ordered by name ascending, only the ones marked for showing in metadata editor |
---|
75 | * @throws ComponentRegistryException |
---|
76 | */ |
---|
77 | @Override |
---|
78 | public List<ProfileDescription> getProfileDescriptionsForMetadaEditor() throws ComponentRegistryException { |
---|
79 | // TODO: Below can also be done by accepting and passing a parameter in the ProfileDescriptionDao, should have better performance |
---|
80 | |
---|
81 | // Get all profile descriptions |
---|
82 | List<ProfileDescription> descriptionsCollection = getProfileDescriptions(); |
---|
83 | // Filter out ones that do should not be shown for metadata editor |
---|
84 | ArrayList<ProfileDescription> descriptions = new ArrayList<ProfileDescription>(descriptionsCollection.size()); |
---|
85 | for (ProfileDescription profile : descriptionsCollection) { |
---|
86 | if (((ProfileDescription) profile).isShowInEditor()) { |
---|
87 | descriptions.add((ProfileDescription) profile); |
---|
88 | } |
---|
89 | } |
---|
90 | // Return filtered list |
---|
91 | return descriptions; |
---|
92 | } |
---|
93 | |
---|
94 | /* HELPER METHODS */ |
---|
95 | protected static String stripRegistryId(String id) { |
---|
96 | return StringUtils.removeStart(id, ComponentRegistry.REGISTRY_ID); |
---|
97 | } |
---|
98 | |
---|
99 | protected static void enrichSpecHeader(CMDComponentSpec spec, AbstractDescription description) { |
---|
100 | Header header = spec.getHeader(); |
---|
101 | header.setID(description.getId()); |
---|
102 | if (StringUtils.isEmpty(header.getName())) { |
---|
103 | header.setName(description.getName()); |
---|
104 | } |
---|
105 | if (StringUtils.isEmpty(header.getDescription())) { |
---|
106 | header.setDescription(description.getDescription()); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | protected static boolean findComponentId(String componentId, List<CMDComponentType> componentReferences) { |
---|
111 | for (CMDComponentType cmdComponent : componentReferences) { |
---|
112 | if (componentId.equals(cmdComponent.getComponentId())) { |
---|
113 | return true; |
---|
114 | } else if (findComponentId(componentId, cmdComponent.getCMDComponent())) { |
---|
115 | return true; |
---|
116 | } |
---|
117 | } |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | protected static void writeXsd(CMDComponentSpec expandedSpec, OutputStream outputStream) { |
---|
122 | MDMarshaller.generateXsd(expandedSpec, outputStream); |
---|
123 | } |
---|
124 | |
---|
125 | protected static void writeXml(CMDComponentSpec spec, OutputStream outputStream) { |
---|
126 | try { |
---|
127 | MDMarshaller.marshal(spec, outputStream); |
---|
128 | } catch (UnsupportedEncodingException e) { |
---|
129 | LOG.error("Error in encoding: ", e); |
---|
130 | } catch (JAXBException e) { |
---|
131 | LOG.error("Cannot marshall spec: " + spec, e); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | protected void checkStillUsed(String componentId) throws DeleteFailedException, ComponentRegistryException { |
---|
136 | List<ProfileDescription> profiles = getUsageInProfiles(componentId); |
---|
137 | List<ComponentDescription> components = getUsageInComponents(componentId); |
---|
138 | if (!profiles.isEmpty() || !components.isEmpty()) { |
---|
139 | throw new DeleteFailedException(createStillInUseMessage(profiles, components)); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | private String createStillInUseMessage(List<ProfileDescription> profiles, List<ComponentDescription> components) { |
---|
144 | StringBuilder result = new StringBuilder(); |
---|
145 | if (!profiles.isEmpty()) { |
---|
146 | result.append("Still used by the following profiles: \n"); |
---|
147 | for (ProfileDescription profileDescription : profiles) { |
---|
148 | result.append(" - ").append(profileDescription.getName()).append("\n"); |
---|
149 | } |
---|
150 | } |
---|
151 | if (!components.isEmpty()) { |
---|
152 | result.append("Still used by the following components: \n"); |
---|
153 | for (ComponentDescription componentDescription : components) { |
---|
154 | result.append(" - ").append(componentDescription.getName()).append("\n"); |
---|
155 | } |
---|
156 | } |
---|
157 | result.append("Try to change above mentioned references first."); |
---|
158 | return result.toString(); |
---|
159 | } |
---|
160 | } |
---|