Ejemplo n.º 1
0
 public Map<String, Integer> generateDataHeaderMapFromVariables() {
   Map<String, Integer> headerMap = new HashMap<>();
   int count = 1;
   for (Variable variable : valueTable.getVariables()) {
     headerMap.put(variable.getName(), count++);
   }
   return headerMap;
 }
Ejemplo n.º 2
0
  public Value getValue(Variable variable) {
    if (variable.getValueType().isBinary()) return getBinaryValue(variable);

    loadResultSetCache();

    Value value = convertValue(variable, resultSetCache.get(variable.getName()));
    resultSetCache.put(variable.getName(), value);
    return value;
  }
Ejemplo n.º 3
0
 /**
  * Convert the value as loaded from the SQL table into the expected variable value type (column
  * type may not match exactly the variable value type).
  *
  * @param variable
  * @param value
  * @return
  */
 private Value convertValue(Variable variable, Value value) {
   if (value.getValueType() != variable.getValueType()) {
     return variable.isRepeatable()
         ? convertToSequence(variable, value)
         : variable.getValueType().convert(value);
   }
   if (variable.isRepeatable() && !value.isSequence()) {
     return convertToSequence(variable, value);
   }
   return value;
 }
Ejemplo n.º 4
0
  private Value getBinaryValue(Variable variable) {
    List<Map<String, Value>> res =
        loadValues(
            Lists.newArrayList(getValueTable().getVariableSqlName(variable.getName())), mapper);

    if (res.isEmpty())
      return variable.isRepeatable()
          ? variable.getValueType().nullSequence()
          : variable.getValueType().nullValue();

    Value value = res.get(0).get(variable.getName());
    return convertValue(variable, value);
  }
Ejemplo n.º 5
0
 private static void addVariableAttributesHeaders(ValueTable table, Collection<String> headers) {
   for (Variable variable : table.getVariables()) {
     for (Attribute attribute : variable.getAttributes()) {
       //noinspection NonConstantStringShouldBeStringBuffer
       String header = attribute.getName();
       if (attribute.isLocalised()) {
         header += ":" + attribute.getLocale();
       }
       if (!headers.contains(header)) {
         headers.add(header);
       }
     }
   }
 }
Ejemplo n.º 6
0
  private void doWriteGitViewVariable(File viewRepo, Variable variable) throws IOException {
    if (variable.hasAttribute("script")) {
      String script = variable.getAttributeStringValue("script");
      File variableFile = new File(viewRepo, variable.getName() + ".js");

      FileWriter fileWriter = new FileWriter(variableFile);
      try {
        fileWriter.append(script);
        fileWriter.flush();
      } finally {
        StreamUtil.silentSafeClose(fileWriter);
      }
    }
  }
Ejemplo n.º 7
0
 private static void addCategoriesHeaders(ValueTable table, List<String> headers) {
   for (Variable variable : table.getVariables()) {
     for (Category category : variable.getCategories()) {
       for (Attribute attribute : category.getAttributes()) {
         if (attribute.getName().equals(VariableConverter.LABEL) && attribute.isLocalised()) {
           String header = VariableConverter.CATEGORIES + ":" + attribute.getLocale();
           if (!headers.contains(header)) {
             headers.add(header);
           }
         }
       }
     }
   }
   if (!headers.get(headers.size() - 1).startsWith(VariableConverter.CATEGORIES)) {
     headers.add(VariableConverter.CATEGORIES);
   }
 }
    /** Returns an iterator of category names */
    private Iterator<String> categoryNames() {
      if (variable.getValueType().equals(BooleanType.get())) {
        return ImmutableList.<String>builder() //
            .add(BooleanType.get().trueValue().toString()) //
            .add(BooleanType.get().falseValue().toString())
            .build()
            .iterator();
      }

      return Iterables.transform(
              variable.getCategories(),
              new Function<Category, String>() {

                @Override
                public String apply(Category from) {
                  return from.getName();
                }
              })
          .iterator();
    }
  private void importIdentifiers(
      @NotNull IdentifiersMapping idMapping, ValueTable sourceTable, @Nullable String select) {

    Variable variable = identifiersTableService.ensureIdentifiersMapping(idMapping);
    String selectScript =
        select == null //
            ? variable.hasAttribute("select")
                ? variable.getAttributeStringValue("select")
                : null //
            : select;

    ValueTable sourceIdentifiersTable =
        identifierService.createPrivateView(sourceTable.getName(), sourceTable, selectScript);
    Variable identifierVariable =
        identifierService.createIdentifierVariable(sourceIdentifiersTable, idMapping);

    PrivateVariableEntityMap entityMap =
        new OpalPrivateVariableEntityMap(
            identifiersTableService.getIdentifiersTable(idMapping.getEntityType()),
            identifierVariable,
            participantIdentifier);

    try (ValueTableWriter identifiersTableWriter =
        identifiersTableService.createIdentifiersTableWriter(idMapping.getEntityType())) {
      for (VariableEntity privateEntity : sourceIdentifiersTable.getVariableEntities()) {
        if (entityMap.publicEntity(privateEntity) == null) {
          entityMap.createPublicEntity(privateEntity);
        }
        identifierService.copyParticipantIdentifiers(
            entityMap.publicEntity(privateEntity),
            sourceIdentifiersTable,
            entityMap,
            identifiersTableWriter);
      }
    }
  }
Ejemplo n.º 10
0
    @Override
    public void writeVariable(@NotNull Variable variable) {
      try {

        VariableConverter variableConverter = valueTable.getVariableConverter();
        if (valueTable.isVariablesFileEmpty()) {
          // Write Header
          writeVariableToCsv(variableConverter.getHeader());
          valueTable.setVariablesFileEmpty(false);
        } else if (valueTable.hasVariable(variable.getName())) {
          // doing an update.
          valueTable.clearVariable(variable);
        }

        String[] line = variableConverter.marshal(variable);
        long lastByte = valueTable.getVariablesLastByte();
        writeVariableToCsv(line);
        valueTable.updateVariableIndex(variable, lastByte, line);
      } catch (IOException e) {
        throw new MagmaRuntimeException(e);
      }
    }
Ejemplo n.º 11
0
 private Value convertToSequence(Variable variable, Value value) {
   return value.isNull()
       ? variable.getValueType().nullSequence()
       : variable.getValueType().sequenceOf(value.toString());
 }