Program: 188337
Project: 188337
Buggy source:
public static void appraisal(User user,SelectedItems<IndexedAIP> selected,boolean accept,String rejectReason,Locale locale) 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);
    }
    Job job=model.retrieveJob(jobId);
    Report report=model.retrieveJobReport(jobId,aipId,true);
    Report reportItem=new Report();
    Messages messages=RodaCoreFactory.getI18NMessages(locale);
    reportItem.setTitle(messages.getTranslation(RodaConstants.I18N_UI_APPRAISAL));
    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,job);
    Pair<Integer,Integer> pair=jobState.get(jobId);
    if (pair == null) {
      jobState.put(jobId,Pair.of(1,accept ? 1 : 0));
    }
 else {
      jobState.put(jobId,Pair.of(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,false);
  }
 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,IndexedReport.class,IndexedPreservationEvent.class);
}

Fixed version (ground truth):
public static void appraisal(User user,SelectedItems<IndexedAIP> selected,boolean accept,String rejectReason,Locale locale) 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);
    }
    Job job=model.retrieveJob(jobId);
    Report report=model.retrieveJobReport(jobId,aipId,true);
    Report reportItem=new Report();
    Messages messages=RodaCoreFactory.getI18NMessages(locale);
    reportItem.setTitle(messages.getTranslation(RodaConstants.I18N_UI_APPRAISAL));
    reportItem.setPlugin(messages.getTranslation(RodaConstants.I18N_UI_APPRAISAL));
    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,job);
    Pair<Integer,Integer> pair=jobState.get(jobId);
    if (pair == null) {
      jobState.put(jobId,Pair.of(1,accept ? 1 : 0));
    }
 else {
      jobState.put(jobId,Pair.of(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,false);
  }
 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,IndexedReport.class,IndexedPreservationEvent.class);
}