Changeset 4550


Ignore:
Timestamp:
02/25/14 09:35:47 (10 years ago)
Author:
twagoo
Message:

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

Location:
ComponentRegistry/trunk/ComponentRegistry/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/CMDComponentSpecExpander.java

    r2561 r4550  
    4444                    CMDComponentSpec spec = getUncachedComponent(componentId);
    4545                    if (spec != null) {
    46                         CMDComponentType nested = getComponentTypeOfAComponent(spec);
     46                        CMDComponentType nested = spec.getCMDComponent();
    4747                        expandNestedComponent(nested.getCMDComponent(), newPath);
    4848                        overwriteAttributes(cmdComponentType, nested);
     
    6363        cmdComponents.clear();
    6464        cmdComponents.addAll(expanded);
    65     }
    66    
    67     private CMDComponentType getComponentTypeOfAComponent(CMDComponentSpec result) {
    68         List<CMDComponentType> cmdComponents = result.getCMDComponent();
    69         if (cmdComponents.size() != 1) {
    70             LOG.error("Internal error: CMDComponentSpec which is not a profile can only have one "
    71                     + "CMDComponentType (which is the description of the component itself).");
    72         }
    73         CMDComponentType cmdComponentType = cmdComponents.get(0);
    74         return cmdComponentType;
    7565    }
    7666
     
    9585        // Use uncached components and profiles, because we expand and thus change them this change should not be in the cache.
    9686        CMDComponentSpec result = getUncachedComponent(componentId);//registry.getUncachedComponent(componentId);
    97         CMDComponentType cmdComponentType = getComponentTypeOfAComponent(result);
     87        CMDComponentType cmdComponentType = result.getCMDComponent();
    9888        expandNestedComponent(cmdComponentType.getCMDComponent(), componentId);
    9989        return result;
     
    10393        // Use uncached components and profiles, because we expand and thus change them this change should not be in the cache.
    10494        CMDComponentSpec result = getUncachedProfile(profileId);//registry.getUncachedProfile(profileId);
    105         List<CMDComponentType> cmdComponents = result.getCMDComponent();
    106         expandNestedComponent(cmdComponents, profileId);
     95        CMDComponentType cmdComponent = result.getCMDComponent();
     96        expandNestedComponent(Collections.singletonList(cmdComponent), profileId);
    10797        return result;
    10898    }
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/frontend/StatisticsPage.java

    r1419 r4550  
    2121import clarin.cmdi.componentregistry.model.ComponentDescription;
    2222import clarin.cmdi.componentregistry.model.ProfileDescription;
     23import java.util.Collection;
     24import java.util.Collections;
    2325
    2426/**
     
    6971        CMDComponentSpec profile = CMDComponentSpecExpanderDbImpl.expandProfile(pd.getId(), (ComponentRegistryDbImpl) registry);
    7072        Statistics stats = new Statistics();
    71         componentCounter(profile.getCMDComponent(), stats);
     73        componentCounter(profile, stats);
    7274        item.add(new Label("nrcomp", "" + stats.componentnumber));
    7375        item.add(new Label("nrprofelem", "" + stats.elementcounter));
     
    8284        CMDComponentSpec compspec = CMDComponentSpecExpanderDbImpl.expandComponent(cd.getId(), (ComponentRegistryDbImpl) registry);
    8385        Statistics stats = new Statistics();
    84         componentCounter(compspec.getCMDComponent(), stats);
     86        componentCounter(compspec, stats);
    8587        item.add(new Label("nrcomp", "" + stats.componentnumber));
    8688        item.add(new Label("nrelem", "" + stats.elementcounter));
     
    8890    }
    8991
    90     private void componentCounter(List<CMDComponentType> components, Statistics stats) {
     92    private void componentCounter(CMDComponentSpec components, Statistics stats) {
     93        componentCounter(Collections.singleton(components.getCMDComponent()), stats);
     94    }
     95   
     96    private void componentCounter(Collection<CMDComponentType> components, Statistics stats) {
    9197        if (components != null) {
    9298            for (CMDComponentType component : components) {
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentRegistryImplBase.java

    r4098 r4550  
    2828 */
    2929public abstract class ComponentRegistryImplBase implements ComponentRegistry {
    30    
     30
    3131    private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryImplBase.class);
    32    
     32
    3333    protected abstract MDMarshaller getMarshaller();
    34    
     34
    3535    @Override
    3636    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 && findComponentId(componentId, spec.getCMDComponent())) {
    43                 LOG.debug("Component {} used in component {}", componentId, spec.getHeader().getID());
    44                 result.add(getComponentDescription(id));
    45             }
    46         }
    47         return result;
     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;
    4848    }
    49    
     49
    5050    @Override
    5151    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 && findComponentId(componentId, profile.getCMDComponent())) {
    57                 LOG.debug("Component {} used in profile {}", componentId, profile.getHeader().getID());
    58                 result.add(getProfileDescription(id));
    59             }
    60         }
    61         return result;
     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;
    6262    }
    6363
    6464    /**
    6565     *
    66      * @return List of profile descriptions ordered by name ascending, only the ones marked for showing in metadata editor
     66     * @return List of profile descriptions ordered by name ascending, only the
     67     * ones marked for showing in metadata editor
    6768     * @throws ComponentRegistryException
    6869     */
     
    7172        // TODO: Below can also be done by accepting and passing a parameter in the ProfileDescriptionDaoImpl, should have better performance
    7273
    73         // Get all profile descriptions
    74         List<String> descriptionsCollectionIds = getAllNonDeletedProfileIds();
    75         // Filter out ones that do should not be shown for metadata editor
    76         ArrayList<ProfileDescription> descriptions = new ArrayList<ProfileDescription>();
    77         for (String id:descriptionsCollectionIds) {
    78             ProfileDescription profile = getProfileDescription(id);
    79             if (profile.isShowInEditor()) {
    80                 descriptions.add(profile);
    81             }
    82         }
    83         // Return filtered list
    84         return descriptions;
     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;
    8586    }
    8687
    8788    /* HELPER METHODS */
    8889    protected static String stripRegistryId(String id) {
    89         return StringUtils.removeStart(id, ComponentRegistry.REGISTRY_ID);
     90        return StringUtils.removeStart(id, ComponentRegistry.REGISTRY_ID);
    9091    }
    91    
     92
    9293    protected static void enrichSpecHeader(CMDComponentSpec spec, BaseDescription description) {
    93         Header header = spec.getHeader();
    94         header.setID(description.getId());
    95         if (StringUtils.isEmpty(header.getName())) {
    96             header.setName(description.getName());
    97         }
    98         if (StringUtils.isEmpty(header.getDescription())) {
    99             header.setDescription(description.getDescription());
    100         }
     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        }
    101102    }
    102    
     103
    103104    protected static boolean findComponentId(String componentId, List<CMDComponentType> componentReferences) {
    104         for (CMDComponentType cmdComponent : componentReferences) {
    105             if (componentId.equals(cmdComponent.getComponentId())) {
    106                 return true;
    107             } else if (findComponentId(componentId, cmdComponent.getCMDComponent())) {
    108                 return true;
    109             }
    110         }
    111         return false;
     105        for (CMDComponentType cmdComponent : componentReferences) {
     106            if (hasComponentId(componentId, cmdComponent)) {
     107                return true;
     108            }
     109        }
     110        return false;
    112111    }
    113    
     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
    114123    protected void writeXsd(CMDComponentSpec expandedSpec, OutputStream outputStream) {
    115         getMarshaller().generateXsd(expandedSpec, outputStream);
     124        getMarshaller().generateXsd(expandedSpec, outputStream);
    116125    }
    117    
     126
    118127    protected void writeXml(CMDComponentSpec spec, OutputStream outputStream) {
    119         try {
    120             getMarshaller().marshal(spec, outputStream);
    121         } catch (UnsupportedEncodingException e) {
    122             LOG.error("Error in encoding: ", e);
    123         } catch (JAXBException e) {
    124             LOG.error("Cannot marshall spec: " + spec, e);
    125         }
     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        }
    126135    }
    127    
     136
    128137    protected void checkStillUsed(String componentId) throws DeleteFailedException, ComponentRegistryException {
    129         for (String id : getAllNonDeletedProfileIds()) {
    130             CMDComponentSpec spec = getMDProfile(id);
    131             if (spec != null && findComponentId(componentId, spec.getCMDComponent())) {
    132                 LOG.warn("Cannot delete component {}, still used in profile {} and possibly other profiles and/or components", componentId, spec.getHeader().getID());
    133                 // Profile match - throw
    134                 throw new DeleteFailedException("Component is still in use by other components or profiles. Request component usage for details.");
    135             }
    136         }
    137        
    138         LOG.debug("Component {} is not used in any profiles", componentId);
    139        
    140         for (String id : getAllNonDeletedComponentIds()) {
    141             CMDComponentSpec spec = getMDComponent(id);
    142             if (spec != null && findComponentId(componentId, spec.getCMDComponent())) {
    143                 LOG.warn("Cannot delete component {}, still used in component {} and possibly other components", componentId, spec.getHeader().getID());
    144                 // Component match -> throw
    145                 throw new DeleteFailedException("Component is still in use by one or more other components. Request component usage for details.");
    146             }
    147         }
     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        }
    148157    }
    149158}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r4135 r4550  
    3838import java.util.ArrayList;
    3939import java.util.Arrays;
     40import java.util.Collections;
    4041import java.util.Date;
    4142import java.util.List;
     
    11761177                                // removing filename from spec before it gets extended.
    11771178                                // recursion over all the components
    1178                                 setFileNamesFromListToNull(spec.getCMDComponent());
     1179                                setFileNamesFromListToNull(Collections.singletonList(spec.getCMDComponent()));
    11791180
    11801181                                try {
     
    12361237                        // ComponentRegistryException
    12371238                        registry.getExpander().expandNestedComponent(
    1238                                         specCopy.getCMDComponent(), desc.getId());
     1239                                        Collections.singletonList(specCopy.getCMDComponent()), desc.getId());
    12391240                } catch (JAXBException ex) {
    12401241                        throw new ComponentRegistryException(
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/MDValidator.java

    r4098 r4550  
    2929
    3030public class MDValidator implements Validator {
    31 
     31   
    3232    private final static Logger LOG = LoggerFactory.getLogger(MDValidator.class);
    3333    static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa.";
     
    5757     * @param desc
    5858     * @param registry (registry you currently used)
    59      * @param userRegistry can be null, We get user registry as well so we can give nice error messages if needed. Can be the same as
     59     * @param userRegistry can be null, We get user registry as well so we can
     60     * give nice error messages if needed. Can be the same as
    6061     * @param registry
    6162     */
    6263    public MDValidator(InputStream input, BaseDescription description, ComponentRegistry registry, ComponentRegistry userRegistry, ComponentRegistry publicRegistry, MDMarshaller marshaller) {
    63         this.input = input;
    64         this.description = description;
    65         this.registry = registry;
    66         this.userRegistry = userRegistry;
    67         this.publicRegistry = publicRegistry;
    68         this.marshaller = marshaller;
    69     }
    70 
     64        this.input = input;
     65        this.description = description;
     66        this.registry = registry;
     67        this.userRegistry = userRegistry;
     68        this.publicRegistry = publicRegistry;
     69        this.marshaller = marshaller;
     70    }
     71   
    7172    @Override
    7273    public List<String> getErrorMessages() {
    73         return errorMessages;
    74     }
    75 
     74        return errorMessages;
     75    }
     76   
    7677    @Override
    7778    public boolean validate() {
    78         try {
    79             clarin.cmdi.schema.cmd.Validator validator = new clarin.cmdi.schema.cmd.Validator(new URL(Configuration.getInstance().getGeneralComponentSchema()));
    80             validator.setResourceResolver(new ComponentRegistryResourceResolver());
    81             // We may need to reuse the input stream, so save it to a byte array first
    82             originalSpecBytes = getBytesFromInputStream();
    83             StreamSource source = new StreamSource(new ByteArrayInputStream(originalSpecBytes));
    84             if (!validator.validateProfile(source)) {
    85                 final List<Message> validatorMessages = validator.getMessages();
    86                 if (validatorMessages.size() > 0) {
    87                     for (Message message : validatorMessages) {
    88                         errorMessages.add(PARSE_ERROR + message.getText());
    89                     }
    90                 } else {
    91                     errorMessages.add(PARSE_ERROR + UNKNOWN_VALIDATION_ERROR);
    92                 }
    93             } else {
    94                 spec = unmarshalSpec(originalSpecBytes);
    95                 if (spec.isIsProfile() != description.isProfile()) {
    96                     errorMessages.add(MISMATCH_ERROR);
    97                 }
    98             }
    99         } catch (MalformedURLException e) {
    100             errorMessages.add(SCHEMA_ERROR + e.getMessage());
    101             LOG.error(SCHEMA_ERROR, e);
    102         } catch (JAXBException e) {
    103             errorMessages.add(PARSE_ERROR + e.getMessage());
    104             LOG.error(PARSE_ERROR, e);
    105         } catch (ValidatorException e) {
    106             errorMessages.add(PARSE_ERROR + e.getMessage());
    107             LOG.error(PARSE_ERROR, e);
    108         } catch (IOException e) {
    109             errorMessages.add(IO_ERROR + e.getMessage());
    110             LOG.error(IO_ERROR, e);
    111         }
    112         if (errorMessages.isEmpty()) {
    113             try {
    114                 validateComponents(spec.getCMDComponent());
    115             } catch (ComponentRegistryException e) {
    116                 errorMessages.add(COMPONENT_REGISTRY_EXCEPTION_ERROR + e);
    117             }
    118         }
    119         return errorMessages.isEmpty();
    120     }
    121 
     79        try {
     80            clarin.cmdi.schema.cmd.Validator validator = new clarin.cmdi.schema.cmd.Validator(new URL(Configuration.getInstance().getGeneralComponentSchema()));
     81            validator.setResourceResolver(new ComponentRegistryResourceResolver());
     82            // We may need to reuse the input stream, so save it to a byte array first
     83            originalSpecBytes = getBytesFromInputStream();
     84            StreamSource source = new StreamSource(new ByteArrayInputStream(originalSpecBytes));
     85            if (!validator.validateProfile(source)) {
     86                final List<Message> validatorMessages = validator.getMessages();
     87                if (validatorMessages.size() > 0) {
     88                    for (Message message : validatorMessages) {
     89                        errorMessages.add(PARSE_ERROR + message.getText());
     90                    }
     91                } else {
     92                    errorMessages.add(PARSE_ERROR + UNKNOWN_VALIDATION_ERROR);
     93                }
     94            } else {
     95                spec = unmarshalSpec(originalSpecBytes);
     96                if (spec.isIsProfile() != description.isProfile()) {
     97                    errorMessages.add(MISMATCH_ERROR);
     98                }
     99            }
     100        } catch (MalformedURLException e) {
     101            errorMessages.add(SCHEMA_ERROR + e.getMessage());
     102            LOG.error(SCHEMA_ERROR, e);
     103        } catch (JAXBException e) {
     104            errorMessages.add(PARSE_ERROR + e.getMessage());
     105            LOG.error(PARSE_ERROR, e);
     106        } catch (ValidatorException e) {
     107            errorMessages.add(PARSE_ERROR + e.getMessage());
     108            LOG.error(PARSE_ERROR, e);
     109        } catch (IOException e) {
     110            errorMessages.add(IO_ERROR + e.getMessage());
     111            LOG.error(IO_ERROR, e);
     112        }
     113        if (errorMessages.isEmpty()) {
     114            try {
     115                validateComponents(spec);
     116            } catch (ComponentRegistryException e) {
     117                errorMessages.add(COMPONENT_REGISTRY_EXCEPTION_ERROR + e);
     118            }
     119        }
     120        return errorMessages.isEmpty();
     121    }
     122   
    122123    private byte[] getBytesFromInputStream() throws IOException {
    123         int len;
    124         byte[] b = new byte[4096];
    125         final ByteArrayOutputStream bOS = new ByteArrayOutputStream();
    126 
    127         while ((len = input.read(b)) > 0) {
    128             bOS.write(b, 0, len);
    129         }
    130 
    131         return bOS.toByteArray();
    132     }
    133 
     124        int len;
     125        byte[] b = new byte[4096];
     126        final ByteArrayOutputStream bOS = new ByteArrayOutputStream();
     127       
     128        while ((len = input.read(b)) > 0) {
     129            bOS.write(b, 0, len);
     130        }
     131       
     132        return bOS.toByteArray();
     133    }
     134
     135    private void validateComponents(CMDComponentSpec componentSpec) throws ComponentRegistryException {
     136        validateComponents(Collections.singletonList(componentSpec.getCMDComponent()));
     137    }
     138   
    134139    private void validateComponents(List<CMDComponentType> cmdComponents) throws ComponentRegistryException {
    135         for (CMDComponentType cmdComponentType : cmdComponents) {
    136             validateDescribedComponents(cmdComponentType);
    137             validateComponents(cmdComponentType.getCMDComponent());//Recursion
    138         }
    139     }
    140 
     140        for (CMDComponentType cmdComponentType : cmdComponents) {
     141            validateDescribedComponents(cmdComponentType);
     142            validateComponents(cmdComponentType.getCMDComponent());//Recursion
     143        }
     144    }
     145   
    141146    private void validateDescribedComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException {
    142         checkPublicComponents(cmdComponentType);
    143     }
    144 
     147        checkPublicComponents(cmdComponentType);
     148    }
     149   
    145150    private void checkPublicComponents(CMDComponentType cmdComponentType) throws ComponentRegistryException {
    146         if (isDefinedInSeparateFile(cmdComponentType)) {
    147             String id = cmdComponentType.getComponentId();
    148             CMDComponentSpec registeredComponent;
    149             if (registry.isPublic()) { // public registry requires only published components
    150                 registeredComponent = registry.getMDComponent(id);
    151                 if (registeredComponent == null) {
    152                     String error = cmdComponentType.getComponentId();
    153                     if (userRegistry != null) {
    154                         ComponentDescription desc = userRegistry.getComponentDescription(id);
    155                         if (desc != null) {
    156                             error = desc.getName() + " (" + cmdComponentType.getComponentId() + ")";
    157                         }
    158                     }
    159                     errorMessages.add(COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR + error);
    160                 }
    161             } else { //User registry, can link to components from public registry and the user's registry
    162                 registeredComponent = registry.getMDComponent(id);
    163                 if (registeredComponent == null) {
    164                     registeredComponent = publicRegistry.getMDComponent(id);
    165                     if (registeredComponent == null) {
    166                         errorMessages.add(COMPONENT_NOT_REGISTERED_ERROR + cmdComponentType.getComponentId());
    167                     }
    168                 }
    169 
    170             }
    171         }
    172     }
    173 
     151        if (isDefinedInSeparateFile(cmdComponentType)) {
     152            String id = cmdComponentType.getComponentId();
     153            CMDComponentSpec registeredComponent;
     154            if (registry.isPublic()) { // public registry requires only published components
     155                registeredComponent = registry.getMDComponent(id);
     156                if (registeredComponent == null) {
     157                    String error = cmdComponentType.getComponentId();
     158                    if (userRegistry != null) {
     159                        ComponentDescription desc = userRegistry.getComponentDescription(id);
     160                        if (desc != null) {
     161                            error = desc.getName() + " (" + cmdComponentType.getComponentId() + ")";
     162                        }
     163                    }
     164                    errorMessages.add(COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR + error);
     165                }
     166            } else { //User registry, can link to components from public registry and the user's registry
     167                registeredComponent = registry.getMDComponent(id);
     168                if (registeredComponent == null) {
     169                    registeredComponent = publicRegistry.getMDComponent(id);
     170                    if (registeredComponent == null) {
     171                        errorMessages.add(COMPONENT_NOT_REGISTERED_ERROR + cmdComponentType.getComponentId());
     172                    }
     173                }
     174               
     175            }
     176        }
     177    }
     178   
    174179    private boolean isDefinedInSeparateFile(CMDComponentType cmdComponentType) {
    175         return cmdComponentType.getName() == null;
     180        return cmdComponentType.getName() == null;
    176181    }
    177182
     
    179184     * Do not call before having called {@link #validate() }!
    180185     *
    181      * @return the spec unmarshalled during {@link #validate() }. If this has not been called, returns null.
     186     * @return the spec unmarshalled during {@link #validate() }. If this has
     187     * not been called, returns null.
    182188     */
    183189    public CMDComponentSpec getCMDComponentSpec() {
    184         return spec;
     190        return spec;
    185191    }
    186192
    187193    /**
    188      * Creates a fresh (re-unmarshalled) copy of the specification this instance has validated. If you are not going to alter this copy,
    189      * you can re-use and share the copy used during validation by getting it from {@link #getCMDComponentSpec() }.
     194     * Creates a fresh (re-unmarshalled) copy of the specification this instance
     195     * has validated. If you are not going to alter this copy, you can re-use
     196     * and share the copy used during validation by getting it from {@link #getCMDComponentSpec()
     197     * }.
    190198     * <em>Do not call before having called {@link #validate() }!</em>
    191199     *
    192      * @return a freshly unmarshalled copy of the spec based on the bytes collected from the input stream passed to {@link #validate() }.
    193      * If this has not been called, returns null.
    194      * @throws JAXBException exception occurred while marshalling from the input bytes
     200     * @return a freshly unmarshalled copy of the spec based on the bytes
     201     * collected from the input stream passed to {@link #validate() }. If this
     202     * has not been called, returns null.
     203     * @throws JAXBException exception occurred while marshalling from the input
     204     * bytes
    195205     * @see #validate()
    196206     * @see #getCMDComponentSpec()
    197207     */
    198208    public CMDComponentSpec getCopyOfCMDComponentSpec() throws JAXBException {
    199         // Re-unmarshall original bytes
    200         return unmarshalSpec(originalSpecBytes);
    201     }
    202 
     209        // Re-unmarshall original bytes
     210        return unmarshalSpec(originalSpecBytes);
     211    }
     212   
    203213    private CMDComponentSpec unmarshalSpec(byte[] inputBytes) throws JAXBException {
    204         return marshaller.unmarshal(CMDComponentSpec.class, new ByteArrayInputStream(inputBytes), null);
     214        return marshaller.unmarshal(CMDComponentSpec.class, new ByteArrayInputStream(inputBytes), null);
    205215    }
    206216}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/CMDComponentSetFilenamesToNullTestRunner.java

    r3449 r4550  
    1111import java.io.IOException;
    1212import java.io.InputStreamReader;
     13import java.util.Collections;
    1314import java.util.List;
    1415import javax.xml.bind.JAXBException;
     
    1617
    1718/**
    18  * non-automated developers. test, with and input (a valid
    19  *         component/prifile xml file and a path to it) and the output (the file
    20  *         where all filenames are set to null, in the same directory as the
    21  *         input file)
    22  * @author olhsha 
     19 * non-automated developers. test, with and input (a valid component/prifile xml
     20 * file and a path to it) and the output (the file where all filenames are set
     21 * to null, in the same directory as the input file)
     22 *
     23 * @author olhsha
    2324 * @author George.Georgovassilis@mpi.nl
    2425 */
     
    2627
    2728    /**
    28      * 
     29     *
    2930     * @author olhsha
    3031     */
    3132    /**
    3233     * makes a new component (with filled-in filename)
    33      *
    34      * @param filename
    35      *            is assigned to the filed "filename" of the new component
    36      * @param childcomponents
    37      *            is assigned to the list of child components of the new
    38      *            component
     34     *
     35     * @param filename is assigned to the filed "filename" of the new component
     36     * @param childcomponents is assigned to the list of child components of the
     37     * new component
    3938     * @return the reference to the new component
    4039     */
    4140    protected CMDComponentType makeTestComponent(String filename,
    42             List<CMDComponentType> childcomponents) {
    43 
    44         CMDComponentType component = new CMDComponentType();
    45         component.setFilename(filename);
    46         List<CMDComponentType> kids = component.getCMDComponent();
    47         assertFalse(kids == null);
    48         assertEquals(kids.size(), 0);
    49         if (childcomponents != null) {
    50             kids.addAll(childcomponents);
    51         }
    52         ;
    53         return component;
     41            List<CMDComponentType> childcomponents) {
     42
     43        CMDComponentType component = new CMDComponentType();
     44        component.setFilename(filename);
     45        List<CMDComponentType> kids = component.getCMDComponent();
     46        assertFalse(kids == null);
     47        assertEquals(kids.size(), 0);
     48        if (childcomponents != null) {
     49            kids.addAll(childcomponents);
     50        }
     51        ;
     52        return component;
    5453    }
    5554
     
    5857     */
    5958    private void checkNullnessOfFilenamesInComponent(CMDComponentType component) {
    60         assertEquals(component.getFilename(), null);
    61         checkNullnessOfFilenamesInListOfComponents(component.getCMDComponent());
     59        assertEquals(component.getFilename(), null);
     60        checkNullnessOfFilenamesInListOfComponents(component.getCMDComponent());
    6261    }
    6362
     
    6665     */
    6766    private void checkNullnessOfFilenamesInListOfComponents(
    68             List<CMDComponentType> listofcomponents) {
    69         for (CMDComponentType currentcomponent : listofcomponents) {
    70             checkNullnessOfFilenamesInComponent(currentcomponent);
    71         }
     67            List<CMDComponentType> listofcomponents) {
     68        for (CMDComponentType currentcomponent : listofcomponents) {
     69            checkNullnessOfFilenamesInComponent(currentcomponent);
     70        }
    7271    }
    7372
     
    7776     */
    7877    private boolean checkNonNullnessOfFilenamesInComponent(
    79             CMDComponentType component) {
    80 
    81         String filename = component.getFilename();
    82         System.out.println(filename);
    83         boolean check = (filename == null);
    84         if (check) {
    85             return false;
    86         }
    87         return checkNonNullnessOfFilenamesInListOfComponents(component
    88                 .getCMDComponent());
     78            CMDComponentType component) {
     79
     80        String filename = component.getFilename();
     81        System.out.println(filename);
     82        boolean check = (filename == null);
     83        if (check) {
     84            return false;
     85        }
     86        return checkNonNullnessOfFilenamesInListOfComponents(component
     87                .getCMDComponent());
    8988    }
    9089
     
    9493     */
    9594    private boolean checkNonNullnessOfFilenamesInListOfComponents(
    96             List<CMDComponentType> listofcomponents) {
    97         boolean check;
    98         for (CMDComponentType currentcomponent : listofcomponents) {
    99             check = checkNonNullnessOfFilenamesInComponent(currentcomponent);
    100             if (!check) {
    101                 return false;
    102             }
    103         }
    104         return true;
     95            List<CMDComponentType> listofcomponents) {
     96        boolean check;
     97        for (CMDComponentType currentcomponent : listofcomponents) {
     98            check = checkNonNullnessOfFilenamesInComponent(currentcomponent);
     99            if (!check) {
     100                return false;
     101            }
     102        }
     103        return true;
    105104    }
    106105
     
    109108     */
    110109    private void addDummyFilenamesToComponent(CMDComponentType component) {
    111         if (component != null) {
    112             component.setFilename("Dummy");
    113             List<CMDComponentType> listofcomponents = component
    114                     .getCMDComponent();
    115             addDummyFilenamesToListOfComponents(listofcomponents);
    116         }
     110        if (component != null) {
     111            component.setFilename("Dummy");
     112            List<CMDComponentType> listofcomponents = component
     113                    .getCMDComponent();
     114            addDummyFilenamesToListOfComponents(listofcomponents);
     115        }
    117116    }
    118117
     
    121120     */
    122121    private void addDummyFilenamesToListOfComponents(
    123             List<CMDComponentType> listofcomponents) {
    124         for (CMDComponentType currentcomponent : listofcomponents) {
    125             addDummyFilenamesToComponent(currentcomponent);
    126         }
     122            List<CMDComponentType> listofcomponents) {
     123        for (CMDComponentType currentcomponent : listofcomponents) {
     124            addDummyFilenamesToComponent(currentcomponent);
     125        }
    127126    }
    128127
     
    131130     */
    132131    private CMDComponentSpec makeTestFromFile(String filename)
    133             throws IOException, JAXBException {
    134         FileInputStream is = new FileInputStream(filename);
    135         String profilestring = RegistryTestHelper.getStringFromStream(is);
    136         CMDComponentSpec compspec = RegistryTestHelper
    137                 .getComponentFromString(profilestring); // calling unmarchaller
    138         List<CMDComponentType> listofcomponents = compspec.getCMDComponent();
    139         addDummyFilenamesToListOfComponents(listofcomponents);
    140         assertTrue(checkNonNullnessOfFilenamesInListOfComponents(listofcomponents));
    141         return compspec;
     132            throws IOException, JAXBException {
     133        FileInputStream is = new FileInputStream(filename);
     134        String profilestring = RegistryTestHelper.getStringFromStream(is);
     135        CMDComponentSpec compspec = RegistryTestHelper
     136                .getComponentFromString(profilestring); // calling unmarchaller
     137        List<CMDComponentType> listofcomponents = Collections.singletonList(compspec.getCMDComponent());
     138        addDummyFilenamesToListOfComponents(listofcomponents);
     139        assertTrue(checkNonNullnessOfFilenamesInListOfComponents(listofcomponents));
     140        return compspec;
    142141    }
    143142
     
    147146     */
    148147    private void writeDummiedXML(String filenamein, String filenameout)
    149             throws IOException, JAXBException {
    150         CMDComponentSpec compspec = makeTestFromFile(filenamein);
    151         String os = RegistryTestHelper.getXml(compspec);
    152         RegistryTestHelper.writeStringToFile(os, filenameout);
     148            throws IOException, JAXBException {
     149        CMDComponentSpec compspec = makeTestFromFile(filenamein);
     150        String os = RegistryTestHelper.getXml(compspec);
     151        RegistryTestHelper.writeStringToFile(os, filenameout);
    153152    }
    154153
     
    157156     */
    158157    private void setFileNamesToNullInFile(String dirName, String fileNameInit,
    159             String fileNameDummied, String fileNameUnDummied)
    160             throws IOException, JAXBException {
    161 
    162         String path = RegistryTestHelper.openTestDir(dirName);
    163         writeDummiedXML(path + fileNameInit, path + fileNameDummied);
    164 
    165         FileInputStream is = new FileInputStream(path + fileNameDummied);
    166         String dummiedcontent = RegistryTestHelper.getStringFromStream(is);
    167         CMDComponentSpec compspec = RegistryTestHelper
    168                 .getComponentFromString(dummiedcontent); // calling unmarchaller
    169 
    170         List<CMDComponentType> listofcomponents = compspec.getCMDComponent();
    171 
    172         IComponentRegistryRestService testrestservice = new ComponentRegistryRestService();
    173         testrestservice.setFileNamesFromListToNull(listofcomponents);
    174         checkNullnessOfFilenamesInListOfComponents(listofcomponents);
    175 
    176         String os = RegistryTestHelper.getXml(compspec);
    177         RegistryTestHelper.writeStringToFile(os, path + fileNameUnDummied);
     158            String fileNameDummied, String fileNameUnDummied)
     159            throws IOException, JAXBException {
     160
     161        String path = RegistryTestHelper.openTestDir(dirName);
     162        writeDummiedXML(path + fileNameInit, path + fileNameDummied);
     163
     164        FileInputStream is = new FileInputStream(path + fileNameDummied);
     165        String dummiedcontent = RegistryTestHelper.getStringFromStream(is);
     166        CMDComponentSpec compspec = RegistryTestHelper
     167                .getComponentFromString(dummiedcontent); // calling unmarchaller
     168
     169        List<CMDComponentType> listofcomponents = Collections.singletonList(compspec.getCMDComponent());
     170
     171        IComponentRegistryRestService testrestservice = new ComponentRegistryRestService();
     172        testrestservice.setFileNamesFromListToNull(listofcomponents);
     173        checkNullnessOfFilenamesInListOfComponents(listofcomponents);
     174
     175        String os = RegistryTestHelper.getXml(compspec);
     176        RegistryTestHelper.writeStringToFile(os, path + fileNameUnDummied);
    178177    }
    179178
     
    184183     */
    185184    public static void main(String args[]) throws java.io.IOException,
    186             JAXBException {
    187 
    188         BufferedReader buffer = new BufferedReader(new InputStreamReader(
    189                 System.in));
    190 
    191         System.out.println("");
    192         System.out
    193                 .print("Sub-directory (of target/) name? (up to 32 symbols): ");
    194         String dirName = buffer.readLine();
    195         System.out.println("");
    196         System.out
    197                 .println("(Watch out: this is a temorary directory, which is removed after any new clean+build)");
    198         System.out.println(dirName);
    199 
    200         System.out.println("");
    201         System.out.println("Check if your file is in this temorary directory");
    202         System.out.print("and input the file name (up to 32 symbols): ");
    203         String fileName = buffer.readLine();
    204         System.out.println(fileName);
    205 
    206         System.out.println("Bedankt, ff wachten .. ");
    207 
    208         String fileNameDummied = "Dummied" + fileName;
    209         String fileNameUnDummied = "Nulled" + fileName;
    210 
    211         CMDComponentSetFilenamesToNullTestRunner helper = new CMDComponentSetFilenamesToNullTestRunner();
    212 
    213         helper.setFileNamesToNullInFile(dirName, fileName, fileNameDummied,
    214                 fileNameUnDummied);
    215 
    216         System.out.println("Now look up the directory target/" + dirName);
     185            JAXBException {
     186
     187        BufferedReader buffer = new BufferedReader(new InputStreamReader(
     188                System.in));
     189
     190        System.out.println("");
     191        System.out
     192                .print("Sub-directory (of target/) name? (up to 32 symbols): ");
     193        String dirName = buffer.readLine();
     194        System.out.println("");
     195        System.out
     196                .println("(Watch out: this is a temorary directory, which is removed after any new clean+build)");
     197        System.out.println(dirName);
     198
     199        System.out.println("");
     200        System.out.println("Check if your file is in this temorary directory");
     201        System.out.print("and input the file name (up to 32 symbols): ");
     202        String fileName = buffer.readLine();
     203        System.out.println(fileName);
     204
     205        System.out.println("Bedankt, ff wachten .. ");
     206
     207        String fileNameDummied = "Dummied" + fileName;
     208        String fileNameUnDummied = "Nulled" + fileName;
     209
     210        CMDComponentSetFilenamesToNullTestRunner helper = new CMDComponentSetFilenamesToNullTestRunner();
     211
     212        helper.setFileNamesToNullInFile(dirName, fileName, fileNameDummied,
     213                fileNameUnDummied);
     214
     215        System.out.println("Now look up the directory target/" + dirName);
    217216    }
    218217}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java

    r4131 r4550  
    161161                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
    162162        assertNotNull(component);
    163         assertEquals("Access", component.getCMDComponent().get(0).getName());
     163        assertEquals("Access", component.getCMDComponent().getName());
    164164        component = getResource().path("/registry/components/" + id2)
    165165                .accept(MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
    166166        assertNotNull(component);
    167         assertEquals("Access", component.getCMDComponent().get(0).getName());
     167        assertEquals("Access", component.getCMDComponent().getName());
    168168
    169169        assertEquals(id2, component.getHeader().getID());
     
    536536                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
    537537        assertNotNull(profile);
    538         assertEquals("Actor", profile.getCMDComponent().get(0).getName());
     538        assertEquals("Actor", profile.getCMDComponent().getName());
    539539        profile = getResource().path("/registry/profiles/" + id2)
    540540                .accept(MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
    541541        assertNotNull(profile);
    542         assertEquals("Actor", profile.getCMDComponent().get(0).getName());
     542        assertEquals("Actor", profile.getCMDComponent().getName());
    543543
    544544        assertEquals(id2, profile.getHeader().getID());
     
    796796        assertEquals("Published", profileDescription.getDescription());
    797797        CMDComponentSpec spec = getPublicSpec(profileDescription);
    798         assertEquals("publishedName", spec.getCMDComponent().get(0).getName());
     798        assertEquals("publishedName", spec.getCMDComponent().getName());
    799799    }
    800800
     
    845845        assertEquals("Published", componentDescription.getDescription());
    846846        CMDComponentSpec spec = getPublicSpec(componentDescription);
    847         assertEquals("publishedName", spec.getCMDComponent().get(0).getName());
     847        assertEquals("publishedName", spec.getCMDComponent().getName());
    848848    }
    849849
     
    11851185        CMDComponentSpec spec = getUserComponent(desc);
    11861186        assertNotNull(spec);
    1187         assertEquals("Access", spec.getCMDComponent().get(0).getName());
     1187        assertEquals("Access", spec.getCMDComponent().getName());
    11881188        components = getUserComponents();
    11891189        assertEquals(1, components.size());
     
    12141214        spec = getUserComponent(desc);
    12151215        assertNotNull(spec);
    1216         assertEquals("TESTNAME", spec.getCMDComponent().get(0).getName());
     1216        assertEquals("TESTNAME", spec.getCMDComponent().getName());
    12171217        components = getUserComponents();
    12181218        assertEquals(1, components.size());
     
    12471247        CMDComponentSpec spec = getUserProfile(desc);
    12481248        assertNotNull(spec);
    1249         assertEquals("Actor", spec.getCMDComponent().get(0).getName());
     1249        assertEquals("Actor", spec.getCMDComponent().getName());
    12501250        profiles = getUserProfiles();
    12511251        assertEquals(1, profiles.size());
     
    12761276        spec = getUserProfile(desc);
    12771277        assertNotNull(spec);
    1278         assertEquals("TESTNAME", spec.getCMDComponent().get(0).getName());
     1278        assertEquals("TESTNAME", spec.getCMDComponent().getName());
    12791279        profiles = getUserProfiles();
    12801280        assertEquals(1, profiles.size());
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/MDValidatorTest.java

    r4098 r4550  
    294294        // Spec content should match XML
    295295        assertTrue(cmdComponentSpec.isIsProfile());
    296         assertEquals("Actor", cmdComponentSpec.getCMDComponent().get(0)
    297                 .getName());
     296        assertEquals("Actor", cmdComponentSpec.getCMDComponent().getName());
    298297
    299298        // Spec copy should be a freshly unmarshalled copy
     
    303302        // Content should still match XML
    304303        assertTrue(specCopy.isIsProfile());
    305         assertEquals("Actor", specCopy.getCMDComponent().get(0).getName());
     304        assertEquals("Actor", specCopy.getCMDComponent().getName());
    306305    }
    307306
Note: See TracChangeset for help on using the changeset viewer.