Program: 64591
Project: 64591
Buggy source:
/** 
 * Builds query operations for a given ProcedureOperationModel.
 */
public void buildProcedureOperation(ProcedureOperationModel procedureOperationModel){
  for (  ProcedureType storedProcedure : procedureOperationModel.getDbStoredProcedures()) {
    boolean hasComplexArgs=Util.hasComplexArgs(storedProcedure);
    QueryOperation qo=new QueryOperation();
    qo.setName(getNameForQueryOperation(procedureOperationModel,storedProcedure));
    String qualifiedProcName=getQualifiedProcedureName(procedureOperationModel,storedProcedure);
    dbwsBuilder.logMessage(FINEST,BUILDING_QUERYOP_FOR + qualifiedProcName);
    QueryHandler qh=null;
    List<DatabaseQuery> queries=dbwsBuilder.getOrProject().getQueries();
    if (queries.size() > 0) {
      for (      DatabaseQuery q : queries) {
        if (q.getName().equals(qo.getName())) {
          qh=new NamedQueryHandler();
          ((NamedQueryHandler)qh).setName(qo.getName());
        }
      }
    }
    if (qh == null) {
      if (storedProcedure.isFunctionType()) {
        qh=new StoredFunctionQueryHandler();
      }
 else {
        qh=new StoredProcedureQueryHandler();
      }
      ((StoredProcedureQueryHandler)qh).setName(qualifiedProcName);
    }
    qo.setQueryHandler(qh);
    String returnType=procedureOperationModel.getReturnType();
    boolean isCollection=procedureOperationModel.isCollection();
    boolean isSimpleXMLFormat=procedureOperationModel.isSimpleXMLFormat();
    Result result=null;
    if (storedProcedure.isFunctionType()) {
      ArgumentType returnArg=((FunctionType)storedProcedure).getReturnArgument();
      result=buildResultForStoredFunction(returnArg,returnType);
      if (returnArg.getEnclosedType().isPLSQLCursorType()) {
        customizeSimpleXMLTagNames((PLSQLCursorType)returnArg.getEnclosedType(),procedureOperationModel);
      }
    }
 else     if (hasComplexArgs) {
      if (Util.noOutArguments(storedProcedure)) {
        result=new Result();
        result.setType(new QName(SCHEMA_URL,INT,SCHEMA_PREFIX));
      }
    }
 else {
      if (returnType != null) {
        result=new Result();
        result.setType(buildCustomQName(returnType,dbwsBuilder));
      }
 else {
        if (isCollection) {
          result=new CollectionResult();
          if (isSimpleXMLFormat) {
            result.setType(SXF_QNAME_CURSOR);
          }
        }
 else {
          result=new Result();
          result.setType(SXF_QNAME);
        }
      }
    }
    for (    ArgumentType arg : storedProcedure.getArguments()) {
      String argName=arg.getArgumentName();
      if (argName != null) {
        QName xmlType=null;
        ProcedureArgument pa=null;
        ProcedureArgument paShadow=null;
        Parameter parm=null;
        ArgumentTypeDirection direction=arg.getDirection();
        if (!hasComplexArgs) {
          if (arg.getEnclosedType().isPLSQLCursorType()) {
            PLSQLCursorType cursorType=(PLSQLCursorType)arg.getEnclosedType();
            if (cursorType.isWeaklyTyped()) {
              xmlType=buildCustomQName("String_Node_Str",dbwsBuilder);
            }
          }
 else {
            xmlType=getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
          }
        }
 else {
          if (arg.getEnclosedType().isPLSQLType()) {
            hasComplexProcedureArgs=true;
            String packageName=((PLSQLType)arg.getEnclosedType()).getParentType().getPackageName();
            String typeString=(packageName != null && packageName.length() > 0) ? packageName + UNDERSCORE + arg.getTypeName() : arg.getTypeName();
            xmlType=buildCustomQName(typeString,dbwsBuilder);
          }
 else           if (arg.getEnclosedType().isVArrayType() || arg.getEnclosedType().isObjectType() || arg.getEnclosedType().isObjectTableType()) {
            hasComplexProcedureArgs=true;
            String typeString=arg.getTypeName().toLowerCase().concat(TYPE_STR);
            xmlType=buildCustomQName(typeString,dbwsBuilder);
          }
 else {
switch (Util.getJDBCTypeFromTypeName(arg.getTypeName())) {
case STRUCT:
case ARRAY:
              String typeString=nct.generateSchemaAlias(arg.getTypeName());
            xmlType=buildCustomQName(typeString,dbwsBuilder);
          break;
default :
        xmlType=getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
      break;
  }
}
}
if (direction == null || direction == IN) {
parm=new Parameter();
parm.setName(argName);
parm.setType(xmlType);
parm.setOptional(arg.optional());
pa=new ProcedureArgument();
pa.setName(argName);
pa.setParameterName(argName);
if (qh instanceof StoredProcedureQueryHandler) {
  ((StoredProcedureQueryHandler)qh).getInArguments().add(pa);
}
}
 else {
pa=new ProcedureOutputArgument();
ProcedureOutputArgument pao=(ProcedureOutputArgument)pa;
pao.setName(argName);
pao.setParameterName(argName);
boolean isCursor=arg.isPLSQLCursorType() || arg.getTypeName().contains(CURSOR_STR);
if (arg.isPLSQLCursorType()) {
  customizeSimpleXMLTagNames((PLSQLCursorType)arg.getEnclosedType(),procedureOperationModel);
}
if (isCursor && returnType == null) {
  pao.setResultType(SXF_QNAME_CURSOR);
  if (result == null) {
    result=new CollectionResult();
    result.setType(SXF_QNAME_CURSOR);
  }
}
 else {
  if (returnType != null && !isSimpleXMLFormat) {
    xmlType=qNameFromString(OPEN_PAREN + dbwsBuilder.getTargetNamespace() + CLOSE_PAREN+ returnType,dbwsBuilder.getSchema());
  }
  if (isCursor) {
    pao.setResultType(new QName(EMPTY_STRING,CURSOR_OF_STR + returnType));
    Result newResult=new CollectionResult();
    newResult.setType(result.getType());
    result=newResult;
  }
 else {
    pao.setResultType(xmlType);
  }
  if (result == null) {
    if (isCollection) {
      result=new CollectionResult();
    }
 else {
      result=new Result();
    }
    result.setType(xmlType);
  }
}
if (direction == INOUT) {
  parm=new Parameter();
  parm.setName(argName);
  parm.setType(xmlType);
  result.setType(xmlType);
  if (qh instanceof StoredProcedureQueryHandler) {
    ((StoredProcedureQueryHandler)qh).getInOutArguments().add(pao);
  }
  paShadow=new ProcedureArgument();
  paShadow.setName(argName);
  paShadow.setParameterName(argName);
}
 else {
  if (qh instanceof StoredProcedureQueryHandler) {
    ((StoredProcedureQueryHandler)qh).getOutArguments().add(pao);
  }
}
}
if (hasComplexArgs && arg.getEnclosedType().isPLSQLType()) {
pa.setComplexTypeName(storedProcedure.getCatalogName() + UNDERSCORE + arg.getTypeName());
if (paShadow != null) {
  paShadow.setComplexTypeName(pa.getComplexTypeName());
}
}
if (parm != null) {
qo.getParameters().add(parm);
}
}
}
if (procedureOperationModel.getBinaryAttachment()) {
Attachment attachment=new Attachment();
attachment.setMimeType(APP_OCTET_STREAM);
result.setAttachment(attachment);
}
handleSimpleXMLFormat(isSimpleXMLFormat,result,procedureOperationModel);
qo.setResult(result);
dbwsBuilder.getXrServiceModel().getOperations().put(qo.getName(),qo);
}
finishProcedureOperation();
}

Fixed version (ground truth):
/** 
 * Builds query operations for a given ProcedureOperationModel.
 */
public void buildProcedureOperation(ProcedureOperationModel procedureOperationModel){
  for (  ProcedureType storedProcedure : procedureOperationModel.getDbStoredProcedures()) {
    boolean hasComplexArgs=Util.hasComplexArgs(storedProcedure);
    QueryOperation qo=new QueryOperation();
    qo.setName(getNameForQueryOperation(procedureOperationModel,storedProcedure));
    String qualifiedProcName=getQualifiedProcedureName(procedureOperationModel,storedProcedure);
    dbwsBuilder.logMessage(FINEST,BUILDING_QUERYOP_FOR + qualifiedProcName);
    QueryHandler qh=null;
    List<DatabaseQuery> queries=dbwsBuilder.getOrProject().getQueries();
    if (queries.size() > 0) {
      for (      DatabaseQuery q : queries) {
        if (q.getName().equals(qo.getName())) {
          qh=new NamedQueryHandler();
          ((NamedQueryHandler)qh).setName(qo.getName());
        }
      }
    }
    if (qh == null) {
      if (storedProcedure.isFunctionType()) {
        qh=new StoredFunctionQueryHandler();
      }
 else {
        qh=new StoredProcedureQueryHandler();
      }
      ((StoredProcedureQueryHandler)qh).setName(qualifiedProcName);
    }
    qo.setQueryHandler(qh);
    String returnType=procedureOperationModel.getReturnType();
    boolean isCollection=procedureOperationModel.isCollection();
    boolean isSimpleXMLFormat=procedureOperationModel.isSimpleXMLFormat();
    Result result=null;
    int outArgCount=0;
    for (    ArgumentType argument : storedProcedure.getArguments()) {
      ArgumentTypeDirection argDirection=argument.getDirection();
      if (argDirection == OUT) {
        outArgCount++;
      }
    }
    if (outArgCount > 1 || (outArgCount > 0 && storedProcedure.isFunctionType())) {
      isCollection=true;
      isSimpleXMLFormat=true;
      result=new CollectionResult();
      result.setType(ANY_QNAME);
    }
 else {
      if (storedProcedure.isFunctionType()) {
        ArgumentType returnArg=((FunctionType)storedProcedure).getReturnArgument();
        result=buildResultForStoredFunction(returnArg,returnType);
        if (returnArg.getEnclosedType().isPLSQLCursorType()) {
          customizeSimpleXMLTagNames((PLSQLCursorType)returnArg.getEnclosedType(),procedureOperationModel);
        }
      }
 else       if (hasComplexArgs) {
        if (Util.noOutArguments(storedProcedure)) {
          result=new Result();
          result.setType(new QName(SCHEMA_URL,INT,SCHEMA_PREFIX));
        }
      }
 else {
        if (returnType != null) {
          result=new Result();
          result.setType(buildCustomQName(returnType,dbwsBuilder));
        }
 else {
          if (isCollection) {
            result=new CollectionResult();
            if (isSimpleXMLFormat) {
              result.setType(SXF_QNAME_CURSOR);
            }
          }
 else {
            result=new Result();
            result.setType(SXF_QNAME);
          }
        }
      }
    }
    for (    ArgumentType arg : storedProcedure.getArguments()) {
      String argName=arg.getArgumentName();
      if (argName != null) {
        QName xmlType=null;
        ProcedureArgument pa=null;
        ProcedureArgument paShadow=null;
        Parameter parm=null;
        ArgumentTypeDirection direction=arg.getDirection();
        if (!hasComplexArgs) {
          if (arg.getEnclosedType().isPLSQLCursorType()) {
            PLSQLCursorType cursorType=(PLSQLCursorType)arg.getEnclosedType();
            if (cursorType.isWeaklyTyped()) {
              xmlType=buildCustomQName("String_Node_Str",dbwsBuilder);
            }
          }
 else {
            xmlType=getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
          }
        }
 else {
          if (arg.getEnclosedType().isPLSQLType()) {
            hasComplexProcedureArgs=true;
            String packageName=((PLSQLType)arg.getEnclosedType()).getParentType().getPackageName();
            String typeString=(packageName != null && packageName.length() > 0) ? packageName + UNDERSCORE + arg.getTypeName() : arg.getTypeName();
            xmlType=buildCustomQName(typeString,dbwsBuilder);
          }
 else           if (arg.getEnclosedType().isVArrayType() || arg.getEnclosedType().isObjectType() || arg.getEnclosedType().isObjectTableType()) {
            hasComplexProcedureArgs=true;
            String typeString=arg.getTypeName().toLowerCase().concat(TYPE_STR);
            xmlType=buildCustomQName(typeString,dbwsBuilder);
          }
 else {
switch (Util.getJDBCTypeFromTypeName(arg.getTypeName())) {
case STRUCT:
case ARRAY:
              String typeString=nct.generateSchemaAlias(arg.getTypeName());
            xmlType=buildCustomQName(typeString,dbwsBuilder);
          break;
default :
        xmlType=getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
      break;
  }
}
}
if (direction == null || direction == IN) {
parm=new Parameter();
parm.setName(argName);
parm.setType(xmlType);
parm.setOptional(arg.optional());
pa=new ProcedureArgument();
pa.setName(argName);
pa.setParameterName(argName);
if (qh instanceof StoredProcedureQueryHandler) {
  ((StoredProcedureQueryHandler)qh).getInArguments().add(pa);
}
}
 else {
pa=new ProcedureOutputArgument();
ProcedureOutputArgument pao=(ProcedureOutputArgument)pa;
pao.setName(argName);
pao.setParameterName(argName);
boolean isCursor=arg.isPLSQLCursorType() || arg.getTypeName().contains(CURSOR_STR);
if (arg.isPLSQLCursorType()) {
  customizeSimpleXMLTagNames((PLSQLCursorType)arg.getEnclosedType(),procedureOperationModel);
}
if (isCursor && returnType == null) {
  pao.setResultType(SXF_QNAME_CURSOR);
  if (result == null) {
    result=new CollectionResult();
    result.setType(SXF_QNAME_CURSOR);
  }
}
 else {
  if (returnType != null && !isSimpleXMLFormat) {
    xmlType=qNameFromString(OPEN_PAREN + dbwsBuilder.getTargetNamespace() + CLOSE_PAREN+ returnType,dbwsBuilder.getSchema());
  }
  if (isCursor) {
    pao.setResultType(new QName(EMPTY_STRING,CURSOR_OF_STR + returnType));
    Result newResult=new CollectionResult();
    newResult.setType(result.getType());
    result=newResult;
  }
 else {
    pao.setResultType(xmlType);
  }
  if (result == null) {
    if (isCollection) {
      result=new CollectionResult();
    }
 else {
      result=new Result();
    }
    result.setType(xmlType);
  }
}
if (direction == INOUT) {
  parm=new Parameter();
  parm.setName(argName);
  parm.setType(xmlType);
  result.setType(xmlType);
  if (qh instanceof StoredProcedureQueryHandler) {
    ((StoredProcedureQueryHandler)qh).getInOutArguments().add(pao);
  }
  paShadow=new ProcedureArgument();
  paShadow.setName(argName);
  paShadow.setParameterName(argName);
}
 else {
  if (qh instanceof StoredProcedureQueryHandler) {
    ((StoredProcedureQueryHandler)qh).getOutArguments().add(pao);
  }
}
}
if (hasComplexArgs && arg.getEnclosedType().isPLSQLType()) {
pa.setComplexTypeName(storedProcedure.getCatalogName() + UNDERSCORE + arg.getTypeName());
if (paShadow != null) {
  paShadow.setComplexTypeName(pa.getComplexTypeName());
}
}
if (parm != null) {
qo.getParameters().add(parm);
}
}
}
if (procedureOperationModel.getBinaryAttachment()) {
Attachment attachment=new Attachment();
attachment.setMimeType(APP_OCTET_STREAM);
result.setAttachment(attachment);
}
handleSimpleXMLFormat(isSimpleXMLFormat,result,procedureOperationModel);
qo.setResult(result);
dbwsBuilder.getXrServiceModel().getOperations().put(qo.getName(),qo);
}
finishProcedureOperation();
}