1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.security.Principal; |
---|
5 | |
---|
6 | import javax.xml.bind.JAXBException; |
---|
7 | |
---|
8 | import org.apache.commons.io.IOUtils; |
---|
9 | import org.slf4j.Logger; |
---|
10 | import org.slf4j.LoggerFactory; |
---|
11 | import org.springframework.dao.DataAccessException; |
---|
12 | |
---|
13 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
14 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
15 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
16 | import clarin.cmdi.componentregistry.DeleteFailedException; |
---|
17 | import clarin.cmdi.componentregistry.ItemNotFoundException; |
---|
18 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
19 | import clarin.cmdi.componentregistry.UserCredentials; |
---|
20 | import clarin.cmdi.componentregistry.UserUnauthorizedException; |
---|
21 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
22 | import clarin.cmdi.componentregistry.frontend.CMDItemInfo; |
---|
23 | import clarin.cmdi.componentregistry.frontend.SubmitFailedException; |
---|
24 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
25 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
26 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
27 | import clarin.cmdi.componentregistry.persistence.ComponentDao; |
---|
28 | |
---|
29 | /** |
---|
30 | * |
---|
31 | * @author george.georgovassilis@mpi.nl |
---|
32 | * |
---|
33 | */ |
---|
34 | public class AdminRegistry { |
---|
35 | |
---|
36 | private final static Logger LOG = LoggerFactory |
---|
37 | .getLogger(AdminRegistry.class); |
---|
38 | private ComponentRegistryFactory componentRegistryFactory; |
---|
39 | private ComponentDao componentDao; |
---|
40 | private MDMarshaller marshaller; |
---|
41 | |
---|
42 | public void setComponentRegistryFactory( |
---|
43 | ComponentRegistryFactory componentRegistryFactory) { |
---|
44 | this.componentRegistryFactory = componentRegistryFactory; |
---|
45 | } |
---|
46 | |
---|
47 | public void setComponentDao(ComponentDao componentDao) { |
---|
48 | this.componentDao = componentDao; |
---|
49 | } |
---|
50 | |
---|
51 | public void setMarshaller(MDMarshaller marshaller) { |
---|
52 | this.marshaller = marshaller; |
---|
53 | } |
---|
54 | |
---|
55 | public void submitFile(CMDItemInfo info, Principal userPrincipal) |
---|
56 | throws SubmitFailedException, UserUnauthorizedException { |
---|
57 | if (userPrincipal == null) { |
---|
58 | LOG.info("Null user principal, nothings is submitted."); |
---|
59 | return; |
---|
60 | } |
---|
61 | |
---|
62 | try { |
---|
63 | BaseDescription originalDescription = info.getDataNode() |
---|
64 | .getDescription(); |
---|
65 | BaseDescription description = null; |
---|
66 | CMDComponentSpec spec = null; |
---|
67 | if (originalDescription.isProfile()) { |
---|
68 | description = marshaller.unmarshal(ProfileDescription.class, |
---|
69 | IOUtils.toInputStream(info.getDescription(), "UTF-8"), |
---|
70 | null); |
---|
71 | } else { |
---|
72 | description = marshaller.unmarshal(ComponentDescription.class, |
---|
73 | IOUtils.toInputStream(info.getDescription(), "UTF-8"), |
---|
74 | null); |
---|
75 | } |
---|
76 | spec = marshaller.unmarshal(CMDComponentSpec.class, |
---|
77 | IOUtils.toInputStream(info.getContent(), "UTF-8"), null); |
---|
78 | checkId(originalDescription.getId(), description.getId()); |
---|
79 | ComponentRegistry cr = this.getRegistry(new UserCredentials(userPrincipal)); |
---|
80 | int result = cr.update(description, spec, info.isForceUpdate()); |
---|
81 | if (result < 0) { |
---|
82 | throw new SubmitFailedException( |
---|
83 | "Problem occured while registering, please check the tomcat logs for errors."); |
---|
84 | } |
---|
85 | } catch (JAXBException e) { |
---|
86 | throw new SubmitFailedException(e); |
---|
87 | } catch (IOException e) { |
---|
88 | throw new SubmitFailedException(e); |
---|
89 | } catch (ItemNotFoundException e) { |
---|
90 | throw new SubmitFailedException(e); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | private void checkId(String id, String id2) throws SubmitFailedException { |
---|
95 | if (id == null || id2 == null || !id.equals(id2)) { |
---|
96 | throw new SubmitFailedException( |
---|
97 | "Id's do not match up, you cannot edit id's: id1=" + id |
---|
98 | + ", id2=" + id2); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | public void undelete(CMDItemInfo info) throws SubmitFailedException { |
---|
103 | BaseDescription desc = info.getDataNode().getDescription(); |
---|
104 | try { |
---|
105 | componentDao.setDeleted(desc, false); |
---|
106 | } catch (DataAccessException e) { |
---|
107 | throw new SubmitFailedException("Undelete failed", e); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | public void delete(CMDItemInfo info, Principal userPrincipal) |
---|
112 | throws SubmitFailedException { |
---|
113 | String id = info.getName(); |
---|
114 | BaseDescription desc = info.getDataNode().getDescription(); |
---|
115 | try { |
---|
116 | this.deleteFromRegistry(userPrincipal, desc, info); |
---|
117 | LOG.info("Deleted item: " + id); |
---|
118 | } catch (IOException e) { |
---|
119 | throw new SubmitFailedException(e); |
---|
120 | } catch (UserUnauthorizedException e) { |
---|
121 | throw new SubmitFailedException(e); |
---|
122 | } catch (DeleteFailedException e) { |
---|
123 | throw new SubmitFailedException(e); |
---|
124 | } catch (ComponentRegistryException e) { |
---|
125 | throw new SubmitFailedException(e); |
---|
126 | } catch (ItemNotFoundException e) { |
---|
127 | throw new SubmitFailedException(e); |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | private void deleteFromRegistry(Principal userPrincipal, |
---|
133 | BaseDescription desc, CMDItemInfo info) throws IOException, |
---|
134 | UserUnauthorizedException, ComponentRegistryException, ItemNotFoundException { |
---|
135 | |
---|
136 | if (userPrincipal == null) { |
---|
137 | LOG.info("Bnull user principal, nothing is deleted"); |
---|
138 | return; |
---|
139 | } |
---|
140 | |
---|
141 | ComponentRegistry registry = this.getRegistry(new UserCredentials(userPrincipal)); |
---|
142 | LOG.info("Deleting item: " + desc); |
---|
143 | if (desc.isProfile()) { |
---|
144 | registry.deleteMDProfile(desc.getId()); |
---|
145 | } else { |
---|
146 | registry.deleteMDComponent(desc.getId(), info.isForceUpdate()); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | private ComponentRegistry getRegistry(UserCredentials credentials) throws UserUnauthorizedException{ |
---|
151 | return componentRegistryFactory.getBaseRegistry(credentials); |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|