Search in sources :

Example 16 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.

the class EventsEndpoint method deserializeMetadataList.

/**
 * Change the simplified fields of key values provided to the external api into a {@link MetadataList}.
 *
 * @param json
 *          The json string that contains an array of metadata field lists for the different catalogs.
 * @return A {@link MetadataList} with the fields populated with the values provided.
 * @throws ParseException
 *           Thrown if unable to parse the json string.
 * @throws NotFoundException
 *           Thrown if unable to find the catalog or field that the json refers to.
 */
protected MetadataList deserializeMetadataList(String json) throws ParseException, NotFoundException {
    MetadataList metadataList = new MetadataList();
    JSONParser parser = new JSONParser();
    JSONArray jsonCatalogs = (JSONArray) parser.parse(json);
    for (int i = 0; i < jsonCatalogs.size(); i++) {
        JSONObject catalog = (JSONObject) jsonCatalogs.get(i);
        String flavorString = catalog.get("flavor").toString();
        if (StringUtils.trimToNull(flavorString) == null) {
            throw new IllegalArgumentException("Unable to create new event as no flavor was given for one of the metadata collections");
        }
        MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(flavorString);
        MetadataCollection collection = null;
        EventCatalogUIAdapter adapter = null;
        for (EventCatalogUIAdapter eventCatalogUIAdapter : getEventCatalogUIAdapters()) {
            if (eventCatalogUIAdapter.getFlavor().equals(flavor)) {
                adapter = eventCatalogUIAdapter;
                collection = eventCatalogUIAdapter.getRawFields();
            }
        }
        if (collection == null) {
            throw new IllegalArgumentException(String.format("Unable to find an EventCatalogUIAdapter with Flavor '%s'", flavorString));
        }
        String fieldsJson = catalog.get("fields").toString();
        if (StringUtils.trimToNull(fieldsJson) != null) {
            Map<String, String> fields = RequestUtils.getKeyValueMap(fieldsJson);
            for (String key : fields.keySet()) {
                MetadataField<?> field = collection.getOutputFields().get(key);
                if (field == null) {
                    throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", key, flavorString));
                }
                collection.removeField(field);
                collection.addField(MetadataField.copyMetadataFieldWithValue(field, fields.get(key)));
            }
        }
        metadataList.add(adapter, collection);
    }
    return metadataList;
}
Also used : JSONArray(org.json.simple.JSONArray) NotFoundException(org.opencastproject.util.NotFoundException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) JSONObject(org.json.simple.JSONObject) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) EventCatalogUIAdapter(org.opencastproject.metadata.dublincore.EventCatalogUIAdapter) JSONParser(org.json.simple.parser.JSONParser) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection)

Example 17 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.

the class SeriesEndpoint method getSeriesMetadata.

/**
 * Loads the metadata for the given series
 *
 * @param series
 *          the source {@link Series}
 * @return a {@link MetadataCollection} instance with all the series metadata
 */
@SuppressWarnings("unchecked")
private MetadataCollection getSeriesMetadata(Series series) {
    MetadataCollection metadata = indexService.getCommonSeriesCatalogUIAdapter().getRawFields();
    MetadataField<?> title = metadata.getOutputFields().get(DublinCore.PROPERTY_TITLE.getLocalName());
    metadata.removeField(title);
    MetadataField<String> newTitle = MetadataUtils.copyMetadataField(title);
    newTitle.setValue(series.getTitle());
    metadata.addField(newTitle);
    MetadataField<?> subject = metadata.getOutputFields().get(DublinCore.PROPERTY_SUBJECT.getLocalName());
    metadata.removeField(subject);
    MetadataField<String> newSubject = MetadataUtils.copyMetadataField(subject);
    newSubject.setValue(series.getSubject());
    metadata.addField(newSubject);
    MetadataField<?> description = metadata.getOutputFields().get(DublinCore.PROPERTY_DESCRIPTION.getLocalName());
    metadata.removeField(description);
    MetadataField<String> newDescription = MetadataUtils.copyMetadataField(description);
    newDescription.setValue(series.getDescription());
    metadata.addField(newDescription);
    MetadataField<?> language = metadata.getOutputFields().get(DublinCore.PROPERTY_LANGUAGE.getLocalName());
    metadata.removeField(language);
    MetadataField<String> newLanguage = MetadataUtils.copyMetadataField(language);
    newLanguage.setValue(series.getLanguage());
    metadata.addField(newLanguage);
    MetadataField<?> rightsHolder = metadata.getOutputFields().get(DublinCore.PROPERTY_RIGHTS_HOLDER.getLocalName());
    metadata.removeField(rightsHolder);
    MetadataField<String> newRightsHolder = MetadataUtils.copyMetadataField(rightsHolder);
    newRightsHolder.setValue(series.getRightsHolder());
    metadata.addField(newRightsHolder);
    MetadataField<?> license = metadata.getOutputFields().get(DublinCore.PROPERTY_LICENSE.getLocalName());
    metadata.removeField(license);
    MetadataField<String> newLicense = MetadataUtils.copyMetadataField(license);
    newLicense.setValue(series.getLicense());
    metadata.addField(newLicense);
    MetadataField<?> organizers = metadata.getOutputFields().get(DublinCore.PROPERTY_CREATOR.getLocalName());
    metadata.removeField(organizers);
    MetadataField<String> newOrganizers = MetadataUtils.copyMetadataField(organizers);
    newOrganizers.setValue(StringUtils.join(series.getOrganizers(), ", "));
    metadata.addField(newOrganizers);
    MetadataField<?> contributors = metadata.getOutputFields().get(DublinCore.PROPERTY_CONTRIBUTOR.getLocalName());
    metadata.removeField(contributors);
    MetadataField<String> newContributors = MetadataUtils.copyMetadataField(contributors);
    newContributors.setValue(StringUtils.join(series.getContributors(), ", "));
    metadata.addField(newContributors);
    MetadataField<?> publishers = metadata.getOutputFields().get(DublinCore.PROPERTY_PUBLISHER.getLocalName());
    metadata.removeField(publishers);
    MetadataField<String> newPublishers = MetadataUtils.copyMetadataField(publishers);
    newPublishers.setValue(StringUtils.join(series.getPublishers(), ", "));
    metadata.addField(newPublishers);
    // Admin UI only field
    MetadataField<String> createdBy = MetadataField.createTextMetadataField("createdBy", Opt.<String>none(), "EVENTS.SERIES.DETAILS.METADATA.CREATED_BY", true, false, Opt.<Boolean>none(), Opt.<Map<String, String>>none(), Opt.<String>none(), Opt.some(CREATED_BY_UI_ORDER), Opt.<String>none());
    createdBy.setValue(series.getCreator());
    metadata.addField(createdBy);
    MetadataField<?> uid = metadata.getOutputFields().get(DublinCore.PROPERTY_IDENTIFIER.getLocalName());
    metadata.removeField(uid);
    MetadataField<String> newUID = MetadataUtils.copyMetadataField(uid);
    newUID.setValue(series.getIdentifier());
    metadata.addField(newUID);
    ExternalMetadataUtils.removeCollectionList(metadata);
    return metadata;
}
Also used : MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection)

Example 18 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.

the class SeriesEndpoint method deserializeMetadataList.

/**
 * Change the simplified fields of key values provided to the external api into a {@link MetadataList}.
 *
 * @param json
 *          The json string that contains an array of metadata field lists for the different catalogs.
 * @return A {@link MetadataList} with the fields populated with the values provided.
 * @throws ParseException
 *           Thrown if unable to parse the json string.
 * @throws NotFoundException
 *           Thrown if unable to find the catalog or field that the json refers to.
 */
protected MetadataList deserializeMetadataList(String json) throws ParseException, NotFoundException {
    MetadataList metadataList = new MetadataList();
    JSONParser parser = new JSONParser();
    JSONArray jsonCatalogs = (JSONArray) parser.parse(json);
    for (int i = 0; i < jsonCatalogs.size(); i++) {
        JSONObject catalog = (JSONObject) jsonCatalogs.get(i);
        if (catalog.get("flavor") == null || StringUtils.isBlank(catalog.get("flavor").toString())) {
            throw new IllegalArgumentException("Unable to create new series as no flavor was given for one of the metadata collections");
        }
        String flavorString = catalog.get("flavor").toString();
        MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(flavorString);
        MetadataCollection collection = null;
        SeriesCatalogUIAdapter adapter = null;
        for (SeriesCatalogUIAdapter seriesCatalogUIAdapter : indexService.getSeriesCatalogUIAdapters()) {
            MediaPackageElementFlavor catalogFlavor = MediaPackageElementFlavor.parseFlavor(seriesCatalogUIAdapter.getFlavor());
            if (catalogFlavor.equals(flavor)) {
                adapter = seriesCatalogUIAdapter;
                collection = seriesCatalogUIAdapter.getRawFields();
            }
        }
        if (collection == null) {
            throw new IllegalArgumentException(String.format("Unable to find an SeriesCatalogUIAdapter with Flavor '%s'", flavorString));
        }
        String fieldsJson = catalog.get("fields").toString();
        if (StringUtils.trimToNull(fieldsJson) != null) {
            Map<String, String> fields = RequestUtils.getKeyValueMap(fieldsJson);
            for (String key : fields.keySet()) {
                if ("subjects".equals(key)) {
                    MetadataField<?> field = collection.getOutputFields().get("subject");
                    if (field == null) {
                        throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", key, flavorString));
                    }
                    collection.removeField(field);
                    try {
                        JSONArray subjects = (JSONArray) parser.parse(fields.get(key));
                        collection.addField(MetadataField.copyMetadataFieldWithValue(field, StringUtils.join(subjects.iterator(), ",")));
                    } catch (ParseException e) {
                        throw new IllegalArgumentException(String.format("Unable to parse the 'subjects' metadata array field because: %s", e.toString()));
                    }
                } else {
                    MetadataField<?> field = collection.getOutputFields().get(key);
                    if (field == null) {
                        throw new NotFoundException(String.format("Cannot find a metadata field with id '%s' from Catalog with Flavor '%s'.", key, flavorString));
                    }
                    collection.removeField(field);
                    collection.addField(MetadataField.copyMetadataFieldWithValue(field, fields.get(key)));
                }
            }
        }
        metadataList.add(adapter, collection);
    }
    return metadataList;
}
Also used : JSONArray(org.json.simple.JSONArray) NotFoundException(org.opencastproject.util.NotFoundException) SeriesCatalogUIAdapter(org.opencastproject.metadata.dublincore.SeriesCatalogUIAdapter) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) ParseException(org.json.simple.parser.ParseException)

Example 19 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.

the class IndexServiceImplTest method updatePresenters.

@Test
public void updatePresenters() throws IOException, org.osgi.service.cm.ConfigurationException {
    String nonUser1 = "Non User 1";
    String nonUser2 = "Non User 2";
    String nonUser3 = "Non User 3";
    Properties eventProperties = new Properties();
    InputStream in = getClass().getResourceAsStream("/episode-catalog.properties");
    eventProperties.load(in);
    in.close();
    Dictionary<String, String> properties = PropertiesUtil.toDictionary(eventProperties);
    SecurityService securityService = EasyMock.createMock(SecurityService.class);
    EasyMock.expect(securityService.getOrganization()).andStubReturn(organization);
    EasyMock.replay(securityService);
    UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser(user1.getUsername())).andStubReturn(user1);
    EasyMock.expect(userDirectoryService.loadUser(user2.getUsername())).andStubReturn(user2);
    EasyMock.expect(userDirectoryService.loadUser(user3.getUsername())).andStubReturn(user3);
    EasyMock.expect(userDirectoryService.loadUser(nonUser1)).andStubReturn(null);
    EasyMock.expect(userDirectoryService.loadUser(nonUser2)).andStubReturn(null);
    EasyMock.expect(userDirectoryService.loadUser(nonUser3)).andStubReturn(null);
    EasyMock.replay(userDirectoryService);
    IndexServiceImpl indexServiceImpl = new IndexServiceImpl();
    CommonEventCatalogUIAdapter commonEventCatalogUIAdapter = new CommonEventCatalogUIAdapter();
    commonEventCatalogUIAdapter.updated(properties);
    indexServiceImpl.setCommonEventCatalogUIAdapter(commonEventCatalogUIAdapter);
    indexServiceImpl.setUserDirectoryService(userDirectoryService);
    indexServiceImpl.setSecurityService(securityService);
    MetadataCollection metadata = commonEventCatalogUIAdapter.getRawFields();
    // Possible presenter combinations
    MetadataField<Iterable<String>> emptyUpdatedPresenter = createCreatorMetadataField(new ArrayList<String>());
    ArrayList<String> oneNonUserList = new ArrayList<>();
    oneNonUserList.add(nonUser1);
    MetadataField<Iterable<String>> nonUserUpdatedPresenter = createCreatorMetadataField(oneNonUserList);
    ArrayList<String> multiNonUserList = new ArrayList<>();
    multiNonUserList.add(nonUser1);
    multiNonUserList.add(nonUser2);
    multiNonUserList.add(nonUser3);
    MetadataField<Iterable<String>> multiNonUserUpdatedPresenter = createCreatorMetadataField(multiNonUserList);
    ArrayList<String> oneUserList = new ArrayList<>();
    oneUserList.add(user1.getUsername());
    MetadataField<Iterable<String>> userUpdatedPresenter = createCreatorMetadataField(oneUserList);
    ArrayList<String> multiUserList = new ArrayList<>();
    multiUserList.add(user1.getUsername());
    multiUserList.add(user2.getUsername());
    multiUserList.add(user3.getUsername());
    MetadataField<Iterable<String>> multiUserUpdatedPresenter = createCreatorMetadataField(multiUserList);
    ArrayList<String> mixedUserList = new ArrayList<>();
    mixedUserList.add(user1.getUsername());
    mixedUserList.add(nonUser1);
    mixedUserList.add(user2.getUsername());
    mixedUserList.add(nonUser2);
    mixedUserList.add(nonUser3);
    mixedUserList.add(user3.getUsername());
    MetadataField<Iterable<String>> mixedPresenters = createCreatorMetadataField(mixedUserList);
    ArrayList<String> userFullNames = new ArrayList<>();
    userFullNames.add(user1.getName());
    userFullNames.add(user2.getName());
    userFullNames.add(user3.getUsername());
    // Empty presenters
    metadata.removeField(metadata.getOutputFields().get(DublinCore.PROPERTY_CREATOR.getLocalName()));
    metadata.addField(emptyUpdatedPresenter);
    Tuple<List<String>, Set<String>> updatedPresenters = indexServiceImpl.getTechnicalPresenters(metadata);
    assertTrue("The presenters dublincore metadata should be empty", updatedPresenters.getA().isEmpty());
    assertTrue("The technical presenters should be empty", updatedPresenters.getB().isEmpty());
    // Non-user presenter
    metadata.removeField(metadata.getOutputFields().get(DublinCore.PROPERTY_CREATOR.getLocalName()));
    metadata.addField(nonUserUpdatedPresenter);
    updatedPresenters = indexServiceImpl.getTechnicalPresenters(metadata);
    assertTrue("There should be one presenter", updatedPresenters.getA().size() == 1);
    assertTrue("There should be no technical presenters", updatedPresenters.getB().isEmpty());
    // Multi non-user presenter
    metadata.removeField(metadata.getOutputFields().get(DublinCore.PROPERTY_CREATOR.getLocalName()));
    metadata.addField(multiNonUserUpdatedPresenter);
    updatedPresenters = indexServiceImpl.getTechnicalPresenters(metadata);
    assertTrue("There should be three presenters", updatedPresenters.getA().size() == 3);
    assertTrue("The value for technical presenters should be empty", updatedPresenters.getB().isEmpty());
    // User presenter
    metadata.removeField(metadata.getOutputFields().get(DublinCore.PROPERTY_CREATOR.getLocalName()));
    metadata.addField(userUpdatedPresenter);
    updatedPresenters = indexServiceImpl.getTechnicalPresenters(metadata);
    assertTrue("There should be one presenter", updatedPresenters.getA().size() == 1);
    assertEquals("The one presenter should have the user's full name", "User 1", updatedPresenters.getA().get(0));
    assertTrue("The one technical presenter", updatedPresenters.getB().size() == 1);
    assertEquals("The one technical presenter has the correct username", "user1", updatedPresenters.getB().iterator().next());
    // Multi user presenter
    metadata.removeField(metadata.getOutputFields().get(DublinCore.PROPERTY_CREATOR.getLocalName()));
    metadata.addField(multiUserUpdatedPresenter);
    updatedPresenters = indexServiceImpl.getTechnicalPresenters(metadata);
    assertTrue("There should be three presenters", updatedPresenters.getA().size() == 3);
    assertTrue("There should be three technical presenters", updatedPresenters.getB().size() == 3);
    assertTrue("The list of technical presenters should contain all of the user names", updatedPresenters.getB().containsAll(multiUserList));
    // Mixed non-user and user presenters
    metadata.removeField(metadata.getOutputFields().get(DublinCore.PROPERTY_CREATOR.getLocalName()));
    metadata.addField(mixedPresenters);
    updatedPresenters = indexServiceImpl.getTechnicalPresenters(metadata);
    assertTrue("There should be six presenters", updatedPresenters.getA().size() == 6);
    assertTrue("There should be three technical presenters", updatedPresenters.getB().size() == 3);
    assertTrue("The list of presenters should contain all of the non-user names", updatedPresenters.getA().containsAll(multiNonUserList));
    assertTrue("The list of presenters should contain all of the user full names", updatedPresenters.getA().containsAll(userFullNames));
    assertTrue("The list of technical presenters should contain all of the usernames", updatedPresenters.getB().containsAll(multiUserList));
}
Also used : Set(java.util.Set) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Properties(java.util.Properties) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) SecurityService(org.opencastproject.security.api.SecurityService) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) DublinCoreMetadataCollection(org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataCollection) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) List(java.util.List) MetadataList(org.opencastproject.index.service.catalog.adapter.MetadataList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) AccessControlList(org.opencastproject.security.api.AccessControlList) Test(org.junit.Test)

Example 20 with MetadataCollection

use of org.opencastproject.metadata.dublincore.MetadataCollection in project opencast by opencast.

the class IndexServiceImplTest method setupCommonCatalogUIAdapter.

private Tuple<CommonEventCatalogUIAdapter, VCell<Option<MetadataCollection>>> setupCommonCatalogUIAdapter(Workspace workspace) throws org.osgi.service.cm.ConfigurationException {
    // Create Common Event Catalog UI Adapter
    final VCell<Option<MetadataCollection>> metadataCell = VCell.ocell();
    CommonEventCatalogUIAdapter commonEventCatalogUIAdapter = new CommonEventCatalogUIAdapter() {

        @Override
        public Catalog storeFields(MediaPackage mediaPackage, MetadataCollection metadata) {
            metadataCell.set(Option.some(metadata));
            return super.storeFields(mediaPackage, metadata);
        }
    };
    Properties episodeCatalogProperties = new Properties();
    InputStream in = null;
    try {
        in = getClass().getResourceAsStream("/episode-catalog.properties");
        episodeCatalogProperties.load(in);
    } catch (IOException e) {
        throw new ComponentException(e);
    } finally {
        IoSupport.closeQuietly(in);
    }
    commonEventCatalogUIAdapter.updated(PropertiesUtil.toDictionary(episodeCatalogProperties));
    commonEventCatalogUIAdapter.setWorkspace(workspace);
    return Tuple.tuple(commonEventCatalogUIAdapter, metadataCell);
}
Also used : InputStream(java.io.InputStream) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ComponentException(org.osgi.service.component.ComponentException) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) Option(org.opencastproject.util.data.Option) DublinCoreMetadataCollection(org.opencastproject.index.service.catalog.adapter.DublinCoreMetadataCollection) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) IOException(java.io.IOException) Properties(java.util.Properties)

Aggregations

MetadataCollection (org.opencastproject.metadata.dublincore.MetadataCollection)36 MetadataList (org.opencastproject.index.service.catalog.adapter.MetadataList)16 Test (org.junit.Test)10 CommonEventCatalogUIAdapter (org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter)9 JSONArray (org.json.simple.JSONArray)7 JSONParser (org.json.simple.parser.JSONParser)7 MediaPackage (org.opencastproject.mediapackage.MediaPackage)7 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)7 EventCatalogUIAdapter (org.opencastproject.metadata.dublincore.EventCatalogUIAdapter)7 NotFoundException (org.opencastproject.util.NotFoundException)7 Path (javax.ws.rs.Path)6 AbstractMetadataCollection (org.opencastproject.index.service.catalog.adapter.AbstractMetadataCollection)6 RestQuery (org.opencastproject.util.doc.rest.RestQuery)6 IOException (java.io.IOException)5 JSONObject (org.json.simple.JSONObject)5 ParseException (org.json.simple.parser.ParseException)5 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)5 SimpleDateFormat (java.text.SimpleDateFormat)4 Date (java.util.Date)4 GET (javax.ws.rs.GET)4