Search in sources :

Example 26 with WorkEntity

use of org.orcid.persistence.jpa.entities.WorkEntity 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();
    Set<ExternalID> existingExternalIdentifiers = buildExistingExternalIdsSet(orcid, sourceEntity.getSourceId());
    if (workBulk.getBulk() != null && !workBulk.getBulk().isEmpty()) {
        List<BulkElement> bulk = workBulk.getBulk();
        //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 {
                    //Validate the work
                    activityValidator.validateWork(work, sourceEntity, true, true, null);
                    //Validate it is not duplicated
                    if (work.getExternalIdentifiers() != null) {
                        for (ExternalID extId : work.getExternalIdentifiers().getExternalIdentifier()) {
                            if (existingExternalIdentifiers.contains(extId)) {
                                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.setProfile(profile);
                    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.getOrcidError(e);
                    bulk.set(i, orcidError);
                }
            }
        }
        workDao.flush();
    }
    return workBulk;
}
Also used : Locale(java.util.Locale) OrcidError(org.orcid.jaxb.model.error_v2.OrcidError) HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) ExternalID(org.orcid.jaxb.model.record_v2.ExternalID) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Date(java.util.Date) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) WorkEntity(org.orcid.persistence.jpa.entities.WorkEntity) BulkElement(org.orcid.jaxb.model.record_v2.BulkElement) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException) Work(org.orcid.jaxb.model.record_v2.Work) Transactional(org.springframework.transaction.annotation.Transactional)

Example 27 with WorkEntity

use of org.orcid.persistence.jpa.entities.WorkEntity in project ORCID-Source by ORCID.

the class WorkManagerImpl method checkSourceAndRemoveWork.

@Override
public boolean checkSourceAndRemoveWork(String orcid, Long workId) {
    boolean result = true;
    WorkEntity workEntity = workDao.getWork(orcid, workId);
    orcidSecurityManager.checkSource(workEntity);
    try {
        Item item = createItem(workEntity);
        workDao.removeWork(orcid, workId);
        workDao.flush();
        notificationManager.sendAmendEmail(orcid, AmendedSection.WORK, item);
    } catch (Exception e) {
        LOGGER.error("Unable to delete work with ID: " + workId);
        result = false;
    }
    return result;
}
Also used : WorkEntity(org.orcid.persistence.jpa.entities.WorkEntity) Item(org.orcid.jaxb.model.notification.permission_v2.Item) OrcidDuplicatedActivityException(org.orcid.core.exception.OrcidDuplicatedActivityException)

Aggregations

WorkEntity (org.orcid.persistence.jpa.entities.WorkEntity)27 Test (org.junit.Test)10 Work (org.orcid.jaxb.model.record_v2.Work)9 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)8 Transactional (org.springframework.transaction.annotation.Transactional)7 Date (java.util.Date)6 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)6 ArrayList (java.util.ArrayList)4 OrcidWork (org.orcid.jaxb.model.message.OrcidWork)4 MinimizedWorkEntity (org.orcid.persistence.jpa.entities.MinimizedWorkEntity)4 DBUnitTest (org.orcid.test.DBUnitTest)4 HashSet (java.util.HashSet)3 BaseTest (org.orcid.core.BaseTest)3 ExternalID (org.orcid.jaxb.model.record_v2.ExternalID)3 Rollback (org.springframework.test.annotation.Rollback)3 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 OrcidDuplicatedActivityException (org.orcid.core.exception.OrcidDuplicatedActivityException)2 OrcidMessage (org.orcid.jaxb.model.message.OrcidMessage)2 Item (org.orcid.jaxb.model.notification.permission_v2.Item)2