source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/AdminRegistry.java @ 5549

Last change on this file since 5549 was 5549, checked in by olhsha@mpi.nl, 10 years ago

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File size: 5.4 KB
Line 
1package clarin.cmdi.componentregistry.impl.database;
2
3import java.io.IOException;
4import java.security.Principal;
5
6import javax.xml.bind.JAXBException;
7
8import org.apache.commons.io.IOUtils;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11import org.springframework.dao.DataAccessException;
12
13import clarin.cmdi.componentregistry.ComponentRegistry;
14import clarin.cmdi.componentregistry.ComponentRegistryException;
15import clarin.cmdi.componentregistry.ComponentRegistryFactory;
16import clarin.cmdi.componentregistry.DeleteFailedException;
17import clarin.cmdi.componentregistry.ItemNotFoundException;
18import clarin.cmdi.componentregistry.MDMarshaller;
19import clarin.cmdi.componentregistry.UserCredentials;
20import clarin.cmdi.componentregistry.UserUnauthorizedException;
21import clarin.cmdi.componentregistry.components.CMDComponentSpec;
22import clarin.cmdi.componentregistry.frontend.CMDItemInfo;
23import clarin.cmdi.componentregistry.frontend.SubmitFailedException;
24import clarin.cmdi.componentregistry.model.BaseDescription;
25import clarin.cmdi.componentregistry.model.ComponentDescription;
26import clarin.cmdi.componentregistry.model.ProfileDescription;
27import clarin.cmdi.componentregistry.persistence.ComponentDao;
28
29/**
30 *
31 * @author george.georgovassilis@mpi.nl
32 *
33 */
34public 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}
Note: See TracBrowser for help on using the repository browser.