Program: 105508
Project: 105508
Buggy source:
@Override @ActionEvent(eventType=EventTypes.EVENT_TEMPLATE_CREATE,eventDescription="String_Node_Str",create=true) public VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd,Account templateOwner) throws ResourceAllocationException {
  Long userId=CallContext.current().getCallingUserId();
  Account caller=CallContext.current().getCallingAccount();
  boolean isAdmin=(_accountMgr.isAdmin(caller.getId()));
  _accountMgr.checkAccess(caller,null,true,templateOwner);
  String name=cmd.getTemplateName();
  if ((name == null) || (name.length() > 32)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (cmd.getTemplateTag() != null) {
    if (!_accountService.isRootAdmin(caller.getId())) {
      throw new PermissionDeniedException("String_Node_Str");
    }
  }
  Integer bits=cmd.getBits();
  Boolean requiresHvm=cmd.getRequiresHvm();
  Boolean passwordEnabled=cmd.isPasswordEnabled();
  Boolean isPublic=cmd.isPublic();
  Boolean featured=cmd.isFeatured();
  int bitsValue=((bits == null) ? 64 : bits.intValue());
  boolean requiresHvmValue=((requiresHvm == null) ? true : requiresHvm.booleanValue());
  boolean passwordEnabledValue=((passwordEnabled == null) ? false : passwordEnabled.booleanValue());
  if (isPublic == null) {
    isPublic=Boolean.FALSE;
  }
  boolean isDynamicScalingEnabled=cmd.isDynamicallyScalable();
  boolean allowPublicUserTemplates=AllowPublicUserTemplates.valueIn(templateOwner.getId());
  if (!isAdmin && !allowPublicUserTemplates && isPublic) {
    throw new PermissionDeniedException("String_Node_Str" + name + "String_Node_Str");
  }
  Long volumeId=cmd.getVolumeId();
  Long snapshotId=cmd.getSnapshotId();
  if ((volumeId == null) && (snapshotId == null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if ((volumeId != null) && (snapshotId != null)) {
    throw new InvalidParameterValueException("String_Node_Str" + volumeId + "String_Node_Str"+ snapshotId+ "String_Node_Str");
  }
  HypervisorType hyperType;
  VolumeVO volume=null;
  SnapshotVO snapshot=null;
  VMTemplateVO privateTemplate=null;
  if (volumeId != null) {
    volume=_volumeDao.findById(volumeId);
    if (volume == null) {
      throw new InvalidParameterValueException("String_Node_Str" + volumeId);
    }
    _accountMgr.checkAccess(caller,null,true,volume);
    if (!_volumeMgr.volumeInactive(volume)) {
      String msg="String_Node_Str" + volume.getName() + "String_Node_Str";
      if (s_logger.isInfoEnabled()) {
        s_logger.info(msg);
      }
      throw new CloudRuntimeException(msg);
    }
    hyperType=_volumeDao.getHypervisorType(volumeId);
  }
 else {
    snapshot=_snapshotDao.findById(snapshotId);
    if (snapshot == null) {
      throw new InvalidParameterValueException("String_Node_Str" + snapshotId);
    }
    volume=_volumeDao.findById(snapshot.getVolumeId());
    _accountMgr.checkAccess(caller,null,true,snapshot);
    if (snapshot.getState() != Snapshot.State.BackedUp) {
      throw new InvalidParameterValueException("String_Node_Str" + snapshotId + "String_Node_Str"+ Snapshot.State.BackedUp+ "String_Node_Str");
    }
    hyperType=snapshot.getHypervisorType();
  }
  _resourceLimitMgr.checkResourceLimit(templateOwner,ResourceType.template);
  _resourceLimitMgr.checkResourceLimit(templateOwner,ResourceType.secondary_storage,new Long(volume != null ? volume.getSize() : snapshot.getSize()).longValue());
  if (!isAdmin || featured == null) {
    featured=Boolean.FALSE;
  }
  Long guestOSId=cmd.getOsTypeId();
  GuestOSVO guestOS=_guestOSDao.findById(guestOSId);
  if (guestOS == null) {
    throw new InvalidParameterValueException("String_Node_Str" + guestOSId + "String_Node_Str");
  }
  Long nextTemplateId=_tmpltDao.getNextInSequence(Long.class,"String_Node_Str");
  String description=cmd.getDisplayText();
  boolean isExtractable=false;
  Long sourceTemplateId=null;
  if (volume != null) {
    VMTemplateVO template=ApiDBUtils.findTemplateById(volume.getTemplateId());
    isExtractable=template != null && template.isExtractable() && template.getTemplateType() != Storage.TemplateType.SYSTEM;
    if (template != null) {
      sourceTemplateId=template.getId();
    }
 else     if (volume.getVolumeType() == Volume.Type.ROOT) {
      UserVm userVm=ApiDBUtils.findUserVmById(volume.getInstanceId());
      sourceTemplateId=userVm.getIsoId();
    }
  }
  String templateTag=cmd.getTemplateTag();
  if (templateTag != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + templateTag);
    }
  }
  privateTemplate=new VMTemplateVO(nextTemplateId,name,ImageFormat.RAW,isPublic,featured,isExtractable,TemplateType.USER,null,requiresHvmValue,bitsValue,templateOwner.getId(),null,description,passwordEnabledValue,guestOS.getId(),true,hyperType,templateTag,cmd.getDetails(),false,isDynamicScalingEnabled);
  if (sourceTemplateId != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + sourceTemplateId);
    }
  }
  privateTemplate.setSourceTemplateId(sourceTemplateId);
  VMTemplateVO template=_tmpltDao.persist(privateTemplate);
  if (template != null) {
    Map<String,String> details=new HashMap<String,String>();
    if (volume != null) {
      Long vmId=volume.getInstanceId();
      if (vmId != null) {
        UserVmVO userVm=_userVmDao.findById(vmId);
        if (userVm != null) {
          _userVmDao.loadDetails(userVm);
          details.putAll(userVm.getDetails());
        }
      }
    }
    if (cmd.getDetails() != null) {
      details.remove("String_Node_Str");
      details.putAll(cmd.getDetails());
    }
    if (!details.isEmpty()) {
      privateTemplate.setDetails(details);
      _tmpltDao.saveDetails(privateTemplate);
    }
    _resourceLimitMgr.incrementResourceCount(templateOwner.getId(),ResourceType.template);
    _resourceLimitMgr.incrementResourceCount(templateOwner.getId(),ResourceType.secondary_storage,new Long(volume != null ? volume.getSize() : snapshot.getSize()));
  }
  if (template != null) {
    return template;
  }
 else {
    throw new CloudRuntimeException("String_Node_Str");
  }
}

Fixed version (ground truth):
@Override @ActionEvent(eventType=EventTypes.EVENT_TEMPLATE_CREATE,eventDescription="String_Node_Str",create=true) public VMTemplateVO createPrivateTemplateRecord(CreateTemplateCmd cmd,Account templateOwner) throws ResourceAllocationException {
  Account caller=CallContext.current().getCallingAccount();
  boolean isAdmin=(_accountMgr.isAdmin(caller.getId()));
  _accountMgr.checkAccess(caller,null,true,templateOwner);
  String name=cmd.getTemplateName();
  if ((name == null) || (name.length() > 32)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (cmd.getTemplateTag() != null) {
    if (!_accountService.isRootAdmin(caller.getId())) {
      throw new PermissionDeniedException("String_Node_Str");
    }
  }
  Integer bits=cmd.getBits();
  Boolean requiresHvm=cmd.getRequiresHvm();
  Boolean passwordEnabled=cmd.isPasswordEnabled();
  Boolean isPublic=cmd.isPublic();
  Boolean featured=cmd.isFeatured();
  int bitsValue=((bits == null) ? 64 : bits.intValue());
  boolean requiresHvmValue=((requiresHvm == null) ? true : requiresHvm.booleanValue());
  boolean passwordEnabledValue=((passwordEnabled == null) ? false : passwordEnabled.booleanValue());
  if (isPublic == null) {
    isPublic=Boolean.FALSE;
  }
  boolean isDynamicScalingEnabled=cmd.isDynamicallyScalable();
  boolean allowPublicUserTemplates=AllowPublicUserTemplates.valueIn(templateOwner.getId());
  if (!isAdmin && !allowPublicUserTemplates && isPublic) {
    throw new PermissionDeniedException("String_Node_Str" + name + "String_Node_Str");
  }
  Long volumeId=cmd.getVolumeId();
  Long snapshotId=cmd.getSnapshotId();
  if ((volumeId == null) && (snapshotId == null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if ((volumeId != null) && (snapshotId != null)) {
    throw new InvalidParameterValueException("String_Node_Str" + volumeId + "String_Node_Str"+ snapshotId+ "String_Node_Str");
  }
  HypervisorType hyperType;
  VolumeVO volume=null;
  SnapshotVO snapshot=null;
  VMTemplateVO privateTemplate=null;
  if (volumeId != null) {
    volume=_volumeDao.findById(volumeId);
    if (volume == null) {
      throw new InvalidParameterValueException("String_Node_Str" + volumeId);
    }
    _accountMgr.checkAccess(caller,null,true,volume);
    if (!_volumeMgr.volumeInactive(volume)) {
      String msg="String_Node_Str" + volume.getName() + "String_Node_Str";
      if (s_logger.isInfoEnabled()) {
        s_logger.info(msg);
      }
      throw new CloudRuntimeException(msg);
    }
    hyperType=_volumeDao.getHypervisorType(volumeId);
  }
 else {
    snapshot=_snapshotDao.findById(snapshotId);
    if (snapshot == null) {
      throw new InvalidParameterValueException("String_Node_Str" + snapshotId);
    }
    volume=_volumeDao.findById(snapshot.getVolumeId());
    _accountMgr.checkAccess(caller,null,true,snapshot);
    if (snapshot.getState() != Snapshot.State.BackedUp) {
      throw new InvalidParameterValueException("String_Node_Str" + snapshotId + "String_Node_Str"+ Snapshot.State.BackedUp+ "String_Node_Str");
    }
    hyperType=snapshot.getHypervisorType();
  }
  _resourceLimitMgr.checkResourceLimit(templateOwner,ResourceType.template);
  _resourceLimitMgr.checkResourceLimit(templateOwner,ResourceType.secondary_storage,new Long(volume != null ? volume.getSize() : snapshot.getSize()).longValue());
  if (!isAdmin || featured == null) {
    featured=Boolean.FALSE;
  }
  Long guestOSId=cmd.getOsTypeId();
  GuestOSVO guestOS=_guestOSDao.findById(guestOSId);
  if (guestOS == null) {
    throw new InvalidParameterValueException("String_Node_Str" + guestOSId + "String_Node_Str");
  }
  Long nextTemplateId=_tmpltDao.getNextInSequence(Long.class,"String_Node_Str");
  String description=cmd.getDisplayText();
  boolean isExtractable=false;
  Long sourceTemplateId=null;
  if (volume != null) {
    VMTemplateVO template=ApiDBUtils.findTemplateById(volume.getTemplateId());
    isExtractable=template != null && template.isExtractable() && template.getTemplateType() != Storage.TemplateType.SYSTEM;
    if (template != null) {
      sourceTemplateId=template.getId();
    }
 else     if (volume.getVolumeType() == Volume.Type.ROOT) {
      UserVm userVm=ApiDBUtils.findUserVmById(volume.getInstanceId());
      sourceTemplateId=userVm.getIsoId();
    }
  }
  String templateTag=cmd.getTemplateTag();
  if (templateTag != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + templateTag);
    }
  }
  privateTemplate=new VMTemplateVO(nextTemplateId,name,ImageFormat.RAW,isPublic,featured,isExtractable,TemplateType.USER,null,requiresHvmValue,bitsValue,templateOwner.getId(),null,description,passwordEnabledValue,guestOS.getId(),true,hyperType,templateTag,cmd.getDetails(),false,isDynamicScalingEnabled);
  if (sourceTemplateId != null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + sourceTemplateId);
    }
  }
  privateTemplate.setSourceTemplateId(sourceTemplateId);
  VMTemplateVO template=_tmpltDao.persist(privateTemplate);
  if (template != null) {
    Map<String,String> details=new HashMap<String,String>();
    if (volume != null) {
      Long vmId=volume.getInstanceId();
      if (vmId != null) {
        UserVmVO userVm=_userVmDao.findById(vmId);
        if (userVm != null) {
          _userVmDao.loadDetails(userVm);
          details.putAll(userVm.getDetails());
        }
      }
    }
    if (cmd.getDetails() != null) {
      details.remove("String_Node_Str");
      details.putAll(cmd.getDetails());
    }
    if (!details.isEmpty()) {
      privateTemplate.setDetails(details);
      _tmpltDao.saveDetails(privateTemplate);
    }
    _resourceLimitMgr.incrementResourceCount(templateOwner.getId(),ResourceType.template);
    _resourceLimitMgr.incrementResourceCount(templateOwner.getId(),ResourceType.secondary_storage,new Long(volume != null ? volume.getSize() : snapshot.getSize()));
  }
  if (template != null) {
    return template;
  }
 else {
    throw new CloudRuntimeException("String_Node_Str");
  }
}