|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   package org.kuali.rice.krms.impl.repository;  | 
  |  18 |     | 
     | 
  |  19 |     | 
     | 
  |  20 |     | 
   import java.util.HashMap;  | 
  |  21 |     | 
   import java.util.List;  | 
  |  22 |     | 
   import java.util.Map;  | 
  |  23 |     | 
     | 
  |  24 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  25 |     | 
   import org.kuali.rice.krad.service.BusinessObjectService;  | 
  |  26 |     | 
   import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;  | 
  |  27 |     | 
   import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;  | 
  |  28 |     | 
     | 
  |  29 |    29 |    public final class PropositionBoServiceImpl implements PropositionBoService { | 
  |  30 |     | 
     | 
  |  31 |     | 
       private BusinessObjectService businessObjectService;  | 
  |  32 |     | 
     | 
  |  33 |     | 
             | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |     | 
           @Override  | 
  |  40 |     | 
           public PropositionDefinition createProposition(PropositionDefinition prop) { | 
  |  41 |    4 |                    if (prop == null){ | 
  |  42 |    1 |                    throw new IllegalArgumentException("proposition is null"); | 
  |  43 |     | 
                   }  | 
  |  44 |    3 |                    if (null != prop.getId()) { | 
  |  45 |    1 |                        throw new IllegalStateException("for creation, PropositionDefinition.id must be null"); | 
  |  46 |     | 
               }  | 
  |  47 |     | 
                     | 
  |  48 |    2 |                    PropositionBo propositionBo = PropositionBo.from(prop);  | 
  |  49 |    2 |                    businessObjectService.save(propositionBo);  | 
  |  50 |    2 |                    return PropositionBo.to(propositionBo);  | 
  |  51 |     | 
           }  | 
  |  52 |     | 
     | 
  |  53 |     | 
             | 
  |  54 |     | 
     | 
  |  55 |     | 
     | 
  |  56 |     | 
     | 
  |  57 |     | 
     | 
  |  58 |     | 
           @Override  | 
  |  59 |     | 
           public void updateProposition(PropositionDefinition prop) { | 
  |  60 |    3 |            if (prop == null) { | 
  |  61 |    1 |                throw new IllegalArgumentException("proposition is null"); | 
  |  62 |     | 
           }  | 
  |  63 |    2 |                    final String propIdKey = prop.getId();  | 
  |  64 |    2 |                    final PropositionDefinition existing = getPropositionById(propIdKey);  | 
  |  65 |    2 |            if (existing == null) { | 
  |  66 |    1 |                throw new IllegalStateException("the proposition does not exist: " + prop); | 
  |  67 |     | 
           }  | 
  |  68 |     | 
           final PropositionDefinition toUpdate;  | 
  |  69 |    1 |            if (!existing.getId().equals(prop.getId())){ | 
  |  70 |    0 |                    final PropositionDefinition.Builder builder = PropositionDefinition.Builder.create(prop);  | 
  |  71 |    0 |                    builder.setId(existing.getId());  | 
  |  72 |    0 |                    toUpdate = builder.build();  | 
  |  73 |    0 |            } else { | 
  |  74 |    1 |                    toUpdate = prop;  | 
  |  75 |     | 
           }  | 
  |  76 |     | 
             | 
  |  77 |    1 |            businessObjectService.save(PropositionBo.from(toUpdate));  | 
  |  78 |    1 |            }  | 
  |  79 |     | 
     | 
  |  80 |     | 
             | 
  |  81 |     | 
     | 
  |  82 |     | 
     | 
  |  83 |     | 
     | 
  |  84 |     | 
     | 
  |  85 |     | 
           @Override  | 
  |  86 |     | 
           public PropositionDefinition getPropositionById(String propId) { | 
  |  87 |    5 |                    if (StringUtils.isBlank(propId)){ | 
  |  88 |    1 |                throw new IllegalArgumentException("propId is null or blank");                         | 
  |  89 |     | 
                   }  | 
  |  90 |    4 |                    PropositionBo bo = businessObjectService.findBySinglePrimaryKey(PropositionBo.class, propId);  | 
  |  91 |    4 |                    return PropositionBo.to(bo);  | 
  |  92 |     | 
           }  | 
  |  93 |     | 
     | 
  |  94 |     | 
             | 
  |  95 |     | 
     | 
  |  96 |     | 
     | 
  |  97 |     | 
     | 
  |  98 |     | 
     | 
  |  99 |     | 
     | 
  |  100 |     | 
           @Override  | 
  |  101 |     | 
           public void createParameter(PropositionParameter parameter) { | 
  |  102 |    3 |                    if (parameter == null){ | 
  |  103 |    1 |                    throw new IllegalArgumentException("parameter is null"); | 
  |  104 |     | 
                   }  | 
  |  105 |    2 |                    final String propIdKey = parameter.getPropId();  | 
  |  106 |    2 |                    final Integer seqNoKey = parameter.getSequenceNumber();  | 
  |  107 |    2 |                    final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);  | 
  |  108 |    2 |                    if (existing != null && existing.getPropId().equals(propIdKey) && existing.getSequenceNumber().equals(seqNoKey)){ | 
  |  109 |    1 |                throw new IllegalStateException("the parameter to create already exists: " + parameter);                         | 
  |  110 |     | 
                   }  | 
  |  111 |     | 
                     | 
  |  112 |    1 |                    businessObjectService.save(PropositionParameterBo.from(parameter));  | 
  |  113 |    1 |            }  | 
  |  114 |     | 
     | 
  |  115 |     | 
             | 
  |  116 |     | 
     | 
  |  117 |     | 
     | 
  |  118 |     | 
     | 
  |  119 |     | 
     | 
  |  120 |     | 
           @Override  | 
  |  121 |     | 
           public void updateParameter(PropositionParameter parameter) { | 
  |  122 |    3 |            if (parameter == null) { | 
  |  123 |    1 |                throw new IllegalArgumentException("parameter is null"); | 
  |  124 |     | 
           }  | 
  |  125 |    2 |                    final String propIdKey = parameter.getPropId();  | 
  |  126 |    2 |                    final Integer seqNoKey = parameter.getSequenceNumber();  | 
  |  127 |    2 |                    final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);  | 
  |  128 |    2 |            if (existing == null) { | 
  |  129 |    1 |                throw new IllegalStateException("the parameter does not exist: " + parameter); | 
  |  130 |     | 
           }  | 
  |  131 |     | 
           final PropositionParameter toUpdate;  | 
  |  132 |    1 |            if (!existing.getId().equals(parameter.getId())){ | 
  |  133 |    0 |                    final PropositionParameter.Builder builder = PropositionParameter.Builder.create(parameter);  | 
  |  134 |    0 |                    builder.setId(existing.getId());  | 
  |  135 |    0 |                    toUpdate = builder.build();  | 
  |  136 |    0 |            } else { | 
  |  137 |    1 |                    toUpdate = parameter;  | 
  |  138 |     | 
           }  | 
  |  139 |     | 
             | 
  |  140 |    1 |            businessObjectService.save(PropositionParameterBo.from(toUpdate));  | 
  |  141 |    1 |            }  | 
  |  142 |     | 
     | 
  |  143 |     | 
             | 
  |  144 |     | 
     | 
  |  145 |     | 
     | 
  |  146 |     | 
     | 
  |  147 |     | 
     | 
  |  148 |     | 
           @Override  | 
  |  149 |     | 
           public List<PropositionParameter> getParameters(String propId) { | 
  |  150 |    4 |                    if (StringUtils.isBlank(propId)) { | 
  |  151 |    2 |                throw new IllegalArgumentException("propId is null or blank"); | 
  |  152 |     | 
                   }  | 
  |  153 |    2 |            final Map<String, Object> map = new HashMap<String, Object>();  | 
  |  154 |    2 |            map.put("propId", propId); | 
  |  155 |    2 |                    List<PropositionParameterBo> bos = (List<PropositionParameterBo>) businessObjectService.findMatchingOrderBy(PropositionParameterBo.class, map, "sequenceNumber", true);  | 
  |  156 |    2 |                    return PropositionParameterBo.to(bos);  | 
  |  157 |     | 
           }  | 
  |  158 |     | 
     | 
  |  159 |     | 
             | 
  |  160 |     | 
     | 
  |  161 |     | 
     | 
  |  162 |     | 
     | 
  |  163 |     | 
     | 
  |  164 |     | 
           @Override  | 
  |  165 |     | 
           public PropositionParameter getParameterById(String id) { | 
  |  166 |    4 |                    if (StringUtils.isBlank(id)) { | 
  |  167 |    2 |                throw new IllegalArgumentException("id is null or blank"); | 
  |  168 |     | 
                   }  | 
  |  169 |    2 |                    PropositionParameterBo bo = businessObjectService.findBySinglePrimaryKey(PropositionParameterBo.class, id);  | 
  |  170 |    2 |                    return PropositionParameterBo.to(bo);  | 
  |  171 |     | 
           }  | 
  |  172 |     | 
     | 
  |  173 |     | 
             | 
  |  174 |     | 
     | 
  |  175 |     | 
     | 
  |  176 |     | 
     | 
  |  177 |     | 
     | 
  |  178 |     | 
           @Override  | 
  |  179 |     | 
           public PropositionParameter getParameterByPropIdAndSequenceNumber(  | 
  |  180 |     | 
                           String propId, Integer sequenceNumber) { | 
  |  181 |    9 |                    if (StringUtils.isBlank(propId)) { | 
  |  182 |    2 |                throw new IllegalArgumentException("propId is null or blank"); | 
  |  183 |     | 
                   }  | 
  |  184 |    7 |                    if (sequenceNumber == null) { | 
  |  185 |    1 |                throw new IllegalArgumentException("sequenceNumber is null"); | 
  |  186 |     | 
                   }  | 
  |  187 |    6 |            final Map<String, Object> map = new HashMap<String, Object>();  | 
  |  188 |    6 |            map.put("propId", propId); | 
  |  189 |    6 |            map.put("sequenceNumber", sequenceNumber); | 
  |  190 |    6 |                    PropositionParameterBo bo = businessObjectService.findByPrimaryKey(PropositionParameterBo.class, map);  | 
  |  191 |    6 |                    return PropositionParameterBo.to(bo);  | 
  |  192 |     | 
           }  | 
  |  193 |     | 
             | 
  |  194 |     | 
       public void setBusinessObjectService(BusinessObjectService businessObjectService) { | 
  |  195 |    29 |            this.businessObjectService = businessObjectService;  | 
  |  196 |    29 |        }  | 
  |  197 |     | 
     | 
  |  198 |     | 
   }  |