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.ComponentStatus; |
---|
17 | import clarin.cmdi.componentregistry.DeleteFailedException; |
---|
18 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
19 | import clarin.cmdi.componentregistry.OwnerUser; |
---|
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 { |
---|
57 | try { |
---|
58 | BaseDescription originalDescription = info.getDataNode() |
---|
59 | .getDescription(); |
---|
60 | BaseDescription description = null; |
---|
61 | CMDComponentSpec spec = null; |
---|
62 | if (originalDescription.isProfile()) { |
---|
63 | description = marshaller.unmarshal(ProfileDescription.class, |
---|
64 | IOUtils.toInputStream(info.getDescription(), "UTF-8"), |
---|
65 | null); |
---|
66 | } else { |
---|
67 | description = marshaller.unmarshal(ComponentDescription.class, |
---|
68 | IOUtils.toInputStream(info.getDescription(), "UTF-8"), |
---|
69 | null); |
---|
70 | } |
---|
71 | spec = marshaller.unmarshal(CMDComponentSpec.class, |
---|
72 | IOUtils.toInputStream(info.getContent(), "UTF-8"), null); |
---|
73 | checkId(originalDescription.getId(), description.getId()); |
---|
74 | |
---|
75 | int result = getRegistry(userPrincipal, originalDescription, info) |
---|
76 | .update(description, spec, userPrincipal, |
---|
77 | info.isForceUpdate()); |
---|
78 | if (result < 0) { |
---|
79 | throw new SubmitFailedException( |
---|
80 | "Problem occured while registering, please check the tomcat logs for errors."); |
---|
81 | } |
---|
82 | } catch (JAXBException e) { |
---|
83 | throw new SubmitFailedException(e); |
---|
84 | } catch (IOException e) { |
---|
85 | throw new SubmitFailedException(e); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | private void checkId(String id, String id2) throws SubmitFailedException { |
---|
90 | if (id == null || id2 == null || !id.equals(id2)) { |
---|
91 | throw new SubmitFailedException( |
---|
92 | "Id's do not match up, you cannot edit id's: id1=" + id |
---|
93 | + ", id2=" + id2); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | public void undelete(CMDItemInfo info) throws SubmitFailedException { |
---|
98 | BaseDescription desc = info.getDataNode().getDescription(); |
---|
99 | try { |
---|
100 | componentDao.setDeleted(desc, false); |
---|
101 | } catch (DataAccessException e) { |
---|
102 | throw new SubmitFailedException("Undelete failed", e); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | public void delete(CMDItemInfo info, Principal userPrincipal) |
---|
107 | throws SubmitFailedException { |
---|
108 | String id = info.getName(); |
---|
109 | BaseDescription desc = info.getDataNode().getDescription(); |
---|
110 | try { |
---|
111 | deleteFromRegistry(userPrincipal, desc, info); |
---|
112 | LOG.info("Deleted item: " + id); |
---|
113 | } catch (IOException e) { |
---|
114 | throw new SubmitFailedException(e); |
---|
115 | } catch (UserUnauthorizedException e) { |
---|
116 | throw new SubmitFailedException(e); |
---|
117 | } catch (DeleteFailedException e) { |
---|
118 | throw new SubmitFailedException(e); |
---|
119 | } catch (ComponentRegistryException e) { |
---|
120 | throw new SubmitFailedException(e); |
---|
121 | } |
---|
122 | |
---|
123 | } |
---|
124 | |
---|
125 | private void deleteFromRegistry(Principal userPrincipal, |
---|
126 | BaseDescription desc, CMDItemInfo info) throws IOException, |
---|
127 | UserUnauthorizedException, ComponentRegistryException { |
---|
128 | ComponentRegistry registry = getRegistry(userPrincipal, desc, info); |
---|
129 | LOG.info("Deleting item: " + desc); |
---|
130 | if (desc.isProfile()) { |
---|
131 | registry.deleteMDProfile(desc.getId(), userPrincipal); |
---|
132 | } else { |
---|
133 | registry.deleteMDComponent(desc.getId(), userPrincipal, |
---|
134 | info.isForceUpdate()); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | private ComponentRegistry getRegistry(Principal userPrincipal, |
---|
139 | BaseDescription desc, CMDItemInfo info) { |
---|
140 | ComponentRegistry registry = componentRegistryFactory |
---|
141 | .getPublicRegistry(); |
---|
142 | // TODO: More generic check |
---|
143 | if (info.getStatus() == ComponentStatus.PRIVATE /* |
---|
144 | * || info.getStatus() |
---|
145 | * == ComponentStatus. |
---|
146 | * DEVELOPMENT |
---|
147 | */) { |
---|
148 | registry = componentRegistryFactory.getOtherUserComponentRegistry( |
---|
149 | userPrincipal, info.getStatus(), |
---|
150 | new OwnerUser(Integer.parseInt(desc.getUserId()))); |
---|
151 | } |
---|
152 | return registry; |
---|
153 | } |
---|
154 | } |
---|