Search in sources :

Example 6 with Selection

use of com.dexels.navajo.document.Selection in project navajo by Dexels.

the class MappingUtils method getSelectedItems.

public static final List<Selection> getSelectedItems(Message msg, Navajo doc, String msgName) throws NavajoException {
    Property prop = null;
    if (msg != null) {
        prop = msg.getProperty(msgName);
    } else {
        prop = doc.getProperty(msgName);
    }
    if (!prop.getType().equals(Property.SELECTION_PROPERTY)) {
        throw doc.getNavajoFactory().createNavajoException("Selection Property expected");
    }
    List<Selection> result = prop.getAllSelectedSelections();
    return result;
}
Also used : Selection(com.dexels.navajo.document.Selection) Property(com.dexels.navajo.document.Property)

Example 7 with Selection

use of com.dexels.navajo.document.Selection in project navajo by Dexels.

the class MappingUtils method setProperty.

public static final Property setProperty(boolean parameter, Message msg, String name, Object value, String type, String subtype, String direction, String description, int length, Navajo outputDoc, Navajo tmlDoc, boolean remove) throws MappingException {
    Message ref = null;
    if (parameter) {
        if (msg == null) {
            msg = tmlDoc.getMessage("__parms__");
            if (msg == null) {
                // Create __parms__ message.
                msg = NavajoFactory.getInstance().createMessage(tmlDoc, "__parms__");
                tmlDoc.addMessage(msg);
            }
        }
        ref = getMessageObject(name, msg, false, tmlDoc, false, "", -1);
        if (ref == null) {
            // Can be null due to absolute param name (starting with '/'). In this case use
            // __parms__ as parent.
            ref = tmlDoc.getMessage("__parms__");
        }
    } else {
        ref = getMessageObject(name, msg, false, outputDoc, false, "", -1);
    }
    if (ref == null) {
        ref = msg;
    }
    String actualName = getStrippedPropertyName(name);
    if (ref == null) {
        throw new MappingException("Property can only be created under a message");
    }
    // with ../ constructions in the name of the new property, it is possible to create a property at the rootMessage of a NavajoDoc but that will never be serialized. Logger message to see how often it happens
    if (ref.equals(ref.getRootDoc().getRootMessage())) {
        logger.warn("WARNING - Adding property to rootMessage of NavajoDoc - property will not be findable");
    }
    Property prop = ref.getProperty(actualName);
    // Remove a parameter if remove flag is set.
    if (remove && prop != null && parameter) {
        ref.removeProperty(prop);
        return null;
    }
    if (prop == null && remove && parameter) {
        return null;
    }
    if (prop == null) {
        // Property does not exist.
        if (!parameter) {
            if (value instanceof Property) {
                // Value is a property itself!
                prop = (Property) ((Property) value).clone(name);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                // Legacy mode hack, many scripts do not expect null valued string properties.
                if (Property.STRING_PROPERTY.equals(type) && value == null) {
                    value = "";
                }
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        } else {
            if (Property.EXPRESSION_LITERAL_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                prop.setValue(new NavajoExpression(value.toString()));
                prop.setType(type);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        }
        ref.addProperty(prop);
    } else {
        // Existing property.
        prop.clearValue();
        if (value instanceof Property) {
            // Value is a property itself!
            prop = (Property) ((Property) value).clone(name);
        } else if (Property.BINARY_PROPERTY.equals(type)) {
            if (value instanceof Binary) {
                prop.setValue((Binary) value);
            } else {
                prop.clearValue();
            }
        } else if (Property.SELECTION_PROPERTY.equals(type) && value != null && value instanceof Selection[]) {
            prop.setCardinality("+");
            prop.setValue((Selection[]) value);
        } else if (!Property.SELECTION_PROPERTY.equals(type)) {
            if (value != null) {
                if ((value instanceof StringLiteral)) {
                    prop.setUnCheckedStringAsValue(((StringLiteral) value).toString());
                } else {
                    prop.setAnyValue(value);
                }
            } else {
                prop.clearValue();
            }
        }
        if (Property.DIR_IN.equals(direction) || Property.DIR_OUT.equals(direction)) {
            prop.setDirection(direction);
        }
        prop.setType(type);
        // Should not matter ;)
        prop.setName(actualName);
    }
    // Set subtype if not empty.
    if (subtype != null && !subtype.equals("")) {
        prop.setSubType(subtype);
    }
    // Set description if not empty.
    if (description != null && !description.equals("")) {
        prop.setDescription(description);
    }
    // Set length if not empty ( = -1) .
    if (length != -1) {
        prop.setLength(length);
    }
    return prop;
}
Also used : NavajoExpression(com.dexels.navajo.document.types.NavajoExpression) Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property) MappingException(com.dexels.navajo.script.api.MappingException)

Example 8 with Selection

use of com.dexels.navajo.document.Selection in project navajo by Dexels.

the class NavajoStreamCollector method createTmlProperty.

private Property createTmlProperty(Prop p) {
    Property result;
    if (Property.SELECTION_PROPERTY.equals(p.type())) {
        result = NavajoFactory.getInstance().createProperty(assemble, p.name(), p.cardinality().orElse(null), p.description(), p.direction().orElse(null));
        for (Select s : p.selections()) {
            Selection sel = NavajoFactory.getInstance().createSelection(assemble, s.name(), s.value(), s.selected());
            result.addSelection(sel);
        }
    } else {
        result = NavajoFactory.getInstance().createProperty(assemble, p.name(), p.type() == null ? Property.STRING_PROPERTY : p.type(), null, p.length(), p.description(), p.direction().orElse(null));
        if (p.value() != null) {
            result.setAnyValue(p.value());
        }
        if (p.type() != null) {
            result.setType(p.type());
        }
    }
    return result;
}
Also used : Selection(com.dexels.navajo.document.Selection) Select(com.dexels.navajo.document.stream.api.Select) Property(com.dexels.navajo.document.Property)

Example 9 with Selection

use of com.dexels.navajo.document.Selection in project navajo by Dexels.

the class ParseSelection method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() == 0) {
        throw new TMLExpressionException(this, "Operand expected");
    }
    Object o = getOperand(0);
    if (o instanceof String) {
        StringTokenizer st = new StringTokenizer((String) o, ";");
        Navajo dummy = NavajoFactory.getInstance().createNavajo();
        Selection[] allSelections = new Selection[st.countTokens()];
        int index = 0;
        while (st.hasMoreTokens()) {
            String value = st.nextToken();
            allSelections[index++] = NavajoFactory.getInstance().createSelection(dummy, value, value, true);
        }
        return allSelections;
    } else {
        throw new TMLExpressionException(this, "String expected");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) Selection(com.dexels.navajo.document.Selection) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 10 with Selection

use of com.dexels.navajo.document.Selection in project navajo by Dexels.

the class APIValue method setValueOnNodeForType.

// private static final String TYPE_BOOLEAN = "boolean";
public static void setValueOnNodeForType(ObjectNode node, String id, String type, Property property, ArticleRuntime runtime) throws APIException {
    if (property == null || property.getTypedValue() == null) {
        node.putNull(id);
    } else {
        if (TYPE_SELECTION.equals(type)) {
            ArrayNode options = runtime.getObjectMapper().createArrayNode();
            if (property.getAllSelections() != null && property.getAllSelections().size() > 0) {
                for (Selection s : property.getAllSelections()) {
                    ObjectNode option = runtime.getObjectMapper().createObjectNode();
                    option.put("selected", s.isSelected());
                    option.put("value", s.getValue());
                    option.put("name", s.getName());
                    options.add(option);
                }
            }
            node.set(id, options);
        } else if (TYPE_DATETIME.equals(type)) {
            validatedType(property, Property.DATE_PROPERTY, id);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            String ISO8601Date = df.format(((Date) property.getTypedValue()));
            node.put(id, ISO8601Date);
        } else if (TYPE_INTEGER.equals(type)) {
            // Null number are sometimes represented as empty strings.
            if (property.getTypedValue() instanceof String && ((String) property.getTypedValue()).trim().length() == 0) {
                node.putNull(id);
            } else {
                Integer value = -1;
                try {
                    value = Integer.parseInt(property.getValue());
                } catch (NumberFormatException e) {
                    throw new APIException("Id " + id + " says it is an integer but cannot be parsed to an int", null, APIErrorCode.InternalError);
                }
                node.put(id, value);
            }
        } else {
            if (property.getType().equals(Property.SELECTION_PROPERTY)) {
                if (property.getSelected() == null || property.getSelected().getName() == Selection.DUMMY_SELECTION) {
                    node.putNull(id);
                } else {
                    node.put(id, property.getSelected().getName());
                }
            } else if (property.getType().equals(Property.BINARY_PROPERTY)) {
                // Put the binary object in - TmlBinarySerializer will take care of serializing it
                node.set(id, new POJONode(property.getTypedValue()));
            } else {
                // Default
                node.put(id, property.getValue());
            }
        }
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Selection(com.dexels.navajo.document.Selection) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) POJONode(com.fasterxml.jackson.databind.node.POJONode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

Selection (com.dexels.navajo.document.Selection)78 Property (com.dexels.navajo.document.Property)36 Test (org.junit.Test)32 Message (com.dexels.navajo.document.Message)24 Navajo (com.dexels.navajo.document.Navajo)21 Access (com.dexels.navajo.script.api.Access)14 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)13 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)12 TipiLink (com.dexels.navajo.expression.api.TipiLink)12 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)12 ArrayList (java.util.ArrayList)12 Optional (java.util.Optional)12 Operand (com.dexels.navajo.document.Operand)11 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)11 List (java.util.List)8 NavajoException (com.dexels.navajo.document.NavajoException)6 UserException (com.dexels.navajo.script.api.UserException)4 Binary (com.dexels.navajo.document.types.Binary)3 FunctionClassification (com.dexels.navajo.expression.api.FunctionClassification)3 StringTokenizer (java.util.StringTokenizer)3