+ @Override
+ public <T extends DefinedTermBase> Pager<T> findByRepresentationText(String label, Class<T> clazz, Integer pageSize, Integer pageNumber) {
+ Integer numberOfResults = dao.countDefinedTermByRepresentationText(label,clazz);
+
+ List<T> results = new ArrayList<T>();
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
+ results = dao.getDefinedTermByRepresentationText(label, clazz, pageSize, pageNumber);
+ }
+
+ return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
+ }
+
+ @Override
+ public <T extends DefinedTermBase> Pager<T> findByRepresentationAbbreviation(String abbrev, Class<T> clazz, Integer pageSize, Integer pageNumber) {
+ Integer numberOfResults = dao.countDefinedTermByRepresentationAbbrev(abbrev,clazz);
+
+ List<T> results = new ArrayList<T>();
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
+ results = dao.getDefinedTermByRepresentationAbbrev(abbrev, clazz, pageSize, pageNumber);
+ }
+
+ return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
+ }
+
+ @Override
+ public List<LanguageString> getAllLanguageStrings(int limit, int start) {
+ return languageStringDao.list(limit, start);
+ }
+
+ @Override
+ public List<Representation> getAllRepresentations(int limit, int start) {
+ return representationDao.list(limit,start);
+ }
+
+ @Override
+ public UUID saveLanguageData(LanguageStringBase languageData) {
+ return languageStringBaseDao.save(languageData).getUuid();
+ }
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.api.service.ServiceBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)
+ */
+ /** @deprecated use {@link #delete(DefinedTermBase, TermDeletionConfigurator)} instead
+ * to allow DeleteResult return type*/
+ @Override
+ @Deprecated
+ public DeleteResult delete(DefinedTermBase term){
+ DeleteResult result = new DeleteResult();
+
+ TermDeletionConfigurator defaultConfig = new TermDeletionConfigurator();
+ result = delete(term, defaultConfig);
+ return result;
+ }
+
+ @Override
+ @Deprecated
+ @Transactional(readOnly = false)
+ public DeleteResult delete(UUID termUuid){
+ DeleteResult result = new DeleteResult();
+
+ TermDeletionConfigurator defaultConfig = new TermDeletionConfigurator();
+ result = delete(dao.load(termUuid), defaultConfig);
+ return result;
+ }
+
+ @Override
+ public DeleteResult delete(DefinedTermBase term, TermDeletionConfigurator config){
+ if (config == null){
+ config = new TermDeletionConfigurator();
+ }
+// boolean isInternal = config.isInternal();
+
+ Set<DefinedTermBase> termsToSave = new HashSet<DefinedTermBase>();
+
+ DeleteResult result = isDeletable(term, config);
+ //CdmBase.deproxy(dao.merge(term), DefinedTermBase.class);
+ try {
+ //generalization of
+ Set<DefinedTermBase> specificTerms = term.getGeneralizationOf();
+ if (specificTerms.size()>0){
+ if (config.isDeleteGeneralizationOfRelations()){
+ DefinedTermBase generalTerm = term.getKindOf();
+ for (DefinedTermBase specificTerm: specificTerms){
+ term.removeGeneralization(specificTerm);
+ if (generalTerm != null){
+ generalTerm.addGeneralizationOf(specificTerm);
+ termsToSave.add(generalTerm);
+ }
+ }
+ }else{
+ //TODO Exception type
+ String message = "This term has specifing terms. Move or delete specifiing terms prior to delete or change delete configuration.";
+ result.addRelatedObjects(specificTerms);
+ result.setAbort();
+ Exception ex = new DataChangeNoRollbackException(message);
+ result.addException(ex);
+ }
+ }
+
+ //kind of
+ DefinedTermBase generalTerm = term.getKindOf();
+ if (generalTerm != null){
+ if (config.isDeleteKindOfRelations()){
+ generalTerm.removeGeneralization(term);
+ }else{
+ //TODO Exception type
+ String message = "This term is kind of another term. Move or delete kind of relationship prior to delete or change delete configuration.";
+ result.addRelatedObject(generalTerm);
+ result.setAbort();
+ DataChangeNoRollbackException ex = new DataChangeNoRollbackException(message);
+ result.addException(ex);
+ throw ex;
+ }
+ }
+
+ //part of
+ DefinedTermBase parentTerm = term.getPartOf();
+ if (parentTerm != null){
+ if (! config.isDeletePartOfRelations()){
+ //TODO Exception type
+ String message = "This term is included in another term. Remove from parent term prior to delete or change delete configuration.";
+ result.addRelatedObject(parentTerm);
+ result.setAbort();
+ DataChangeNoRollbackException ex = new DataChangeNoRollbackException(message);
+ result.addException(ex);
+ }
+ }
+
+
+ //included in
+ Set<DefinedTermBase> includedTerms = term.getIncludes();
+ if (includedTerms.size()> 0){
+// if (config.isDeleteIncludedTerms()){
+// for (DefinedTermBase includedTerm: includedTerms){
+// config.setCheck(true);
+// DeleteResult includedResult = this.delete(includedTerm, config);
+//// config.setCheck(isCheck);
+// result.includeResult(includedResult);
+// }
+// }else
+ if (config.isDeleteIncludedRelations()){
+ DefinedTermBase parent = term.getPartOf();
+ for (DefinedTermBase includedTerm: includedTerms){
+ term.removeIncludes(includedTerm);
+ if (parent != null){
+ parent.addIncludes(includedTerm);
+ termsToSave.add(parent);
+ }
+ }
+ }else{
+ //TODO Exception type
+ String message = "This term includes other terms. Move or delete included terms prior to delete or change delete configuration.";
+ result.addRelatedObjects(includedTerms);
+ result.setAbort();
+ Exception ex = new DataChangeNoRollbackException(message);
+ result.addException(ex);
+ }
+ }
+
+ //part of
+ if (parentTerm != null){
+ if (config.isDeletePartOfRelations()){
+ parentTerm.removeIncludes(term);
+ termsToSave.add(parentTerm);
+ }else{
+ //handelede before "included in"
+ }
+ }
+
+// relatedObjects;
+
+
+ if (result.isOk()){
+ TermVocabulary voc = term.getVocabulary();
+ if (voc!= null){
+ voc.removeTerm(term);
+ }
+ //TODO save voc
+ if (true /*!config.isInternal()*/){
+ dao.delete(term);
+ dao.saveOrUpdateAll(termsToSave);
+// for (DeleteResult.PersistPair persistPair : result.getObjectsToDelete()){
+// persistPair.dao.delete(persistPair.objectToPersist);
+// }
+// for (DeleteResult.PersistPair persistPair : result.getObjectsToSave()){
+// persistPair.dao.saveOrUpdate(persistPair.objectToPersist);
+// }
+
+ }
+ }
+ } catch (DataChangeNoRollbackException e) {
+ result.setStatus(Status.ERROR);
+ }
+ return result;
+ }
+
+ @Override
+ @Transactional(readOnly = false)
+ public DeleteResult delete(UUID termUuid, TermDeletionConfigurator config){
+ return delete(dao.load(termUuid), config);
+ }
+
+ @Override
+ @Transactional(readOnly = false)
+ public void updateTitleCache(Class<? extends DefinedTermBase> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<DefinedTermBase> cacheStrategy, IProgressMonitor monitor) {
+ //TODO shouldnt this be TermBase instead of DefinedTermBase
+ if (clazz == null){
+ clazz = DefinedTermBase.class;
+ }
+ super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
+ }
+
+ @Override
+ public DeleteResult isDeletable(DefinedTermBase term, DeleteConfiguratorBase config){
+ DeleteResult result = new DeleteResult();
+ Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(term);
+ if (references != null){
+ result.addRelatedObjects(references);
+ Iterator<CdmBase> iterator = references.iterator();
+ CdmBase ref;
+ while (iterator.hasNext()){
+ ref = iterator.next();
+ if (ref instanceof TermVocabulary){
+ result.getRelatedObjects().remove(ref);
+ }else{
+
+ String message = "An object of " + ref.getClass().getName() + " with ID " + ref.getId() + " is referencing the object" ;
+ result.addException(new ReferencedObjectUndeletableException(message));
+ result.setAbort();
+ }
+
+ }
+ }
+ return result;
+ }
+