Program: 126349
Project: 126349
Buggy source:
/** 
 * DOC wchen Comment method "createMetadataFromXSD".
 * @param parameter
 * @param connectionLabel
 * @param portTypeName
 * @param operationName
 * @param schemaFile
 * @param selectItems
 * @param zip
 * @param populationUtil
 * @throws IOException
 */
public static void createMetadataFromXSD(QName parameter,String connectionLabel,String portTypeName,String operationName,File schemaFile,Collection<XmlFileConnectionItem> selectItems,File zip,XSDPopulationUtil2 populationUtil) throws IOException {
  String name=parameter.getLocalPart();
  XmlFileConnection connection=null;
  Property connectionProperty=null;
  XmlFileConnectionItem connectionItem=null;
  String oldConnectionId=null;
  String oldTableId=null;
  IMetadataTable oldMetadataTable=null;
  Map<String,String> oldTableMap=null;
  if (!selectItems.isEmpty()) {
    boolean needRewrite=false;
    for (    XmlFileConnectionItem item : selectItems) {
      connectionProperty=item.getProperty();
      if (connectionProperty.getLabel().equals(name)) {
        oldConnectionId=connectionProperty.getId();
        connectionItem=item;
        connection=(XmlFileConnection)connectionItem.getConnection();
        needRewrite=true;
        Set<MetadataTable> tables=ConnectionHelper.getTables(connection);
        MetadataTable oldTable=null;
        if (tables.size() > 0) {
          oldTable=tables.toArray(new MetadataTable[0])[0];
          oldTableId=oldTable.getId();
          oldMetadataTable=ConvertionHelper.convert(oldTable);
        }
        oldTableMap=RepositoryUpdateManager.getOldTableIdAndNameMap(connectionItem,oldTable,false);
        break;
      }
    }
    if (!needRewrite && !isNameValidInXmlFileConnection(parameter,portTypeName,operationName)) {
      return;
    }
  }
  connection=ConnectionFactory.eINSTANCE.createXmlFileConnection();
  connection.setName(ERepositoryObjectType.METADATA_FILE_XML.getKey());
  connectionItem=PropertiesFactory.eINSTANCE.createXmlFileConnectionItem();
  connectionProperty=PropertiesFactory.eINSTANCE.createProperty();
  connectionProperty.setAuthor(((RepositoryContext)CoreRuntimePlugin.getInstance().getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getUser());
  connectionProperty.setLabel(connectionLabel);
  connectionProperty.setVersion(VersionUtils.DEFAULT_VERSION);
  connectionProperty.setStatusCode("String_Node_Str");
  connectionItem.setProperty(connectionProperty);
  connectionItem.setConnection(connection);
  connection.setInputModel(false);
  ByteArray byteArray=PropertiesFactory.eINSTANCE.createByteArray();
  byteArray.setInnerContentFromFile(zip);
  connection.setFileContent(byteArray.getInnerContent());
  String schemaFileName=schemaFile.getName();
  schemaFileName=schemaFileName.substring(0,schemaFileName.lastIndexOf("String_Node_Str"));
  connection.setXmlFilePath(schemaFileName.concat("String_Node_Str").concat(zip.getName()));
  try {
    String filePath=schemaFile.getPath();
    XSDSchema xsdSchema=populationUtil.getXSDSchema(filePath);
    List<ATreeNode> rootNodes=populationUtil.getAllRootNodes(xsdSchema);
    ATreeNode node=null;
    for (    ATreeNode curNode : rootNodes) {
      String curName=(String)curNode.getValue();
      if (curName.contains("String_Node_Str")) {
        if (curName.split("String_Node_Str")[1].equals(name)) {
          node=curNode;
          break;
        }
      }
 else       if (curName.equals(name)) {
        node=curNode;
        break;
      }
    }
    node=populationUtil.getSchemaTree(xsdSchema,node);
    orderId=1;
    loopElementFound=false;
    if (ConnectionHelper.getTables(connection).isEmpty()) {
      MetadataTable table=ConnectionFactory.eINSTANCE.createMetadataTable();
      if (oldTableId != null) {
        table.setId(oldTableId);
      }
 else {
        table.setId(ProxyRepositoryFactory.getInstance().getNextId());
      }
      RecordFile record=(RecordFile)ConnectionHelper.getPackage(connection.getName(),connection,RecordFile.class);
      if (record != null) {
        PackageHelper.addMetadataTable(table,record);
      }
 else {
        RecordFile newrecord=RecordFactory.eINSTANCE.createRecordFile();
        newrecord.setName(connection.getName());
        ConnectionHelper.addPackage(newrecord,connection);
        PackageHelper.addMetadataTable(table,newrecord);
      }
    }
    boolean haveElement=false;
    for (    Object curNode : node.getChildren()) {
      if (((ATreeNode)curNode).getType() == ATreeNode.ELEMENT_TYPE) {
        haveElement=true;
        break;
      }
    }
    fillRootInfo(connection,node,"String_Node_Str",!haveElement);
  }
 catch (  IOException e) {
    throw e;
  }
catch (  URISyntaxException e1) {
    ExceptionHandler.process(e1);
  }
catch (  OdaException e) {
    ExceptionHandler.process(e);
  }
  IProxyRepositoryFactory factory=ProxyRepositoryFactory.getInstance();
  connectionProperty.setId(factory.getNextId());
  try {
    String folderPath=getImportedXmlSchemaPath(parameter.getNamespaceURI(),portTypeName,operationName);
    IPath path=new Path(folderPath);
    factory.create(connectionItem,path,true);
    if (oldConnectionId != null) {
      connectionItem.getProperty().setId(oldConnectionId);
      factory.save(connectionItem);
    }
    propagateSchemaChange(oldMetadataTable,oldTableMap,connection,connectionItem);
    ProxyRepositoryFactory.getInstance().saveProject(ProjectManager.getInstance().getCurrentProject());
  }
 catch (  PersistenceException e) {
    ExceptionHandler.process(e);
  }
catch (  URISyntaxException e) {
    ExceptionHandler.process(e);
  }
}

Fixed version (ground truth):
/** 
 * DOC wchen Comment method "createMetadataFromXSD".
 * @param parameter
 * @param connectionLabel
 * @param portTypeName
 * @param operationName
 * @param schemaFile
 * @param selectItems
 * @param zip
 * @param populationUtil
 * @throws IOException
 */
public static void createMetadataFromXSD(QName parameter,String connectionLabel,String portTypeName,String operationName,File schemaFile,Collection<XmlFileConnectionItem> selectItems,File zip,XSDPopulationUtil2 populationUtil) throws IOException {
  String name=parameter.getLocalPart();
  XmlFileConnection connection=null;
  Property connectionProperty=null;
  XmlFileConnectionItem connectionItem=null;
  String oldConnectionId=null;
  String oldTableId=null;
  IMetadataTable oldMetadataTable=null;
  Map<String,String> oldTableMap=null;
  if (!selectItems.isEmpty()) {
    boolean needRewrite=false;
    for (    XmlFileConnectionItem item : selectItems) {
      connectionProperty=item.getProperty();
      if (connectionProperty.getLabel().equals(name)) {
        oldConnectionId=connectionProperty.getId();
        connectionItem=item;
        connection=(XmlFileConnection)connectionItem.getConnection();
        needRewrite=true;
        Set<MetadataTable> tables=ConnectionHelper.getTables(connection);
        MetadataTable oldTable=null;
        if (tables.size() > 0) {
          oldTable=tables.toArray(new MetadataTable[0])[0];
          oldTableId=oldTable.getId();
          oldMetadataTable=ConvertionHelper.convert(oldTable);
        }
        oldTableMap=RepositoryUpdateManager.getOldTableIdAndNameMap(connectionItem,oldTable,false);
        break;
      }
    }
    if (!needRewrite && !isNameValidInXmlFileConnection(parameter,portTypeName,operationName)) {
      return;
    }
  }
  connection=ConnectionFactory.eINSTANCE.createXmlFileConnection();
  connection.setName(ERepositoryObjectType.METADATA_FILE_XML.getKey());
  connectionItem=PropertiesFactory.eINSTANCE.createXmlFileConnectionItem();
  connectionProperty=PropertiesFactory.eINSTANCE.createProperty();
  connectionProperty.setAuthor(((RepositoryContext)CoreRuntimePlugin.getInstance().getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getUser());
  connectionProperty.setLabel(connectionLabel);
  connectionProperty.setVersion(VersionUtils.DEFAULT_VERSION);
  connectionProperty.setStatusCode("String_Node_Str");
  connectionItem.setProperty(connectionProperty);
  connectionItem.setConnection(connection);
  connection.setInputModel(false);
  ByteArray byteArray=PropertiesFactory.eINSTANCE.createByteArray();
  byteArray.setInnerContentFromFile(zip);
  connection.setFileContent(byteArray.getInnerContent());
  String schemaFileName=schemaFile.getName();
  schemaFileName=schemaFileName.substring(0,schemaFileName.lastIndexOf("String_Node_Str"));
  connection.setXmlFilePath(schemaFileName.concat("String_Node_Str").concat(zip.getName()));
  try {
    String filePath=schemaFile.getPath();
    XSDSchema xsdSchema=populationUtil.getXSDSchema(filePath);
    List<ATreeNode> rootNodes=populationUtil.getAllRootNodes(xsdSchema);
    ATreeNode node=null;
    for (    ATreeNode curNode : rootNodes) {
      String curName=(String)curNode.getValue();
      if (curName.contains("String_Node_Str")) {
        if (curName.split("String_Node_Str")[1].equals(name)) {
          node=curNode;
          break;
        }
      }
 else       if (curName.equals(name)) {
        node=curNode;
        break;
      }
    }
    node=populationUtil.getSchemaTree(xsdSchema,node);
    orderId=1;
    loopElementFound=false;
    if (ConnectionHelper.getTables(connection).isEmpty()) {
      MetadataTable table=ConnectionFactory.eINSTANCE.createMetadataTable();
      if (oldTableId != null) {
        table.setId(oldTableId);
      }
 else {
        table.setId(ProxyRepositoryFactory.getInstance().getNextId());
      }
      RecordFile record=(RecordFile)ConnectionHelper.getPackage(connection.getName(),connection,RecordFile.class);
      if (record != null) {
        PackageHelper.addMetadataTable(table,record);
      }
 else {
        RecordFile newrecord=RecordFactory.eINSTANCE.createRecordFile();
        newrecord.setName(connection.getName());
        ConnectionHelper.addPackage(newrecord,connection);
        PackageHelper.addMetadataTable(table,newrecord);
      }
    }
    boolean haveElement=false;
    for (    Object curNode : node.getChildren()) {
      if (((ATreeNode)curNode).getType() == ATreeNode.ELEMENT_TYPE) {
        haveElement=true;
        break;
      }
    }
    List<FOXTreeNode> foxTreeNodes=TreeUtil.getFoxTreeNodesByRootNode(xsdSchema,node,true);
    if (foxTreeNodes.size() > 0 && false) {
      FOXTreeNode foxTreeNode=foxTreeNodes.get(0);
      EList root=connection.getRoot();
      if (root != null) {
        XMLFileNode xmlFileNode=ConnectionFactory.eINSTANCE.createXMLFileNode();
        String currentPath="String_Node_Str" + foxTreeNode.getLabel();
        xmlFileNode.setXMLPath(currentPath);
        xmlFileNode.setRelatedColumn(foxTreeNode.getColumnLabel());
        xmlFileNode.setAttribute(foxTreeNode.isMain() ? "String_Node_Str" : "String_Node_Str");
        xmlFileNode.setDefaultValue(foxTreeNode.getDefaultValue());
        xmlFileNode.setType(foxTreeNode.getDataType());
        XMLFileNode originalXmlNode=null;
        if (root.size() > 0) {
          originalXmlNode=(XMLFileNode)root.get(0);
        }
        root.clear();
        root.add(xmlFileNode);
      }
    }
    fillRootInfo(connection,node,"String_Node_Str",!haveElement);
  }
 catch (  IOException e) {
    throw e;
  }
catch (  URISyntaxException e1) {
    ExceptionHandler.process(e1);
  }
catch (  OdaException e) {
    ExceptionHandler.process(e);
  }
  IProxyRepositoryFactory factory=ProxyRepositoryFactory.getInstance();
  connectionProperty.setId(factory.getNextId());
  try {
    String folderPath=getImportedXmlSchemaPath(parameter.getNamespaceURI(),portTypeName,operationName);
    IPath path=new Path(folderPath);
    factory.create(connectionItem,path,true);
    if (oldConnectionId != null) {
      connectionItem.getProperty().setId(oldConnectionId);
      factory.save(connectionItem);
    }
    propagateSchemaChange(oldMetadataTable,oldTableMap,connection,connectionItem);
    ProxyRepositoryFactory.getInstance().saveProject(ProjectManager.getInstance().getCurrentProject());
  }
 catch (  PersistenceException e) {
    ExceptionHandler.process(e);
  }
catch (  URISyntaxException e) {
    ExceptionHandler.process(e);
  }
}