Program: 188617
Project: 188617
Buggy source:
public static void appraisal(RodaUser user,SelectedItems<IndexedAIP> selected,boolean accept,String rejectReason) throws GenericException, AuthorizationDeniedException, RequestNotValidException, NotFoundException {
  List<String> listOfIds=consolidate(user,IndexedAIP.class,selected);
  ModelService model=RodaCoreFactory.getModelService();
  IndexService index=RodaCoreFactory.getIndexService();
  Date now=new Date();
  Map<String,Pair<Integer,Integer>> jobState=new HashMap<>();
  String userAgentId;
  try {
    boolean notifyAgent=true;
    PreservationMetadata pm=PremisV3Utils.createPremisUserAgentBinary(user.getName(),model,index,notifyAgent);
    userAgentId=pm.getId();
  }
 catch (  AlreadyExistsException e) {
    userAgentId=IdUtils.getUserAgentId(user.getName());
  }
catch (  ValidationException e) {
    throw new GenericException(e);
  }
  for (  String aipId : listOfIds) {
    AIP aip=model.retrieveAIP(aipId);
    String jobId=aip.getIngestJobId();
    if (accept) {
      aip.setState(AIPState.ACTIVE);
      model.updateAIPState(aip,user.getName());
      String id=IdUtils.createPreservationMetadataId(PreservationMetadataType.EVENT);
      PreservationEventType type=PreservationEventType.ACCESSION;
      String preservationEventDescription=AutoAcceptSIPPlugin.DESCRIPTION;
      List<LinkingIdentifier> sources=new ArrayList<>();
      List<LinkingIdentifier> outcomes=Arrays.asList(PluginHelper.getLinkingIdentifier(aipId,RodaConstants.PRESERVATION_LINKING_OBJECT_OUTCOME));
      PluginState outcome=PluginState.SUCCESS;
      String outcomeDetailNote=AutoAcceptSIPPlugin.SUCCESS_MESSAGE;
      String outcomeDetailExtension=null;
      boolean notifyEvent=true;
      try {
        ContentPayload premisEvent=PremisV3Utils.createPremisEventBinary(id,now,type.toString(),preservationEventDescription,sources,outcomes,outcome.name(),outcomeDetailNote,outcomeDetailExtension,Arrays.asList(userAgentId));
        model.createPreservationMetadata(PreservationMetadataType.EVENT,id,aipId,null,null,null,premisEvent,notifyEvent);
      }
 catch (      AlreadyExistsException|ValidationException e) {
        throw new GenericException(e);
      }
    }
 else {
      model.deleteAIP(aipId);
      try {
        Map<String,String> parameters=new HashMap<String,String>();
        parameters.put("String_Node_Str",aipId);
        Job job=new Job();
        job.setName(RiskIncidenceRemoverPlugin.class.getSimpleName() + "String_Node_Str" + job.getStartDate());
        job.setPlugin(RiskIncidenceRemoverPlugin.class.getName());
        job.setPluginParameters(parameters);
        job.setSourceObjects(new SelectedItemsAll<>(AIP.class.getName()));
        Jobs.createJob(user,job);
      }
 catch (      JobAlreadyStartedException e) {
        LOGGER.error("String_Node_Str");
      }
    }
    Report report=model.retrieveJobReport(jobId,aipId);
    Report reportItem=new Report();
    reportItem.setTitle("String_Node_Str");
    reportItem.setPlugin(user.getName());
    reportItem.setPluginDetails(rejectReason);
    reportItem.setPluginState(accept ? PluginState.SUCCESS : PluginState.FAILURE);
    reportItem.setOutcomeObjectState(accept ? AIPState.ACTIVE : AIPState.DELETED);
    reportItem.setDateCreated(now);
    report.addReport(reportItem);
    model.createOrUpdateJobReport(report);
    Pair<Integer,Integer> pair=jobState.get(jobId);
    if (pair == null) {
      jobState.put(jobId,Pair.create(1,accept ? 1 : 0));
    }
 else {
      jobState.put(jobId,Pair.create(pair.getFirst() + 1,pair.getSecond() + (accept ? 1 : 0)));
    }
  }
  for (  Entry<String,Pair<Integer,Integer>> entry : jobState.entrySet()) {
    String jobId=entry.getKey();
    int total=entry.getValue().getFirst();
    int accepted=entry.getValue().getSecond();
    int rejected=total - accepted;
    Job job=model.retrieveJob(jobId);
    if (rejected > 0) {
      job.getJobStats().setSourceObjectsProcessedWithSuccess(job.getJobStats().getSourceObjectsProcessedWithSuccess() - rejected);
      job.getJobStats().setSourceObjectsProcessedWithFailure(job.getJobStats().getSourceObjectsProcessedWithFailure() + rejected);
    }
    job.getJobStats().setOutcomeObjectsWithManualIntervention(job.getJobStats().getOutcomeObjectsWithManualIntervention() - total);
    model.createOrUpdateJob(job);
  }
  RodaCoreFactory.getIndexService().commit(IndexedAIP.class,Job.class,Report.class,IndexedPreservationEvent.class);
}

Fixed version (ground truth):
public static void appraisal(RodaUser user,SelectedItems<IndexedAIP> selected,boolean accept,String rejectReason) throws GenericException, AuthorizationDeniedException, RequestNotValidException, NotFoundException {
  List<String> listOfIds=consolidate(user,IndexedAIP.class,selected);
  ModelService model=RodaCoreFactory.getModelService();
  IndexService index=RodaCoreFactory.getIndexService();
  Date now=new Date();
  Map<String,Pair<Integer,Integer>> jobState=new HashMap<>();
  List<String> aipsToDelete=new ArrayList<>();
  String userAgentId;
  try {
    boolean notifyAgent=true;
    PreservationMetadata pm=PremisV3Utils.createPremisUserAgentBinary(user.getName(),model,index,notifyAgent);
    userAgentId=pm.getId();
  }
 catch (  AlreadyExistsException e) {
    userAgentId=IdUtils.getUserAgentId(user.getName());
  }
catch (  ValidationException e) {
    throw new GenericException(e);
  }
  for (  String aipId : listOfIds) {
    AIP aip=model.retrieveAIP(aipId);
    String jobId=aip.getIngestJobId();
    if (accept) {
      aip.setState(AIPState.ACTIVE);
      model.updateAIPState(aip,user.getName());
      String id=IdUtils.createPreservationMetadataId(PreservationMetadataType.EVENT);
      PreservationEventType type=PreservationEventType.ACCESSION;
      String preservationEventDescription=AutoAcceptSIPPlugin.DESCRIPTION;
      List<LinkingIdentifier> sources=new ArrayList<>();
      List<LinkingIdentifier> outcomes=Arrays.asList(PluginHelper.getLinkingIdentifier(aipId,RodaConstants.PRESERVATION_LINKING_OBJECT_OUTCOME));
      PluginState outcome=PluginState.SUCCESS;
      String outcomeDetailNote=AutoAcceptSIPPlugin.SUCCESS_MESSAGE;
      String outcomeDetailExtension=null;
      boolean notifyEvent=true;
      try {
        ContentPayload premisEvent=PremisV3Utils.createPremisEventBinary(id,now,type.toString(),preservationEventDescription,sources,outcomes,outcome.name(),outcomeDetailNote,outcomeDetailExtension,Arrays.asList(userAgentId));
        model.createPreservationMetadata(PreservationMetadataType.EVENT,id,aipId,null,null,null,premisEvent,notifyEvent);
      }
 catch (      AlreadyExistsException|ValidationException e) {
        throw new GenericException(e);
      }
    }
 else {
      model.deleteAIP(aipId);
      aipsToDelete.add(aipId);
    }
    Report report=model.retrieveJobReport(jobId,aipId);
    Report reportItem=new Report();
    reportItem.setTitle("String_Node_Str");
    reportItem.setPlugin(user.getName());
    reportItem.setPluginDetails(rejectReason);
    reportItem.setPluginState(accept ? PluginState.SUCCESS : PluginState.FAILURE);
    reportItem.setOutcomeObjectState(accept ? AIPState.ACTIVE : AIPState.DELETED);
    reportItem.setDateCreated(now);
    report.addReport(reportItem);
    model.createOrUpdateJobReport(report);
    Pair<Integer,Integer> pair=jobState.get(jobId);
    if (pair == null) {
      jobState.put(jobId,Pair.create(1,accept ? 1 : 0));
    }
 else {
      jobState.put(jobId,Pair.create(pair.getFirst() + 1,pair.getSecond() + (accept ? 1 : 0)));
    }
  }
  try {
    Job job=new Job();
    job.setName(RiskIncidenceRemoverPlugin.class.getSimpleName() + "String_Node_Str" + job.getStartDate());
    job.setPlugin(RiskIncidenceRemoverPlugin.class.getName());
    job.setSourceObjects(SelectedItemsList.create(AIP.class,aipsToDelete));
    Jobs.createJob(user,job);
  }
 catch (  JobAlreadyStartedException e) {
    LOGGER.error("String_Node_Str");
  }
  for (  Entry<String,Pair<Integer,Integer>> entry : jobState.entrySet()) {
    String jobId=entry.getKey();
    int total=entry.getValue().getFirst();
    int accepted=entry.getValue().getSecond();
    int rejected=total - accepted;
    Job job=model.retrieveJob(jobId);
    if (rejected > 0) {
      job.getJobStats().setSourceObjectsProcessedWithSuccess(job.getJobStats().getSourceObjectsProcessedWithSuccess() - rejected);
      job.getJobStats().setSourceObjectsProcessedWithFailure(job.getJobStats().getSourceObjectsProcessedWithFailure() + rejected);
    }
    job.getJobStats().setOutcomeObjectsWithManualIntervention(job.getJobStats().getOutcomeObjectsWithManualIntervention() - total);
    model.createOrUpdateJob(job);
  }
  RodaCoreFactory.getIndexService().commit(IndexedAIP.class,Job.class,Report.class,IndexedPreservationEvent.class);
}