001package org.kuali.rice.krms.impl.repository;
002
003import org.kuali.rice.core.api.criteria.QueryByCriteria;
004import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
005import org.kuali.rice.krad.data.DataObjectService;
006import org.kuali.rice.krad.data.PersistenceOption;
007import org.kuali.rice.krms.api.repository.term.TermDefinition;
008import org.kuali.rice.krms.impl.util.KrmsImplConstants;
009
010/**
011 * This class is a temp fix to resolve the issue https://jira.kuali.org/browse/KSENROLL-12746
012 * It was coded to use the constant TERM_ID("termId") instead of "term.id"
013 */
014public class TempFixTermBoServiceImpl extends TermBoServiceImpl {
015
016    protected DataObjectService dataObjectService;
017
018    @Override
019    public void setDataObjectService(DataObjectService dataObjectService) {
020        this.dataObjectService = dataObjectService;
021        super.setDataObjectService(dataObjectService);
022    }
023
024    @Override
025    public void updateTerm(TermDefinition term) throws RiceIllegalArgumentException {
026        if (term == null) {
027            throw new IllegalArgumentException("term is null");
028        }
029
030        // must already exist to be able to update
031        final String termId = term.getId();
032        final TermBo existing = dataObjectService.find(TermBo.class, termId);
033
034        if (existing == null) {
035            throw new IllegalStateException("the term resolver does not exist: " + term);
036        }
037
038        final TermDefinition toUpdate;
039
040        if (!existing.getId().equals(term.getId())) {
041            // if passed in id does not match existing id, correct it
042            final TermDefinition.Builder builder = TermDefinition.Builder.create(term);
043            builder.setId(existing.getId());
044            toUpdate = builder.build();
045        } else {
046            toUpdate = term;
047        }
048
049        // copy all updateable fields to bo
050        TermBo boToUpdate = TermBo.from(toUpdate);
051
052        // delete any old, existing parameters
053
054//        QueryByCriteria crit =
055//                QueryByCriteria.Builder.forAttribute("term.id", toUpdate.getId()).build();
056//        dataObjectService.deleteMatching(TermParameterBo.class, crit);
057
058        // update the rule and create new attributes
059        dataObjectService.save(boToUpdate, PersistenceOption.FLUSH);
060    }
061}