Example #1
0
  public TS_Value readFlags(AtfxCache atfxCache, long iidLc) throws AoException {
    long start = System.currentTimeMillis();

    // read external component instances
    long aidLc = atfxCache.getAidsByBaseType("aolocalcolumn").iterator().next();
    ApplicationRelation relExtComps =
        atfxCache.getApplicationRelationByBaseName(aidLc, "external_component");
    Collection<Long> iidExtComps = atfxCache.getRelatedInstanceIds(aidLc, iidLc, relExtComps);

    if (iidExtComps.size() < 1) {
      return null;
    } else if (iidExtComps.size() != 1) {
      throw new AoException(
          ErrorCode.AO_NOT_IMPLEMENTED,
          SeverityFlag.ERROR,
          0,
          "The implementation currently only may read exactly one external component file");
    }
    long iidExtComp = iidExtComps.iterator().next();
    long aidExtComp = atfxCache.getAidsByBaseType("aoexternalcomponent").iterator().next();

    // get filename
    Integer attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "flags_filename_url");
    if (attrNo == null) {
      return null;
    }
    TS_Value v = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp);
    if ((v == null)
        || (v.flag != 15)
        || (v.u.stringVal() == null)
        || (v.u.stringVal().length() < 1)) {
      return null;
    }
    String flagsFilenameUrl = v.u.stringVal();
    File fileRoot = new File(atfxCache.getContext().get("FILE_ROOT").value.u.stringVal());
    File flagsFile = new File(fileRoot, flagsFilenameUrl);

    // read start offset, may be DT_LONG or DT_LONGLONG
    int flagsStartOffset = 0;
    attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "flags_start_offset");
    if (attrNo == null) {
      throw new AoException(
          ErrorCode.AO_NOT_FOUND,
          SeverityFlag.ERROR,
          0,
          "Application attribute derived from base attribute 'flags_start_offset' not found");
    }
    TS_Value vStartOffset = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp);
    if (vStartOffset.u.discriminator() == DataType.DT_LONG) {
      flagsStartOffset = vStartOffset.u.longVal();
    } else if (vStartOffset.u.discriminator() == DataType.DT_LONGLONG) {
      flagsStartOffset = (int) ODSHelper.asJLong(vStartOffset.u.longlongVal());
    }

    // read length
    attrNo = atfxCache.getAttrNoByBaName(aidExtComp, "component_length");
    int componentLength = atfxCache.getInstanceValue(aidExtComp, attrNo, iidExtComp).u.longVal();

    // read values
    TS_Value tsValue = new TS_Value();
    tsValue.flag = (short) 15;
    tsValue.u = new TS_Union();
    tsValue.u.shortSeq(new short[componentLength]);

    RandomAccessFile raf = null;
    byte[] backingBuffer = new byte[2];
    try {
      // open source channel
      raf = new BufferedRandomAccessFile(flagsFile, "r", BUFFER_SIZE);
      raf.seek(flagsStartOffset);

      for (int i = 0; i < componentLength; i++) {
        raf.read(backingBuffer, 0, backingBuffer.length);
        ByteBuffer sourceMbb = ByteBuffer.wrap(backingBuffer);
        sourceMbb.order(ByteOrder.LITTLE_ENDIAN);
        tsValue.u.shortSeq()[i] = sourceMbb.getShort();
      }

      LOG.info(
          "Read "
              + componentLength
              + " flags from component file '"
              + flagsFilenameUrl
              + "' in "
              + (System.currentTimeMillis() - start)
              + "ms");
      return tsValue;
    } catch (IOException e) {
      LOG.error(e.getMessage(), e);
      throw new AoException(ErrorCode.AO_NOT_FOUND, SeverityFlag.ERROR, 0, e.getMessage());
    } finally {
      backingBuffer = null;
      try {
        if (raf != null) {
          raf.close();
        }
        raf = null;
      } catch (Exception e) {
        LOG.error(e.getMessage(), e);
      }
    }
  }
Example #2
0
  public TS_Value readValues(AtfxCache atfxCache, long iidLc, DataType targetDataType)
      throws AoException {
    // read external component instances
    long aidLc = atfxCache.getAidsByBaseType("aolocalcolumn").iterator().next();
    ApplicationRelation relExtComps =
        atfxCache.getApplicationRelationByBaseName(aidLc, "external_component");
    List<Long> iidExtComps = atfxCache.getRelatedInstanceIds(aidLc, iidLc, relExtComps);
    if (iidExtComps.size() > 0) {
      Collections.sort(iidExtComps, new ExternalComponentComparator(atfxCache));
    }

    TS_Value tsValue = new TS_Value();
    tsValue.flag = (short) 15;
    tsValue.u = new TS_Union();

    // get raw data type
    DataType rawDataType = targetDataType;
    Integer attrNo = atfxCache.getAttrNoByBaName(aidLc, "raw_datatype");
    if (attrNo != null) {
      TS_Value valRawDatatype = atfxCache.getInstanceValue(aidLc, attrNo, iidLc);
      if (valRawDatatype != null && valRawDatatype.flag == 15) {
        int val = valRawDatatype.u.enumVal();
        if (val == 1) { // DT_STRING
          rawDataType = DataType.DS_STRING;
        } else if (val == 2) { // DT_SHORT
          rawDataType = DataType.DS_SHORT;
        } else if (val == 3) { // DT_FLOAT
          rawDataType = DataType.DS_FLOAT;
        } else if (val == 4) { // DT_BOOLEAN
          rawDataType = DataType.DS_BOOLEAN;
        } else if (val == 5) { // DT_BYTE
          rawDataType = DataType.DS_BYTE;
        } else if (val == 6) { // DT_LONG
          rawDataType = DataType.DS_LONG;
        } else if (val == 7) { // DT_DOUBLE
          rawDataType = DataType.DS_DOUBLE;
        } else if (val == 8) { // DT_LONGLONG
          rawDataType = DataType.DS_LONGLONG;
        } else if (val == 10) { // DT_DATE
          rawDataType = DataType.DS_DATE;
        } else if (val == 11) { // DT_BYTESTR
          rawDataType = DataType.DS_BYTESTR;
        } else if (val == 13) { // DT_COMPLEX
          rawDataType = DataType.DS_COMPLEX;
        } else if (val == 14) { // DT_DCOMPLEX
          rawDataType = DataType.DS_DCOMPLEX;
        } else if (val == 28) { // DT_EXTERNALREFERENCE
          rawDataType = DataType.DS_EXTERNALREFERENCE;
        } else if (val == 30) { // DT_ENUM
          rawDataType = DataType.DS_ENUM;
        }
      }
    }

    // DS_STRING
    if (rawDataType == DataType.DS_STRING || rawDataType == DataType.DS_BYTESTR) {
      List<String> list = new ArrayList<String>();
      for (long iidExtComp : iidExtComps) {
        list.addAll(readStringValues(atfxCache, iidExtComp));
      }
      tsValue.u.stringSeq(list.toArray(new String[0]));
    } // DS_DATE
    else if (rawDataType == DataType.DS_DATE) {
      List<String> list = new ArrayList<String>();
      for (long iidExtComp : iidExtComps) {
        list.addAll(readStringValues(atfxCache, iidExtComp));
      }
      tsValue.u.dateSeq(list.toArray(new String[0]));
    }
    // DS_NUMBER
    else {
      List<Number> list = new ArrayList<Number>();
      for (long iidExtComp : iidExtComps) {
        list.addAll(readNumberValues(atfxCache, iidExtComp, rawDataType));
      }
      // DS_BOOLEAN
      if (rawDataType == DataType.DS_BOOLEAN) {
        boolean[] ar = new boolean[list.size()];
        for (int i = 0; i < list.size(); i++) {
          ar[i] = list.get(i).byteValue() != 0;
        }
        tsValue.u.booleanSeq(ar);
      }
      // DS_BYTE
      else if (rawDataType == DataType.DS_BYTE) {
        byte[] ar = new byte[list.size()];
        for (int i = 0; i < list.size(); i++) {
          ar[i] = list.get(i).byteValue();
        }
        tsValue.u.byteSeq(ar);
      }
      // DS_SHORT
      else if (rawDataType == DataType.DS_SHORT) {
        short[] ar = new short[list.size()];
        for (int i = 0; i < list.size(); i++) {
          ar[i] = list.get(i).shortValue();
        }
        tsValue.u.shortSeq(ar);
      }
      // DS_LONG
      else if (rawDataType == DataType.DS_LONG) {
        int[] ar = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
          ar[i] = list.get(i).intValue();
        }
        tsValue.u.longSeq(ar);
      }
      // DS_DOUBLE
      else if (rawDataType == DataType.DS_DOUBLE) {
        double[] ar = new double[list.size()];
        for (int i = 0; i < list.size(); i++) {
          ar[i] = list.get(i).doubleValue();
        }
        tsValue.u.doubleSeq(ar);
      }
      // DS_LONGLONG
      else if (rawDataType == DataType.DS_LONGLONG) {
        T_LONGLONG[] ar = new T_LONGLONG[list.size()];
        for (int i = 0; i < list.size(); i++) {
          ar[i] = ODSHelper.asODSLongLong(list.get(i).longValue());
        }
        tsValue.u.longlongSeq(ar);
      }
      // DS_FLOAT
      else if (rawDataType == DataType.DS_FLOAT) {
        float[] ar = new float[list.size()];
        for (int i = 0; i < list.size(); i++) {
          ar[i] = list.get(i).floatValue();
        }
        tsValue.u.floatSeq(ar);
      }
      // DS_COMPLEX
      else if (rawDataType == DataType.DS_COMPLEX) {
        int size = list.size() / 2;
        T_COMPLEX[] ar = new T_COMPLEX[size];
        for (int i = 0; i < size; i++) {
          ar[i] = new T_COMPLEX();
          ar[i].r = list.get(i * 2).floatValue();
          ar[i].i = list.get(i * 2 + 1).floatValue();
        }
        tsValue.u.complexSeq(ar);
      }
      // DS_DCOMPLEX
      else if (rawDataType == DataType.DS_DCOMPLEX) {
        int size = list.size() / 2;
        T_DCOMPLEX[] ar = new T_DCOMPLEX[size];
        for (int i = 0; i < size; i++) {
          ar[i] = new T_DCOMPLEX();
          ar[i].r = list.get(i * 2).doubleValue();
          ar[i].i = list.get(i * 2 + 1).doubleValue();
        }
        tsValue.u.dcomplexSeq(ar);
      }
      // unsupported
      else {
        throw new AoException(
            ErrorCode.AO_NOT_IMPLEMENTED,
            SeverityFlag.ERROR,
            0,
            "Reading values from external component not yet supported for datatype: "
                + ODSHelper.dataType2String(targetDataType));
      }
    }
    return tsValue;
  }