source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentRegistryImplBase.java @ 4550

Last change on this file since 4550 was 4550, checked in by twagoo, 10 years ago

Fixed errors due to schema change allowing only one root element in a profile or component (see r4525)

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.BaseDescription;
22import clarin.cmdi.componentregistry.model.ComponentDescription;
23import clarin.cmdi.componentregistry.model.ProfileDescription;
24
25/**
26 *
27 * @author Twan Goosen <twan.goosen@mpi.nl>
28 */
29public abstract class ComponentRegistryImplBase implements ComponentRegistry {
30
31    private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryImplBase.class);
32
33    protected abstract MDMarshaller getMarshaller();
34
35    @Override
36    public List<ComponentDescription> getUsageInComponents(String componentId) throws ComponentRegistryException {
37        LOG.debug("Checking usage of component {} in components", componentId);
38        List<ComponentDescription> result = new ArrayList<ComponentDescription>();
39        List<String> ids = getAllNonDeletedComponentIds();
40        for (String id : ids) {
41            CMDComponentSpec spec = getMDComponent(id);
42            if (spec != null && hasComponentId(componentId, spec.getCMDComponent())) {
43                LOG.debug("Component {} used in component {}", componentId, spec.getHeader().getID());
44                result.add(getComponentDescription(id));
45            }
46        }
47        return result;
48    }
49
50    @Override
51    public List<ProfileDescription> getUsageInProfiles(String componentId) throws ComponentRegistryException {
52        LOG.debug("Checking usage of component {} in profiles", componentId);
53        List<ProfileDescription> result = new ArrayList<ProfileDescription>();
54        for (String id : getAllNonDeletedProfileIds()) {
55            CMDComponentSpec profile = getMDProfile(id);
56            if (profile != null && hasComponentId(componentId, profile.getCMDComponent())) {
57                LOG.debug("Component {} used in profile {}", componentId, profile.getHeader().getID());
58                result.add(getProfileDescription(id));
59            }
60        }
61        return result;
62    }
63
64    /**
65     *
66     * @return List of profile descriptions ordered by name ascending, only the
67     * ones marked for showing in metadata editor
68     * @throws ComponentRegistryException
69     */
70    @Override
71    public List<ProfileDescription> getProfileDescriptionsForMetadaEditor() throws ComponentRegistryException {
72        // TODO: Below can also be done by accepting and passing a parameter in the ProfileDescriptionDaoImpl, should have better performance
73
74        // Get all profile descriptions
75        List<String> descriptionsCollectionIds = getAllNonDeletedProfileIds();
76        // Filter out ones that do should not be shown for metadata editor
77        ArrayList<ProfileDescription> descriptions = new ArrayList<ProfileDescription>();
78        for (String id : descriptionsCollectionIds) {
79            ProfileDescription profile = getProfileDescription(id);
80            if (profile.isShowInEditor()) {
81                descriptions.add(profile);
82            }
83        }
84        // Return filtered list
85        return descriptions;
86    }
87
88    /* HELPER METHODS */
89    protected static String stripRegistryId(String id) {
90        return StringUtils.removeStart(id, ComponentRegistry.REGISTRY_ID);
91    }
92
93    protected static void enrichSpecHeader(CMDComponentSpec spec, BaseDescription description) {
94        Header header = spec.getHeader();
95        header.setID(description.getId());
96        if (StringUtils.isEmpty(header.getName())) {
97            header.setName(description.getName());
98        }
99        if (StringUtils.isEmpty(header.getDescription())) {
100            header.setDescription(description.getDescription());
101        }
102    }
103
104    protected static boolean findComponentId(String componentId, List<CMDComponentType> componentReferences) {
105        for (CMDComponentType cmdComponent : componentReferences) {
106            if (hasComponentId(componentId, cmdComponent)) {
107                return true;
108            }
109        }
110        return false;
111    }
112
113    private static boolean hasComponentId(String componentId, CMDComponentType cmdComponent) {
114        if (componentId.equals(cmdComponent.getComponentId())) {
115            return true;
116        } else if (findComponentId(componentId, cmdComponent.getCMDComponent())) {
117            return true;
118        } else {
119            return false;
120        }
121    }
122
123    protected void writeXsd(CMDComponentSpec expandedSpec, OutputStream outputStream) {
124        getMarshaller().generateXsd(expandedSpec, outputStream);
125    }
126
127    protected void writeXml(CMDComponentSpec spec, OutputStream outputStream) {
128        try {
129            getMarshaller().marshal(spec, outputStream);
130        } catch (UnsupportedEncodingException e) {
131            LOG.error("Error in encoding: ", e);
132        } catch (JAXBException e) {
133            LOG.error("Cannot marshall spec: " + spec, e);
134        }
135    }
136
137    protected void checkStillUsed(String componentId) throws DeleteFailedException, ComponentRegistryException {
138        for (String id : getAllNonDeletedProfileIds()) {
139            CMDComponentSpec spec = getMDProfile(id);
140            if (spec != null && hasComponentId(componentId, spec.getCMDComponent())) {
141                LOG.warn("Cannot delete component {}, still used in profile {} and possibly other profiles and/or components", componentId, spec.getHeader().getID());
142                // Profile match - throw
143                throw new DeleteFailedException("Component is still in use by other components or profiles. Request component usage for details.");
144            }
145        }
146
147        LOG.debug("Component {} is not used in any profiles", componentId);
148
149        for (String id : getAllNonDeletedComponentIds()) {
150            CMDComponentSpec spec = getMDComponent(id);
151            if (spec != null && hasComponentId(componentId, spec.getCMDComponent())) {
152                LOG.warn("Cannot delete component {}, still used in component {} and possibly other components", componentId, spec.getHeader().getID());
153                // Component match -> throw
154                throw new DeleteFailedException("Component is still in use by one or more other components. Request component usage for details.");
155            }
156        }
157    }
158}
Note: See TracBrowser for help on using the repository browser.