source: ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentRegistryImplBase.java @ 1635

Last change on this file since 1635 was 1635, checked in by jeafer, 13 years ago

Jean-Charles branch commit3,
Changes regarding comment on the ComponentRegistry service.
Delete_comment from RestService? to Database access (CommentsDao?) implemented.
Post_comment from RestService? to Database access (CommentsDao?) implemented. (Not yet tested)
Comment class updated
CommentsDao? updated
CommentTest? class implemented (New Class)
Test classes ComponentRegistryRestServiceTest? and RegistryHelperTest? starting to implement tests for Comment validation (insertion, deletion)

File size: 6.7 KB
Line 
1package clarin.cmdi.componentregistry.impl;
2
3import java.io.OutputStream;
4import java.io.UnsupportedEncodingException;
5import java.util.ArrayList;
6import java.util.List;
7
8import javax.xml.bind.JAXBException;
9
10import org.apache.commons.lang.StringUtils;
11import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
14import clarin.cmdi.componentregistry.ComponentRegistry;
15import clarin.cmdi.componentregistry.ComponentRegistryException;
16import clarin.cmdi.componentregistry.DeleteFailedException;
17import clarin.cmdi.componentregistry.MDMarshaller;
18import clarin.cmdi.componentregistry.components.CMDComponentSpec;
19import clarin.cmdi.componentregistry.components.CMDComponentType;
20import clarin.cmdi.componentregistry.components.CMDComponentSpec.Header;
21import clarin.cmdi.componentregistry.model.AbstractDescription;
22import clarin.cmdi.componentregistry.model.Comment;
23import clarin.cmdi.componentregistry.model.ComponentDescription;
24import clarin.cmdi.componentregistry.model.ProfileDescription;
25
26/**
27 *
28 * @author Twan Goosen <twan.goosen@mpi.nl>
29 */
30public 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}
Note: See TracBrowser for help on using the repository browser.