Program: 107377
Project: 107377
Buggy source:
@DB protected boolean updateTemplateOrIsoPermissions(UpdateTemplateOrIsoPermissionsCmd cmd){
  Transaction txn=Transaction.currentTxn();
  Long id=cmd.getId();
  Account account=UserContext.current().getAccount();
  List<String> accountNames=cmd.getAccountNames();
  Long userId=UserContext.current().getUserId();
  Boolean isFeatured=cmd.isFeatured();
  Boolean isPublic=cmd.isPublic();
  String operation=cmd.getOperation();
  String mediaType="String_Node_Str";
  VMTemplateVO template=_templateDao.findById(id);
  if (template == null || !templateIsCorrectType(template)) {
    throw new ServerApiException(BaseCmd.INTERNAL_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id);
  }
  if (cmd instanceof UpdateTemplatePermissionsCmd) {
    mediaType="String_Node_Str";
    if (template.getFormat().equals(ImageFormat.ISO)) {
      throw new ServerApiException(BaseCmd.INTERNAL_ERROR,"String_Node_Str");
    }
  }
  if (cmd instanceof UpdateIsoPermissionsCmd) {
    mediaType="String_Node_Str";
    if (!template.getFormat().equals(ImageFormat.ISO)) {
      throw new ServerApiException(BaseCmd.INTERNAL_ERROR,"String_Node_Str");
    }
  }
  if (account != null) {
    if (!isAdmin(account.getType()) && (template.getAccountId() != account.getId())) {
      throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id);
    }
 else     if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      Long templateOwnerDomainId=findDomainIdByAccountId(template.getAccountId());
      if (!isChildDomain(account.getDomainId(),templateOwnerDomainId)) {
        throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id);
      }
    }
  }
  if (userId == null) {
    userId=Long.valueOf(User.UID_SYSTEM);
  }
  if (template.getRemoved() != null) {
    s_logger.error("String_Node_Str" + mediaType + "String_Node_Str"+ id+ "String_Node_Str");
    throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id+ "String_Node_Str");
  }
  if (id == Long.valueOf(1)) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id);
  }
  boolean isAdmin=((account == null) || isAdmin(account.getType()));
  boolean allowPublicUserTemplates=Boolean.parseBoolean(getConfigurationValue("String_Node_Str"));
  if (!isAdmin && !allowPublicUserTemplates && isPublic != null && isPublic) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + mediaType + "String_Node_Str");
  }
  if (accountNames != null) {
    if ((operation == null) || (!operation.equalsIgnoreCase("String_Node_Str") && !operation.equalsIgnoreCase("String_Node_Str") && !operation.equalsIgnoreCase("String_Node_Str"))) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + "String_Node_Str" + operation + "String_Node_Str");
    }
  }
  Long accountId=template.getAccountId();
  if (accountId == null) {
    throw new InvalidParameterValueException("String_Node_Str" + template.getName());
  }
  VMTemplateVO updatedTemplate=_templateDao.createForUpdate();
  if (isPublic != null) {
    updatedTemplate.setPublicTemplate(isPublic.booleanValue());
  }
  if (isFeatured != null) {
    updatedTemplate.setFeatured(isFeatured.booleanValue());
  }
  _templateDao.update(template.getId(),updatedTemplate);
  Long domainId=account.getDomainId();
  if ("String_Node_Str".equalsIgnoreCase(operation)) {
    txn.start();
    for (    String accountName : accountNames) {
      Account permittedAccount=_accountDao.findActiveAccount(accountName,domainId);
      if (permittedAccount != null) {
        if (permittedAccount.getId() == account.getId()) {
          continue;
        }
        LaunchPermissionVO existingPermission=_launchPermissionDao.findByTemplateAndAccount(id,permittedAccount.getId());
        if (existingPermission == null) {
          LaunchPermissionVO launchPermission=new LaunchPermissionVO(id,permittedAccount.getId());
          _launchPermissionDao.persist(launchPermission);
        }
      }
 else {
        txn.rollback();
        throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ "String_Node_Str");
      }
    }
    txn.commit();
  }
 else   if ("String_Node_Str".equalsIgnoreCase(operation)) {
    List<Long> accountIds=new ArrayList<Long>();
    for (    String accountName : accountNames) {
      Account permittedAccount=_accountDao.findActiveAccount(accountName,domainId);
      if (permittedAccount != null) {
        accountIds.add(permittedAccount.getId());
      }
    }
    _launchPermissionDao.removePermissions(id,accountIds);
  }
 else   if ("String_Node_Str".equalsIgnoreCase(operation)) {
    updatedTemplate=_templateDao.createForUpdate();
    updatedTemplate.setPublicTemplate(false);
    updatedTemplate.setFeatured(false);
    _templateDao.update(template.getId(),updatedTemplate);
    _launchPermissionDao.removeAllPermissions(id);
  }
  return true;
}

Fixed version (ground truth):
@DB protected boolean updateTemplateOrIsoPermissions(UpdateTemplateOrIsoPermissionsCmd cmd){
  Transaction txn=Transaction.currentTxn();
  Long id=cmd.getId();
  Account account=UserContext.current().getAccount();
  List<String> accountNames=cmd.getAccountNames();
  Long userId=UserContext.current().getUserId();
  Boolean isFeatured=cmd.isFeatured();
  Boolean isPublic=cmd.isPublic();
  String operation=cmd.getOperation();
  String mediaType="String_Node_Str";
  VMTemplateVO template=_templateDao.findById(id);
  if (template == null || !templateIsCorrectType(template)) {
    throw new ServerApiException(BaseCmd.INTERNAL_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id);
  }
  if (cmd instanceof UpdateTemplatePermissionsCmd) {
    mediaType="String_Node_Str";
    if (template.getFormat().equals(ImageFormat.ISO)) {
      throw new ServerApiException(BaseCmd.INTERNAL_ERROR,"String_Node_Str");
    }
  }
  if (cmd instanceof UpdateIsoPermissionsCmd) {
    mediaType="String_Node_Str";
    if (!template.getFormat().equals(ImageFormat.ISO)) {
      throw new ServerApiException(BaseCmd.INTERNAL_ERROR,"String_Node_Str");
    }
  }
  if (account != null) {
    if (!isAdmin(account.getType()) && (template.getAccountId() != account.getId())) {
      throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id);
    }
 else     if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
      Long templateOwnerDomainId=findDomainIdByAccountId(template.getAccountId());
      if (!isChildDomain(account.getDomainId(),templateOwnerDomainId)) {
        throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id);
      }
    }
  }
  if (userId == null) {
    userId=Long.valueOf(User.UID_SYSTEM);
  }
  if (template.getRemoved() != null) {
    s_logger.error("String_Node_Str" + mediaType + "String_Node_Str"+ id+ "String_Node_Str");
    throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id+ "String_Node_Str");
  }
  if (id == Long.valueOf(1)) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + mediaType + "String_Node_Str"+ id);
  }
  boolean isAdmin=((account == null) || isAdmin(account.getType()));
  boolean allowPublicUserTemplates=Boolean.parseBoolean(getConfigurationValue("String_Node_Str"));
  if (!isAdmin && !allowPublicUserTemplates && isPublic != null && isPublic) {
    throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + mediaType + "String_Node_Str");
  }
  if (accountNames != null) {
    if ((operation == null) || (!operation.equalsIgnoreCase("String_Node_Str") && !operation.equalsIgnoreCase("String_Node_Str") && !operation.equalsIgnoreCase("String_Node_Str"))) {
      throw new ServerApiException(BaseCmd.PARAM_ERROR,"String_Node_Str" + "String_Node_Str" + operation + "String_Node_Str");
    }
  }
  Long accountId=template.getAccountId();
  if (accountId == null) {
    throw new InvalidParameterValueException("String_Node_Str" + template.getName());
  }
  VMTemplateVO updatedTemplate=_templateDao.createForUpdate();
  if (isPublic != null) {
    updatedTemplate.setPublicTemplate(isPublic.booleanValue());
  }
  if (isFeatured != null) {
    updatedTemplate.setFeatured(isFeatured.booleanValue());
  }
  _templateDao.update(template.getId(),updatedTemplate);
  Long domainId;
  domainId=(null == account) ? DomainVO.ROOT_DOMAIN : account.getDomainId();
  if ("String_Node_Str".equalsIgnoreCase(operation)) {
    txn.start();
    for (    String accountName : accountNames) {
      Account permittedAccount=_accountDao.findActiveAccount(accountName,domainId);
      if (permittedAccount != null) {
        if (permittedAccount.getId() == account.getId()) {
          continue;
        }
        LaunchPermissionVO existingPermission=_launchPermissionDao.findByTemplateAndAccount(id,permittedAccount.getId());
        if (existingPermission == null) {
          LaunchPermissionVO launchPermission=new LaunchPermissionVO(id,permittedAccount.getId());
          _launchPermissionDao.persist(launchPermission);
        }
      }
 else {
        txn.rollback();
        throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ "String_Node_Str");
      }
    }
    txn.commit();
  }
 else   if ("String_Node_Str".equalsIgnoreCase(operation)) {
    List<Long> accountIds=new ArrayList<Long>();
    for (    String accountName : accountNames) {
      Account permittedAccount=_accountDao.findActiveAccount(accountName,domainId);
      if (permittedAccount != null) {
        accountIds.add(permittedAccount.getId());
      }
    }
    _launchPermissionDao.removePermissions(id,accountIds);
  }
 else   if ("String_Node_Str".equalsIgnoreCase(operation)) {
    updatedTemplate=_templateDao.createForUpdate();
    updatedTemplate.setPublicTemplate(false);
    updatedTemplate.setFeatured(false);
    _templateDao.update(template.getId(),updatedTemplate);
    _launchPermissionDao.removeAllPermissions(id);
  }
  return true;
}