Program: 51972
Project: 51972
Buggy source:
/** 
 * Process the editing of an existing or a newly created policy. 
 * @param context The current DSpace context.
 * @param objectType The type of the policy's parent object (ITEM, COLLECTION, COMMUNITY)
 * @param objectID The ID of the policy's parent object
 * @param policyID The ID of the policy being edited (-1 if a new policy is being created)
 * @param groupID The ID of the group to be associated with this policy
 * @param actionID The ID of the action (dependent on the objectType) to be associated with this policy
 * @return A process result's object.
 */
public static FlowResult processEditPolicy(Context context,int objectType,UUID objectID,int policyID,UUID groupID,int actionID,String name,String description,String startDateParam,String endDateParam) throws SQLException, AuthorizeException {
  FlowResult result=new FlowResult();
  boolean added=false;
  ResourcePolicy policy=resourcePolicyService.find(context,policyID);
  if (policy != null) {
    AuthorizeUtil.authorizeManagePolicy(context,policy);
  }
  if (actionID == -1) {
    result.setContinue(false);
    result.addError("String_Node_Str");
    return result;
  }
  if (groupID == null) {
    result.setContinue(false);
    result.addError("String_Node_Str");
    return result;
  }
  Date startDate=null;
  Date endDate=null;
  if (StringUtils.isNotBlank(startDateParam)) {
    try {
      startDate=DateUtils.parseDate(startDateParam,new String[]{"String_Node_Str","String_Node_Str","String_Node_Str"});
    }
 catch (    ParseException e) {
      startDate=null;
    }
    if (startDate == null) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
  if (StringUtils.isNotBlank(endDateParam)) {
    try {
      endDate=DateUtils.parseDate(endDateParam,new String[]{"String_Node_Str","String_Node_Str","String_Node_Str"});
    }
 catch (    ParseException e) {
      endDate=null;
    }
    if (endDate == null) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
  if (endDate != null && startDate != null) {
    if (startDate.after(endDate)) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
  DSpaceObject dso=ContentServiceFactory.getInstance().getDSpaceObjectService(objectType).find(context,objectID);
  if (policy == null) {
    if (authorizeService.isAnIdenticalPolicyAlreadyInPlace(context,dso,groupService.find(context,groupID),actionID,-1)) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
 else {
    if (authorizeService.isAnIdenticalPolicyAlreadyInPlace(context,dso,groupService.find(context,groupID),actionID,policy.getID())) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
  DSpaceObject policyParent=null;
  if (policy == null) {
switch (objectType) {
case Constants.COMMUNITY:
{
        policyParent=communityService.find(context,objectID);
        AuthorizeUtil.authorizeManageCommunityPolicy(context,(Community)policyParent);
        break;
      }
case Constants.COLLECTION:
{
      policyParent=collectionService.find(context,objectID);
      AuthorizeUtil.authorizeManageCollectionPolicy(context,(Collection)policyParent);
      break;
    }
case Constants.ITEM:
{
    policyParent=itemService.find(context,objectID);
    AuthorizeUtil.authorizeManageItemPolicy(context,(Item)policyParent);
    break;
  }
case Constants.BUNDLE:
{
  policyParent=bundleService.find(context,objectID);
  AuthorizeUtil.authorizeManageItemPolicy(context,(Item)(bundleService.getParentObject(context,(Bundle)policyParent)));
  break;
}
case Constants.BITSTREAM:
{
policyParent=bitstreamService.find(context,objectID);
AuthorizeUtil.authorizeManageItemPolicy(context,(Item)(bitstreamService.getParentObject(context,(Bitstream)policyParent)));
break;
}
}
policy=resourcePolicyService.create(context);
policy.setdSpaceObject(policyParent);
policy.setRpType(ResourcePolicy.TYPE_CUSTOM);
added=true;
}
Group group=groupService.find(context,groupID);
policy.setAction(actionID);
policy.setGroup(group);
policy.setRpName(name);
policy.setRpDescription(description);
if (endDate != null) policy.setEndDate(endDate);
 else policy.setEndDate(null);
if (startDate != null) policy.setStartDate(startDate);
 else policy.setStartDate(null);
Bitstream logo=null;
DSpaceObject logoContainer=null;
if (objectType == Constants.COLLECTION) {
logoContainer=collectionService.find(context,objectID);
logo=((Collection)logoContainer).getLogo();
}
 else if (objectType == Constants.COMMUNITY) {
logoContainer=communityService.find(context,objectID);
logo=((Community)logoContainer).getLogo();
}
if (logo != null) {
List policySet=authorizeService.getPolicies(context,logoContainer);
authorizeService.removeAllPolicies(context,logo);
authorizeService.addPolicies(context,policySet,logo);
}
resourcePolicyService.update(context,policy);
result.setContinue(true);
result.setOutcome(true);
if (added) {
result.setMessage(new Message("String_Node_Str","String_Node_Str"));
}
 else {
result.setMessage(new Message("String_Node_Str","String_Node_Str"));
}
result.setParameter("String_Node_Str",policy.getID());
return result;
}

Fixed version (ground truth):
/** 
 * Process the editing of an existing or a newly created policy. 
 * @param context The current DSpace context.
 * @param objectType The type of the policy's parent object (ITEM, COLLECTION, COMMUNITY)
 * @param objectID The ID of the policy's parent object
 * @param policyID The ID of the policy being edited (-1 if a new policy is being created)
 * @param groupID The ID of the group to be associated with this policy
 * @param actionID The ID of the action (dependent on the objectType) to be associated with this policy
 * @return A process result's object.
 */
public static FlowResult processEditPolicy(Context context,int objectType,UUID objectID,int policyID,UUID groupID,int actionID,String name,String description,String startDateParam,String endDateParam) throws SQLException, AuthorizeException {
  FlowResult result=new FlowResult();
  boolean added=false;
  ResourcePolicy policy=resourcePolicyService.find(context,policyID);
  if (policy != null) {
    AuthorizeUtil.authorizeManagePolicy(context,policy);
  }
  if (actionID == -1) {
    result.setContinue(false);
    result.addError("String_Node_Str");
    return result;
  }
  if (groupID == null) {
    result.setContinue(false);
    result.addError("String_Node_Str");
    return result;
  }
  Date startDate=null;
  Date endDate=null;
  if (StringUtils.isNotBlank(startDateParam)) {
    try {
      startDate=DateUtils.parseDate(startDateParam,new String[]{"String_Node_Str","String_Node_Str","String_Node_Str"});
    }
 catch (    ParseException e) {
      startDate=null;
    }
    if (startDate == null) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
  if (StringUtils.isNotBlank(endDateParam)) {
    try {
      endDate=DateUtils.parseDate(endDateParam,new String[]{"String_Node_Str","String_Node_Str","String_Node_Str"});
    }
 catch (    ParseException e) {
      endDate=null;
    }
    if (endDate == null) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
  if (endDate != null && startDate != null) {
    if (startDate.after(endDate)) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
  DSpaceObject dso=ContentServiceFactory.getInstance().getDSpaceObjectService(objectType).find(context,objectID);
  if (policy == null) {
    if (authorizeService.isAnIdenticalPolicyAlreadyInPlace(context,dso,groupService.find(context,groupID),actionID,-1)) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
 else {
    if (authorizeService.isAnIdenticalPolicyAlreadyInPlace(context,dso,groupService.find(context,groupID),actionID,policy.getID())) {
      result.setContinue(false);
      result.addError("String_Node_Str");
      return result;
    }
  }
  DSpaceObject policyParent=null;
  if (policy == null) {
switch (objectType) {
case Constants.COMMUNITY:
{
        policyParent=communityService.find(context,objectID);
        AuthorizeUtil.authorizeManageCommunityPolicy(context,(Community)policyParent);
        break;
      }
case Constants.COLLECTION:
{
      policyParent=collectionService.find(context,objectID);
      AuthorizeUtil.authorizeManageCollectionPolicy(context,(Collection)policyParent);
      break;
    }
case Constants.ITEM:
{
    policyParent=itemService.find(context,objectID);
    AuthorizeUtil.authorizeManageItemPolicy(context,(Item)policyParent);
    break;
  }
case Constants.BUNDLE:
{
  policyParent=bundleService.find(context,objectID);
  AuthorizeUtil.authorizeManageItemPolicy(context,(Item)(bundleService.getParentObject(context,(Bundle)policyParent)));
  break;
}
case Constants.BITSTREAM:
{
policyParent=bitstreamService.find(context,objectID);
AuthorizeUtil.authorizeManageItemPolicy(context,(Item)(bitstreamService.getParentObject(context,(Bitstream)policyParent)));
break;
}
default :
throw new IllegalArgumentException("String_Node_Str");
}
policy=resourcePolicyService.create(context);
policy.setdSpaceObject(policyParent);
policy.setRpType(ResourcePolicy.TYPE_CUSTOM);
added=true;
}
Group group=groupService.find(context,groupID);
policy.setAction(actionID);
policy.setGroup(group);
policy.setRpName(name);
policy.setRpDescription(description);
if (endDate != null) policy.setEndDate(endDate);
 else policy.setEndDate(null);
if (startDate != null) policy.setStartDate(startDate);
 else policy.setStartDate(null);
Bitstream logo=null;
DSpaceObject logoContainer=null;
if (objectType == Constants.COLLECTION) {
logoContainer=collectionService.find(context,objectID);
logo=((Collection)logoContainer).getLogo();
}
 else if (objectType == Constants.COMMUNITY) {
logoContainer=communityService.find(context,objectID);
logo=((Community)logoContainer).getLogo();
}
if (logo != null) {
List policySet=authorizeService.getPolicies(context,logoContainer);
authorizeService.removeAllPolicies(context,logo);
authorizeService.addPolicies(context,policySet,logo);
}
resourcePolicyService.update(context,policy);
result.setContinue(true);
result.setOutcome(true);
if (added) {
result.setMessage(new Message("String_Node_Str","String_Node_Str"));
}
 else {
result.setMessage(new Message("String_Node_Str","String_Node_Str"));
}
result.setParameter("String_Node_Str",policy.getID());
return result;
}