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.ItemNotFoundException; |
---|
17 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
18 | import clarin.cmdi.componentregistry.UserUnauthorizedException; |
---|
19 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
20 | import clarin.cmdi.componentregistry.components.CMDComponentSpec.Header; |
---|
21 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
22 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
23 | |
---|
24 | /** |
---|
25 | * |
---|
26 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
27 | */ |
---|
28 | public abstract class ComponentRegistryImplBase implements ComponentRegistry { |
---|
29 | |
---|
30 | private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryImplBase.class); |
---|
31 | |
---|
32 | protected abstract MDMarshaller getMarshaller(); |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * |
---|
38 | * @return List of profile descriptions ordered by name ascending, only the |
---|
39 | * ones marked for showing in metadata editor |
---|
40 | * @throws ComponentRegistryException |
---|
41 | */ |
---|
42 | @Override |
---|
43 | public List<ProfileDescription> getProfileDescriptionsForMetadaEditor() throws ComponentRegistryException { |
---|
44 | // TODO: Below can also be done by accepting and passing a parameter in the ProfileDescriptionDaoImpl, should have better performance |
---|
45 | |
---|
46 | // Get all profile descriptions |
---|
47 | List<String> descriptionsCollectionIds = getAllNonDeletedProfileIds(); |
---|
48 | // Filter out ones that do should not be shown for metadata editor |
---|
49 | ArrayList<ProfileDescription> descriptions = new ArrayList<ProfileDescription>(); |
---|
50 | for (String id : descriptionsCollectionIds) { |
---|
51 | try { |
---|
52 | ProfileDescription profile = getProfileDescriptionAccessControlled(id); |
---|
53 | if (profile.isShowInEditor()) { |
---|
54 | descriptions.add(profile); |
---|
55 | } |
---|
56 | } catch (UserUnauthorizedException e) { |
---|
57 | } catch (ItemNotFoundException e) { |
---|
58 | } |
---|
59 | |
---|
60 | } |
---|
61 | // Return filtered list |
---|
62 | return descriptions; |
---|
63 | } |
---|
64 | |
---|
65 | /* HELPER METHODS */ |
---|
66 | protected static String stripRegistryId(String id) { |
---|
67 | return StringUtils.removeStart(id, ComponentRegistry.REGISTRY_ID); |
---|
68 | } |
---|
69 | |
---|
70 | protected static void enrichSpecHeader(CMDComponentSpec spec, BaseDescription description) { |
---|
71 | Header header = spec.getHeader(); |
---|
72 | header.setID(description.getId()); |
---|
73 | if (StringUtils.isEmpty(header.getName())) { |
---|
74 | header.setName(description.getName()); |
---|
75 | } |
---|
76 | if (StringUtils.isEmpty(header.getDescription())) { |
---|
77 | header.setDescription(description.getDescription()); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | protected void writeXsd(CMDComponentSpec expandedSpec, OutputStream outputStream) { |
---|
86 | getMarshaller().generateXsd(expandedSpec, outputStream); |
---|
87 | } |
---|
88 | |
---|
89 | protected void writeXml(CMDComponentSpec spec, OutputStream outputStream) { |
---|
90 | try { |
---|
91 | getMarshaller().marshal(spec, outputStream); |
---|
92 | } catch (UnsupportedEncodingException e) { |
---|
93 | LOG.error("Error in encoding: ", e); |
---|
94 | } catch (JAXBException e) { |
---|
95 | LOG.error("Cannot marshall spec: " + spec, e); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|