Search in sources :

Example 1 with TransientNonEmptyString

use of org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString in project ORCID-Source by ORCID.

the class ActivityValidator method checkExternalIdentifiersForDuplicates.

public void checkExternalIdentifiersForDuplicates(ExternalIDs newExtIds, ExternalIDs existingExtIds, Source existingSource, SourceEntity sourceEntity) {
    if (existingExtIds != null && newExtIds != null) {
        for (ExternalID existingId : existingExtIds.getExternalIdentifier()) {
            for (ExternalID newId : newExtIds.getExternalIdentifier()) {
                // normalize the ids before checking equality
                newId.setNormalized(new TransientNonEmptyString(norm.normalise(newId.getType(), newId.getValue())));
                if (existingId.getNormalized() == null)
                    existingId.setNormalized(new TransientNonEmptyString(norm.normalise(existingId.getType(), existingId.getValue())));
                if (areRelationshipsSameButNotBothPartOf(existingId.getRelationship(), newId.getRelationship()) && newId.equals(existingId) && sourceEntity.getSourceId().equals(getExistingSource(existingSource))) {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("clientName", sourceEntity.getSourceName());
                    throw new OrcidDuplicatedActivityException(params);
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ExternalID(org.orcid.jaxb.model.v3.dev1.record.ExternalID) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString)

Example 2 with TransientNonEmptyString

use of org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString in project ORCID-Source by ORCID.

the class JSONWorkExternalIdentifiersConverterV3 method convertFrom.

@Override
public ExternalIDs convertFrom(String source, Type<ExternalIDs> destinationType) {
    JSONWorkExternalIdentifiers workExternalIdentifiers = JsonUtils.readObjectFromJsonString(source, JSONWorkExternalIdentifiers.class);
    ExternalIDs externalIDs = new ExternalIDs();
    for (JSONWorkExternalIdentifier workExternalIdentifier : workExternalIdentifiers.getWorkExternalIdentifier()) {
        ExternalID id = new ExternalID();
        if (workExternalIdentifier.getWorkExternalIdentifierType() == null) {
            id.setType(WorkExternalIdentifierType.OTHER_ID.value());
        } else {
            id.setType(conv.convertFrom(workExternalIdentifier.getWorkExternalIdentifierType(), null));
        }
        if (workExternalIdentifier.getWorkExternalIdentifierId() != null) {
            id.setValue(workExternalIdentifier.getWorkExternalIdentifierId().content);
            // note, uses API type name.
            id.setNormalized(new TransientNonEmptyString(norm.normalise(id.getType(), workExternalIdentifier.getWorkExternalIdentifierId().content)));
            if (StringUtils.isEmpty(id.getNormalized().getValue())) {
                id.setNormalizedError(new TransientError(localeManager.resolveMessage("transientError.normalization_failed.code"), localeManager.resolveMessage("transientError.normalization_failed.message", id.getType(), workExternalIdentifier.getWorkExternalIdentifierId().content)));
            }
        }
        if (workExternalIdentifier.getUrl() != null) {
            id.setUrl(new Url(workExternalIdentifier.getUrl().getValue()));
        }
        if (workExternalIdentifier.getRelationship() != null) {
            id.setRelationship(Relationship.fromValue(conv.convertFrom(workExternalIdentifier.getRelationship(), null)));
        }
        externalIDs.getExternalIdentifier().add(id);
    }
    return externalIDs;
}
Also used : ExternalIDs(org.orcid.jaxb.model.v3.dev1.record.ExternalIDs) JSONWorkExternalIdentifiers(org.orcid.core.adapter.jsonidentifier.JSONWorkExternalIdentifiers) TransientError(org.orcid.jaxb.model.v3.dev1.common.TransientError) ExternalID(org.orcid.jaxb.model.v3.dev1.record.ExternalID) JSONWorkExternalIdentifier(org.orcid.core.adapter.jsonidentifier.JSONWorkExternalIdentifier) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString) JSONUrl(org.orcid.core.adapter.jsonidentifier.JSONUrl) Url(org.orcid.jaxb.model.v3.dev1.common.Url)

Example 3 with TransientNonEmptyString

use of org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString in project ORCID-Source by ORCID.

the class WorkManagerImpl method createWorks.

/**
 * Add a list of works to the given profile
 *
 * @param works
 *            The list of works that want to be added
 * @param orcid
 *            The id of the user we want to add the works to
 *
 * @return the work bulk with the put codes of the new works or the error
 *         that indicates why a work can't be added
 */
@Override
@Transactional
public WorkBulk createWorks(String orcid, WorkBulk workBulk) {
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    List<Work> existingWorks = this.findWorks(orcid);
    if (workBulk.getBulk() != null && !workBulk.getBulk().isEmpty()) {
        List<BulkElement> bulk = workBulk.getBulk();
        Set<ExternalID> existingExternalIdentifiers = buildExistingExternalIdsSet(existingWorks, sourceEntity.getSourceId());
        if ((existingWorks.size() + bulk.size()) > this.maxNumOfActivities) {
            throw new ExceedMaxNumberOfElementsException();
        }
        // Check bulk size
        if (bulk.size() > maxBulkSize) {
            Locale locale = localeManager.getLocale();
            throw new IllegalArgumentException(messageSource.getMessage("apiError.validation_too_many_elements_in_bulk.exception", new Object[] { maxBulkSize }, locale));
        }
        for (int i = 0; i < bulk.size(); i++) {
            if (Work.class.isAssignableFrom(bulk.get(i).getClass())) {
                Work work = (Work) bulk.get(i);
                try {
                    activityValidator.validateWork(work, sourceEntity, true, true, null);
                    // Validate it is not duplicated
                    if (work.getExternalIdentifiers() != null) {
                        for (ExternalID extId : work.getExternalIdentifiers().getExternalIdentifier()) {
                            // normalise the provided ID
                            extId.setNormalized(new TransientNonEmptyString(norm.normalise(extId.getType(), extId.getValue())));
                            // If the external id exists and is a SELF identifier, then mark it as duplicated
                            if (existingExternalIdentifiers.contains(extId) && Relationship.SELF.equals(extId.getRelationship())) {
                                Map<String, String> params = new HashMap<String, String>();
                                params.put("clientName", sourceEntity.getSourceName());
                                throw new OrcidDuplicatedActivityException(params);
                            }
                        }
                    }
                    // Save the work
                    WorkEntity workEntity = jpaJaxbWorkAdapter.toWorkEntity(work);
                    ProfileEntity profile = profileEntityCacheManager.retrieve(orcid);
                    workEntity.setOrcid(orcid);
                    workEntity.setAddedToProfileDate(new Date());
                    // Set source id
                    if (sourceEntity.getSourceProfile() != null) {
                        workEntity.setSourceId(sourceEntity.getSourceProfile().getId());
                    }
                    if (sourceEntity.getSourceClient() != null) {
                        workEntity.setClientSourceId(sourceEntity.getSourceClient().getId());
                    }
                    setIncomingWorkPrivacy(workEntity, profile);
                    DisplayIndexCalculatorHelper.setDisplayIndexOnNewEntity(workEntity, true);
                    workDao.persist(workEntity);
                    // Update the element in the bulk
                    Work updatedWork = jpaJaxbWorkAdapter.toWork(workEntity);
                    bulk.set(i, updatedWork);
                    // Add the work extIds to the list of existing external identifiers
                    addExternalIdsToExistingSet(updatedWork, existingExternalIdentifiers);
                } catch (Exception e) {
                    // Get the exception
                    OrcidError orcidError = orcidCoreExceptionMapper.getV3OrcidError(e);
                    bulk.set(i, orcidError);
                }
            }
        }
        workDao.flush();
    }
    return workBulk;
}
Also used : Locale(java.util.Locale) OrcidError(org.orcid.jaxb.model.v3.dev1.error.OrcidError) HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ExternalID(org.orcid.jaxb.model.v3.dev1.record.ExternalID) ExceedMaxNumberOfElementsException(org.orcid.core.exception.ExceedMaxNumberOfElementsException) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Date(java.util.Date) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) ExceedMaxNumberOfElementsException(org.orcid.core.exception.ExceedMaxNumberOfElementsException) WorkEntity(org.orcid.persistence.jpa.entities.WorkEntity) MinimizedWorkEntity(org.orcid.persistence.jpa.entities.MinimizedWorkEntity) BulkElement(org.orcid.jaxb.model.record.bulk.BulkElement) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) Work(org.orcid.jaxb.model.v3.dev1.record.Work) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with TransientNonEmptyString

use of org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString in project ORCID-Source by ORCID.

the class ExternalIDNormalizedValueTest method testEqualsAndHashcode.

@Test
public void testEqualsAndHashcode() {
    ExternalID id1 = new ExternalID();
    id1.setType("doi");
    id1.setValue("VALUE");
    id1.setNormalized(new TransientNonEmptyString("value"));
    ExternalID id2 = new ExternalID();
    id2.setType("doi");
    id2.setValue("value");
    id2.setNormalized(new TransientNonEmptyString("value"));
    assertEquals(id1, id2);
    assertEquals(id1.hashCode(), id2.hashCode());
    Set<ExternalID> set1 = new HashSet<ExternalID>();
    id1.getNormalized().setValue("value");
    set1.add(id1);
    set1.add(id2);
    assertEquals(1, set1.size());
    id1.getNormalized().setValue("VALUE");
    assertNotEquals(id1, id2);
    assertNotEquals(id1.hashCode(), id2.hashCode());
    Set<ExternalID> set2 = new HashSet<ExternalID>();
    set2.add(id1);
    set2.add(id2);
    assertEquals(2, set2.size());
}
Also used : ExternalID(org.orcid.jaxb.model.v3.dev1.record.ExternalID) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with TransientNonEmptyString

use of org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString in project ORCID-Source by ORCID.

the class TransientNonEmptyStringTest method testMarshal.

/**
 *    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 *    <common:external-ids xmlns:internal="http://www.orcid.org/ns/internal" xmlns:address="http://www.orcid.org/ns/address" xmlns:email="http://www.orcid.org/ns/email" xmlns:history="http://www.orcid.org/ns/history" xmlns:employment="http://www.orcid.org/ns/employment" xmlns:person="http://www.orcid.org/ns/person" xmlns:education="http://www.orcid.org/ns/education" xmlns:other-name="http://www.orcid.org/ns/other-name" xmlns:personal-details="http://www.orcid.org/ns/personal-details" xmlns:bulk="http://www.orcid.org/ns/bulk" xmlns:common="http://www.orcid.org/ns/common" xmlns:record="http://www.orcid.org/ns/record" xmlns:keyword="http://www.orcid.org/ns/keyword" xmlns:activities="http://www.orcid.org/ns/activities" xmlns:deprecated="http://www.orcid.org/ns/deprecated" xmlns:external-identifier="http://www.orcid.org/ns/external-identifier" xmlns:funding="http://www.orcid.org/ns/funding" xmlns:error="http://www.orcid.org/ns/error" xmlns:preferences="http://www.orcid.org/ns/preferences" xmlns:work="http://www.orcid.org/ns/work" xmlns:researcher-url="http://www.orcid.org/ns/researcher-url" xmlns:peer-review="http://www.orcid.org/ns/peer-review">
 *        <common:external-id>
 *            <common:external-id-type>doi</common:external-id-type>
 *            <common:external-id-value>value</common:external-id-value>
 *            <common:external-id-normalized transient="true">normalized-value</common:external-id-normalized>
 *        </common:external-id>
 *    </common:external-ids>
 * @throws JAXBException
 * @throws SAXException
 * @throws IOException
 */
@Test
public void testMarshal() throws JAXBException, SAXException, IOException {
    ExternalIDs ids = new ExternalIDs();
    ExternalID id = new ExternalID();
    id.setType("doi");
    id.setValue("value");
    id.setNormalized(new TransientNonEmptyString("normalized-value"));
    ids.getExternalIdentifier().add(id);
    JAXBContext context = JAXBContext.newInstance(ExternalIDs.class);
    Marshaller marshaller = context.createMarshaller();
    StringWriter sw = new StringWriter();
    marshaller.marshal(ids, sw);
    assertTrue(sw.toString().contains("<common:external-id-normalized transient=\"true\">normalized-value</common:external-id-normalized>"));
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/common_3.0_dev1/common-3.0_dev1.xsd"));
    Validator validator = schema.newValidator();
    validator.validate(new JAXBSource(context, ids));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Marshaller(javax.xml.bind.Marshaller) ExternalIDs(org.orcid.jaxb.model.v3.dev1.record.ExternalIDs) StringWriter(java.io.StringWriter) ExternalID(org.orcid.jaxb.model.v3.dev1.record.ExternalID) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) TransientNonEmptyString(org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString) Validator(javax.xml.validation.Validator) JAXBSource(javax.xml.bind.util.JAXBSource) Test(org.junit.Test)

Aggregations

TransientNonEmptyString (org.orcid.jaxb.model.v3.dev1.common.TransientNonEmptyString)11 ExternalID (org.orcid.jaxb.model.v3.dev1.record.ExternalID)11 Test (org.junit.Test)8 ExternalIDs (org.orcid.jaxb.model.v3.dev1.record.ExternalIDs)4 HashMap (java.util.HashMap)2 OrcidDuplicatedActivityException (org.orcid.core.exception.OrcidDuplicatedActivityException)2 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)2 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 JAXBSource (javax.xml.bind.util.JAXBSource)1 Schema (javax.xml.validation.Schema)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 Validator (javax.xml.validation.Validator)1 JSONUrl (org.orcid.core.adapter.jsonidentifier.JSONUrl)1 JSONWorkExternalIdentifier (org.orcid.core.adapter.jsonidentifier.JSONWorkExternalIdentifier)1