Package weave.config

Examples of weave.config.DataConfig$DataEntity


      String[] configColumnNames, String[] sqlColumnNames, String sqlSchema, String sqlTable,
      boolean ignoreKeyColumnQueries, int[] filterColumnIndices, DataEntityMetadata tableImportInfo, boolean configAppend)
    throws RemoteException
  {
      int table_id = DataConfig.NULL; 
    DataConfig dataConfig = getDataConfig();
   
    if (sqlColumnNames == null || sqlColumnNames.length == 0)
      throw new RemoteException("No columns were found.");
    ConnectionInfo connInfo = getConnectionConfig().getConnectionInfo(connectionName);
    if (connInfo == null)
      throw new RemoteException(String.format("Connection named \"%s\" does not exist.", connectionName));
   
    String failMessage = String.format("Failed to add DataTable \"%s\" to the configuration.\n", configDataTableName);
    String query = null;
    try
    {
      Connection conn = connInfo.getStaticReadOnlyConnection();
     
      // If key column is actually the name of a column, put quotes around it.  Otherwise, don't.
      String q_sqlKeyColumn;
      if (ListUtils.findString(sqlKeyColumn, sqlColumnNames) >= 0)
        q_sqlKeyColumn = SQLUtils.quoteSymbol(conn, sqlKeyColumn);
      else
        q_sqlKeyColumn = sqlKeyColumn;
     
      String q_sqlSecondKeyColumn = secondKeyColumnIndex >= 0
          ? SQLUtils.quoteSymbol(conn, sqlColumnNames[secondKeyColumnIndex])
          : null;

      // Write SQL statements into sqlconfig.
     
      // generate and test each query before modifying config file
      List<ColumnInfo> columnInfoList = new Vector<ColumnInfo>();
      boolean foundMatchingColumnIds = false;
     
      SQLResult filteredValues = null;
      if (filterColumnIndices != null && filterColumnIndices.length > 0)
      {
        // get a list of unique combinations of filter values
        String columnList = "";
        for (int i = 0; i < filterColumnIndices.length; i++)
        {
          if (i > 0)
            columnList += ",";
          columnList += SQLUtils.quoteSymbol(conn, sqlColumnNames[filterColumnIndices[i]]);
        }
       
        query = String.format(
            "select distinct %s from %s order by %s",
            columnList,
            SQLUtils.quoteSchemaTable(conn, sqlSchema, sqlTable),
            columnList
          );
        filteredValues = SQLUtils.getResultFromQuery(conn, query, null, true);
        // System.out.println(query);
        // System.out.println(filteredValues);
      }
     
      String queryFormat = "SELECT %s,%s FROM %s";
      if (SQLUtils.isOracleServer(conn))
      {
        // workaround for ambiguous column name when filtering by rownum
        queryFormat = "SELECT %s thekey, %s thevalue FROM %s";
      }
     
      for (int iCol = 0; iCol < sqlColumnNames.length; iCol++)
      {
        String q_sqlColumn = sqlColumnNames[iCol];
        // System.out.println("columnName: " + columnName +
        // "\tkeyColumnName: " + keyColumnName + "\toriginalKeyCol: " +
        // originalKeyColumName);
        if (ignoreKeyColumnQueries && sqlKeyColumn.equals(q_sqlColumn))
          continue;
        q_sqlColumn = SQLUtils.quoteSymbol(conn, q_sqlColumn);

        // hack
        if (secondKeyColumnIndex >= 0)
          q_sqlColumn += "," + q_sqlSecondKeyColumn;

        // generate column query
        query = String.format(
            queryFormat,
            q_sqlKeyColumn,
            q_sqlColumn,
            SQLUtils.quoteSchemaTable(conn, sqlSchema, sqlTable)
          );

        DataEntityMetadata metaQuery = new DataEntityMetadata();
        // we don't search public metadata because that would be a separate sql query
        // and we only know the entityType.
        metaQuery.setPrivateValues(
            PrivateMetadata.CONNECTION, connectionName,
            PrivateMetadata.SQLQUERY, query
          );
        if (filteredValues != null)
        {
          String filteredQuery = buildFilteredQuery(conn, query, filteredValues.columnNames);
          metaQuery.setPrivateValues(PrivateMetadata.SQLQUERY, filteredQuery);
          // generate one query per unique filter value combination
          for (int iRow = 0; iRow < filteredValues.rows.length; iRow++)
          {
            ColumnInfo info = new ColumnInfo();
            columnInfoList.add(info);
           
            info.schema = sqlSchema;
            info.table = sqlTable;
            info.column = sqlColumnNames[iCol];
           
            info.sqlParamsArray = filteredValues.rows[iRow];
            info.sqlParamsStr = CSVParser.defaultParser.createCSVRow(info.sqlParamsArray, true);
            info.title = buildFilteredColumnTitle(configColumnNames[iCol], info.sqlParamsArray);
            info.query = filteredQuery;
            testQueryAndGetDataType(conn, info);
           
            if (configAppend)
            {
              // try to find a matching column using private metadata: connection, sqlQuery, and sqlParams
              metaQuery.setPrivateValues(PrivateMetadata.SQLPARAMS, info.sqlParamsStr);
              info.existingColumnId = ListUtils.getFirstSortedItem(
                  dataConfig.searchPrivateMetadata(metaQuery.privateMetadata, null),
                  DataConfig.NULL
                );
              if (info.existingColumnId != DataConfig.NULL)
                foundMatchingColumnIds = true;
            }
          }
        }
        else
        {
          ColumnInfo info = new ColumnInfo();
          columnInfoList.add(info);
         
          info.schema = sqlSchema;
          info.table = sqlTable;
          info.column = sqlColumnNames[iCol];
         
          info.title = configColumnNames[iCol];
          info.query = query;
          testQueryAndGetDataType(conn, info);
         
          if (configAppend)
          {
            // try to find a matching column using private metadata: connection and sqlQuery
            info.existingColumnId = ListUtils.getFirstSortedItem(
                dataConfig.searchPrivateMetadata(metaQuery.privateMetadata, null),
                DataConfig.NULL
              );
            if (info.existingColumnId != DataConfig.NULL)
              foundMatchingColumnIds = true;
          }
        }
      }
     
      // done generating column info
     
      // determine if columns should be appended to an existing table
      int existingTableId = DataConfig.NULL;
      if (foundMatchingColumnIds)
      {
        // get the set of all matching column ids
        Set<Integer> columnIds = new HashSet<Integer>();
        for (ColumnInfo info : columnInfoList)
          columnIds.add(info.existingColumnId);
        // find first matching parent table id
        for (Relationship r : dataConfig.getRelationships(columnIds))
        {
          String parentType = dataConfig.getEntityTypes(Arrays.asList(r.parentId)).get(r.parentId);
          if (Strings.equal(parentType, EntityType.TABLE))
          {
            existingTableId = r.parentId;
            break;
          }
        }
      }
     
      DataEntityMetadata tableInfo = tableImportInfo == null ? new DataEntityMetadata() : tableImportInfo;
      tableInfo.setPublicValues(PublicMetadata.ENTITYTYPE, EntityType.TABLE);
          tableInfo.setPrivateValues(
            PrivateMetadata.CONNECTION, connectionName,
            PrivateMetadata.SQLSCHEMA, sqlSchema,
            PrivateMetadata.SQLTABLE, sqlTable,
            PrivateMetadata.SQLKEYCOLUMN, sqlKeyColumn
          );
         
          if (existingTableId == DataConfig.NULL)
          {
            // only set title if creating a new table
            tableInfo.setPublicValues(PublicMetadata.TITLE, configDataTableName);
        table_id = dataConfig.newEntity(tableInfo, DataConfig.NULL, DataConfig.NULL);
          }
          else
          {
            table_id = existingTableId;
            // update private metadata only
            dataConfig.updateEntity(table_id, tableInfo);
          }
     
      for (int i = 0; i < columnInfoList.size(); i++)
      {
        ColumnInfo info = columnInfoList.get(i);
        DataEntityMetadata newMeta = new DataEntityMetadata();
       
        // only set title on new columns
        if (existingTableId == DataConfig.NULL || info.existingColumnId == DataConfig.NULL)
          newMeta.setPublicValues(PublicMetadata.TITLE, info.title);
         
        newMeta.setPublicValues(
          PublicMetadata.ENTITYTYPE, EntityType.COLUMN,
          PublicMetadata.KEYTYPE, keyType,
          PublicMetadata.DATATYPE, info.dataType,
          PublicMetadata.PROJECTION, info.projection
        );
        newMeta.setPrivateValues(
          PrivateMetadata.CONNECTION, connectionName,
          PrivateMetadata.SQLQUERY, info.query,
          PrivateMetadata.SQLSCHEMA, info.schema,
          PrivateMetadata.SQLTABLE, info.table,
          PrivateMetadata.SQLKEYCOLUMN, sqlKeyColumn,
          PrivateMetadata.SQLCOLUMN, info.column
        );
        if (filteredValues != null)
        {
          newMeta.setPrivateValues(
            PrivateMetadata.SQLPARAMS, info.sqlParamsStr,
            PrivateMetadata.SQLFILTERCOLUMNS, CSVParser.defaultParser.createCSVRow(ListUtils.getItems(sqlColumnNames, filterColumnIndices), true)
          );
        }

        // if not updating an existing column, create a new one
        if (existingTableId == DataConfig.NULL || info.existingColumnId == DataConfig.NULL)
          dataConfig.newEntity(newMeta, table_id, DataConfig.NULL);
        else
          dataConfig.updateEntity(info.existingColumnId, newMeta);
      }
    }
    catch (SQLException e)
    {
      throw new RemoteException(failMessage, e);
View Full Code Here


      String sqlSchema, String sqlTablePrefix, boolean sqlOverwrite, String configTitle,
      String configKeyType, String projectionSRS, String[] nullValues, boolean importDBFData, boolean append)
    throws RemoteException
  {
    ConnectionInfo connInfo = getConnectionInfo(configConnectionName, password);
    DataConfig dataConfig = getDataConfig();
   
    if (Strings.isEmpty(sqlSchema))
      throw new RemoteException("SQL schema must be specified.");
    if (Strings.isEmpty(sqlTablePrefix))
      throw new RemoteException("SQL table prefix must be specified.");
   
    // use lower case sql table names (fix for mysql linux problems)
    sqlTablePrefix = sqlTablePrefix.toLowerCase();

    if (sqlOverwrite && !connInfo.is_superuser)
      throw new RemoteException(String.format(
          "User \"%s\" does not have permission to overwrite SQL tables.", configConnectionName));

    String dbfTableName = sqlTablePrefix + "_dbfdata";
    Connection conn = null;
        int tableId = -1;
    try
    {
      conn = connInfo.getConnection();
      // store dbf data to database
      if (importDBFData)
      {
        importDBF(
            configConnectionName, password, fileNameWithoutExtension, sqlSchema, dbfTableName,
            sqlOverwrite, nullValues);
      }

      GeometryStreamConverter converter = new GeometryStreamConverter(new SQLGeometryStreamDestination(
          conn, sqlSchema, sqlTablePrefix, sqlOverwrite));
      for (String file : fileNameWithoutExtension)
      {
        // convert shape data to streaming sql format
        String shpfile = getUploadPath() + file + ".shp";
        SHPGeometryStreamUtils.convertShapefile(converter, shpfile, Arrays.asList(keyColumns));
      }
      converter.flushAndCommitAll();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw new RemoteException("Shapefile import failed", e);
    }
    finally
    {
      SQLUtils.cleanup(conn);
    }

        DataEntityMetadata tableInfo = new DataEntityMetadata();
        tableInfo.setPrivateValues(
          PrivateMetadata.IMPORTMETHOD, "importSHP",
          PrivateMetadata.FILENAME, CSVParser.defaultParser.createCSVRow(fileNameWithoutExtension, true),
          PrivateMetadata.KEYCOLUMN, CSVParser.defaultParser.createCSVRow(keyColumns, true),
          PrivateMetadata.CONNECTION, configConnectionName,
          PrivateMetadata.SQLSCHEMA, sqlSchema,
          PrivateMetadata.SQLTABLEPREFIX, sqlTablePrefix
        );
   
    if (importDBFData)
    {
     
      // get key column SQL code
      String keyColumnsString;
      if (keyColumns.length == 1)
      {
        keyColumnsString = keyColumns[0];
      }
      else
      {
        keyColumnsString = "CONCAT(";
        for (int i = 0; i < keyColumns.length; i++)
        {
          if (i > 0)
            keyColumnsString += ",";
          keyColumnsString += "CAST(" + keyColumns[i] + " AS CHAR)";
        }
        keyColumnsString += ")";
      }

      // add SQL statements to sqlconfig
      String[] columnNames = getSQLColumnNames(configConnectionName, password, sqlSchema, dbfTableName);
      tableId = addConfigDataTable(
          configTitle, configConnectionName,
          configKeyType, keyColumnsString, -1, columnNames, columnNames,
          sqlSchema, dbfTableName, false, null, tableInfo, append);
    }
    else
    {
      tableInfo.setPublicValues(
        PublicMetadata.ENTITYTYPE, EntityType.TABLE,
        PublicMetadata.TITLE, configTitle
      );
            tableId = dataConfig.newEntity(tableInfo, DataConfig.NULL, DataConfig.NULL);
    }

    try
    {
      // prepare metadata for existing column check
      DataEntityMetadata geomInfo = new DataEntityMetadata();
      // we don't search public metadata because that would require two sql queries
      geomInfo.setPrivateValues(
        PrivateMetadata.CONNECTION, configConnectionName,
        PrivateMetadata.SQLSCHEMA, sqlSchema,
        PrivateMetadata.SQLTABLEPREFIX, sqlTablePrefix
      );
     
      // check for existing geometry column using private metadata only
      int existingGeomId = ListUtils.getFirstSortedItem(
          dataConfig.searchPrivateMetadata(geomInfo.privateMetadata, null),
          DataConfig.NULL
        );
      if (existingGeomId != DataConfig.NULL)
      {
        // see if the existing geometry column has the same parent table
        boolean foundSameTableId = false;
        for (int parentId : dataConfig.getParentIds(existingGeomId))
          if (parentId == tableId)
            foundSameTableId = true;
        // if it does not have the same parent table, clear the existingGeomId
        // so a new column entity will be created under the new table
        if (!foundSameTableId)
          existingGeomId = DataConfig.NULL;
      }
     
      // set the new public metadata
      geomInfo.setPublicValues(
          PublicMetadata.ENTITYTYPE, EntityType.COLUMN,
          PublicMetadata.DATATYPE, DataType.GEOMETRY,
          PublicMetadata.KEYTYPE, configKeyType,
          PublicMetadata.PROJECTION, projectionSRS
      );
     
      if (existingGeomId == DataConfig.NULL)
      {
        // we only update the title if the column doesn't already exist
        geomInfo.setPublicValues(PublicMetadata.TITLE, configTitle);
        // create new column
        dataConfig.newEntity(geomInfo, tableId, 0);
      }
      else
      {
        // update existing column
        dataConfig.updateEntity(existingGeomId, geomInfo);
      }
    }
    catch (IOException e)
    {
      throw new RemoteException("Unexpected error",e);
View Full Code Here

   */
  public DataEntity[] testAllQueries(String user, String password, int table_id)
    throws RemoteException
  {
    authenticate(user, password);
    DataConfig config = getDataConfig();
    Collection<Integer> ids = config.getChildIds(table_id);
    DataEntity[] columns = config.getEntities(ids, true).toArray(new DataEntity[0]);
    String query = null;
    for (DataEntity entity : columns)
    {
      try
      {
View Full Code Here

  ////////////////////////////
  // Row query
 
  public WeaveRecordList getRows(String keyType, String[] keysArray) throws RemoteException
  {
    DataConfig dataConfig = getDataConfig();
   
    DataEntityMetadata params = new DataEntityMetadata();
    params.setPublicValues(
        PublicMetadata.ENTITYTYPE, EntityType.COLUMN,
        PublicMetadata.KEYTYPE, keyType
      );
    List<Integer> columnIds = new ArrayList<Integer>( dataConfig.searchPublicMetadata(params.publicMetadata, null) );

    if (columnIds.size() > MAX_COLUMN_REQUEST_COUNT)
      columnIds = columnIds.subList(0, MAX_COLUMN_REQUEST_COUNT);
    return DataService.getFilteredRows(ListUtils.toIntArray(columnIds), null, keysArray);
  }
View Full Code Here

      throw new RemoteException("At least one column must be specified.");
   
    if (filters != null)
      filters.assertValid();
   
    DataConfig dataConfig = getDataConfig();
    WeaveRecordList result = new WeaveRecordList();
    Map<Integer, DataEntity> entityLookup = new HashMap<Integer, DataEntity>();
   
    {
      // get all column IDs whether or not they are to be selected.
      Set<Integer> allColumnIds = new HashSet<Integer>();
      if (filters != null)
        allColumnIds.addAll(getReferencedColumnIds(filters));
      for (int id : columns)
        allColumnIds.add(id);
      // get all corresponding entities
      for (DataEntity entity : dataConfig.getEntities(allColumnIds, true))
        entityLookup.put(entity.id, entity);
      // check for missing columns
      for (int id : allColumnIds)
        if (entityLookup.get(id) == null)
          throw new RemoteException("No column with ID=" + id);
View Full Code Here

      metadata.remove(PublicMetadata.TITLE);
    String minStr = metadata.remove(PublicMetadata.MIN);
    String maxStr = metadata.remove(PublicMetadata.MAX);
    String paramsStr = metadata.remove(PrivateMetadata.SQLPARAMS);
   
    DataConfig dataConfig = getDataConfig();
   
    Collection<Integer> ids = dataConfig.searchPublicMetadata(metadata, null);
   
    // attempt recovery for backwards compatibility
    if (ids.size() == 0)
    {
      if (metadata.containsKey(DATATABLE) && metadata.containsKey(NAME))
      {
        // try to find columns sqlTable==dataTable and sqlColumn=name
        Map<String,String> privateMetadata = new HashMap<String,String>();
        String sqlTable = metadata.get(DATATABLE);
        String sqlColumn = metadata.get(NAME);
        for (int i = 0; i < 2; i++)
        {
          if (i == 1)
            sqlTable = sqlTable.toLowerCase();
          privateMetadata.put(PrivateMetadata.SQLTABLE, sqlTable);
          privateMetadata.put(PrivateMetadata.SQLCOLUMN, sqlColumn);
          ids = dataConfig.searchPrivateMetadata(privateMetadata, null);
          if (ids.size() > 0)
            break;
        }
      }
      else if (metadata.containsKey(NAME)
          && Strings.equal(metadata.get(PublicMetadata.DATATYPE), DataType.GEOMETRY))
      {
        metadata.put(PublicMetadata.TITLE, metadata.remove(NAME));
        ids = dataConfig.searchPublicMetadata(metadata, null);
      }
      if (ids.size() == 0)
        throw new RemoteException("No column matches metadata query: " + metadata);
    }
   
View Full Code Here

TOP

Related Classes of weave.config.DataConfig$DataEntity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.