private int comparePublicationDate(WorkEntity other) { if (other.getPublicationDate() == null) { if (this.publicationDate == null) { return 0; } else { return 1; } } else if (this.publicationDate == null) { return -1; } return this.publicationDate.compareTo(other.getPublicationDate()); }
private int compareTitles(WorkEntity other) { if (other.getTitle() == null) { if (title == null) { return 0; } else { return 1; } } if (title == null) { return -1; } return title.compareToIgnoreCase(other.getTitle()); }
@Override @Transactional public WorkEntity editWork(WorkEntity updatedWork) { WorkEntity workToUpdate = this.find(updatedWork.getId()); mergeWork(workToUpdate, updatedWork); workToUpdate = this.merge(workToUpdate); return workToUpdate; }
public int compare(WorkEntity work1, WorkEntity work2) { if (work2 == null) { throw new NullPointerException("Can't compare with null"); } // Negate the result (Multiply it by -1) to reverse the order. int comparison = work1.comparePublicationDate(work2) * -1; if (comparison == 0) { comparison = work1.compareTitles(work2); if (comparison == 0) { return work1.compareIds(work2); } } return comparison; }
private int compareIds(WorkEntity other) { if (other.getId() == null) { if (id == null) { if (equals(other)) { return 0; } else { // If can't determine preferred order, then be polite and // say 'after you!' return -1; } } else { return 1; } } if (id == null) { return -1; } return id.compareTo(other.getId()); }
private void mergeWork(WorkEntity workToUpdate, WorkEntity workWithNewData) { workToUpdate.setTitle(workWithNewData.getTitle()); workToUpdate.setTranslatedTitle(workWithNewData.getTranslatedTitle()); workToUpdate.setSubtitle(workWithNewData.getSubtitle()); workToUpdate.setDescription(workWithNewData.getDescription()); workToUpdate.setWorkUrl(workWithNewData.getWorkUrl()); workToUpdate.setCitation(workWithNewData.getCitation()); workToUpdate.setJournalTitle(workWithNewData.getJournalTitle()); workToUpdate.setLanguageCode(workWithNewData.getLanguageCode()); workToUpdate.setTranslatedTitleLanguageCode(workWithNewData.getTranslatedTitleLanguageCode()); workToUpdate.setIso2Country(workWithNewData.getIso2Country()); workToUpdate.setCitationType(workWithNewData.getCitationType()); workToUpdate.setWorkType(workWithNewData.getWorkType()); workToUpdate.setPublicationDate(workWithNewData.getPublicationDate()); workToUpdate.setContributorsJson(workWithNewData.getContributorsJson()); workToUpdate.setExternalIdentifiersJson(workWithNewData.getExternalIdentifiersJson()); workToUpdate.setVisibility(workWithNewData.getVisibility()); workToUpdate.setDisplayIndex(workWithNewData.getDisplayIndex()); workToUpdate.setSource(workWithNewData.getSource()); workToUpdate.setLastModified(new Date()); if (workWithNewData.getAddedToProfileDate() != null) { workToUpdate.setAddedToProfileDate(workWithNewData.getAddedToProfileDate()); } workToUpdate.setProfile(workWithNewData.getProfile()); }
@Test @Rollback(true) @Transactional(propagation = Propagation.REQUIRES_NEW) public void testAddWork() { String title = "New Work"; String subtitle = "Subtitle"; String citation = "Test citation"; String description = "Description for new work"; String url = "http://work.com"; WorkEntity work = new WorkEntity(); work.setCitation(citation); work.setCitationType(CitationType.FORMATTED_UNSPECIFIED); work.setDescription(description); work.setTitle(title); work.setSubtitle(subtitle); work.setWorkType(WorkType.BOOK); work.setWorkUrl(url); ProfileEntity profile = new ProfileEntity(USER_ORCID); work.setProfile(profile); work.setSourceId(USER_ORCID); work.setAddedToProfileDate(new Date()); assertNull(work.getId()); try { work = workDao.addWork(work); } catch (Exception e) { fail(); } assertNotNull(work.getId()); }