Search in sources :

Example 1 with ScriptableArrayBuilder

use of edu.mit.simile.backstage.util.ScriptableArrayBuilder in project backstage by zepheira.

the class DomLensNode method generateSubcontentAttributes.

protected void generateSubcontentAttributes(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
    ScriptableArrayBuilder arrayBuilder = new ScriptableArrayBuilder();
    for (SubcontentAttribute a : _subcontentAttributes) {
        DefaultScriptableObject o = new DefaultScriptableObject();
        o.put("name", o, a.name);
        StringBuffer sb = new StringBuffer();
        for (Fragment f : a.fragments) {
            if (f instanceof StringFragment) {
                sb.append(((StringFragment) f).text);
            } else {
                try {
                    boolean first = true;
                    ExpressionQueryResult eqr = ((ExpressionFragment) f).expression.computeOutputOnValue(value, database, connection);
                    if (eqr != null) {
                        TupleQueryResult queryResult = eqr.tupleQuery.evaluate();
                        try {
                            while (queryResult.hasNext()) {
                                BindingSet bindingSet = queryResult.next();
                                Value value2 = bindingSet.getValue(eqr.resultVar.getName());
                                if (first) {
                                    first = false;
                                } else {
                                    sb.append(";");
                                }
                                sb.append(renderInnerValueToText(value2, database, connection));
                            }
                        } finally {
                            queryResult.close();
                        }
                    } else {
                        sb.append(renderInnerValueToText(value, database, connection));
                    }
                } catch (Exception e) {
                    _logger.error("", e);
                }
            }
        }
        o.put("value", o, sb.toString());
        arrayBuilder.add(o);
    }
    result.put("subcontentAttributes", result, arrayBuilder.toArray());
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) BindingSet(org.openrdf.query.BindingSet) DefaultScriptableObject(edu.mit.simile.backstage.util.DefaultScriptableObject) StringFragment(edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.StringFragment) ExpressionQueryResult(edu.mit.simile.backstage.model.data.ExpressionQueryResult) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult) Fragment(edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.Fragment) StringFragment(edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.StringFragment) ExpressionFragment(edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.ExpressionFragment)

Example 2 with ScriptableArrayBuilder

use of edu.mit.simile.backstage.util.ScriptableArrayBuilder in project backstage by zepheira.

the class BackChannel method getComponentStateArray.

public NativeArray getComponentStateArray() {
    ScriptableArrayBuilder states = new ScriptableArrayBuilder();
    for (Component c : _componentsChangingState) {
        Scriptable state = c.getComponentState();
        if (state != null) {
            state.put("id", state, c.getID());
            states.add(state);
        }
    }
    return states.toArray();
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) Component(edu.mit.simile.backstage.model.ui.Component) Scriptable(org.mozilla.javascript.Scriptable)

Example 3 with ScriptableArrayBuilder

use of edu.mit.simile.backstage.util.ScriptableArrayBuilder in project backstage by zepheira.

the class DomLensNode method generateContentWithInnerTemplates.

protected void generateContentWithInnerTemplates(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
    ScriptableArrayBuilder arrayBuilder = new ScriptableArrayBuilder();
    try {
        ExpressionQueryResult eqr = _contentExpression.computeOutputOnValue(value, database, connection);
        if (eqr != null) {
            TupleQueryResult queryResult = eqr.tupleQuery.evaluate();
            try {
                while (queryResult.hasNext()) {
                    BindingSet bindingSet = queryResult.next();
                    Value value2 = bindingSet.getValue(eqr.resultVar.getName());
                    arrayBuilder.add(generateInnerContentWithInnerTemplates(value2, database, connection));
                }
            } finally {
                queryResult.close();
            }
        } else {
            arrayBuilder.add(generateInnerContentWithInnerTemplates(value, database, connection));
        }
    } catch (Exception e) {
        _logger.error("", e);
    }
    result.put("content", result, arrayBuilder.toArray());
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) BindingSet(org.openrdf.query.BindingSet) ExpressionQueryResult(edu.mit.simile.backstage.model.data.ExpressionQueryResult) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 4 with ScriptableArrayBuilder

use of edu.mit.simile.backstage.util.ScriptableArrayBuilder in project backstage by zepheira.

the class ListFacet method getComponentState.

@SuppressWarnings("unchecked")
@Override
public Scriptable getComponentState() {
    DefaultScriptableObject result = new DefaultScriptableObject();
    ScriptableArrayBuilder facetChoicesWithSelection = new ScriptableArrayBuilder();
    int selectionCount = 0;
    List<FacetChoice> facetChoices = (List<FacetChoice>) _context.getDatabase().cacheAndRun(getCacheableQueryKey(), new ListFacetCacheableQuery());
    if (facetChoices != null) {
        for (FacetChoice fc : facetChoices) {
            DefaultScriptableObject valueO = new DefaultScriptableObject();
            boolean selected = _selection.contains(fc._valueString);
            valueO.put("value", valueO, fc._valueString);
            valueO.put("count", valueO, fc._count);
            valueO.put("label", valueO, fc._label);
            valueO.put("selected", valueO, selected);
            facetChoicesWithSelection.add(valueO);
            if (selected) {
                selectionCount++;
            }
        }
    }
    result.put("values", result, facetChoicesWithSelection.toArray());
    result.put("selectionCount", result, selectionCount);
    return result;
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) DefaultScriptableObject(edu.mit.simile.backstage.util.DefaultScriptableObject) ArrayList(java.util.ArrayList) ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) List(java.util.List)

Example 5 with ScriptableArrayBuilder

use of edu.mit.simile.backstage.util.ScriptableArrayBuilder in project backstage by zepheira.

the class DefaultLens method render.

@Override
public void render(URI item, Scriptable result, Database database, SailRepositoryConnection connection) {
    result.put("label", result, database.getItemLabel(database.getItemId(item)));
    try {
        RepositoryResult<Statement> r = connection.getStatements(item, null, null, true, SailUtilities.noContext);
        Map<URI, Set<Value>> propertyToValues = new HashMap<URI, Set<Value>>();
        try {
            while (r.hasNext()) {
                Statement s = r.next();
                URI predicate = s.getPredicate();
                Set<Value> values = propertyToValues.get(predicate);
                if (values == null) {
                    values = new HashSet<Value>();
                    propertyToValues.put(predicate, values);
                }
                values.add(s.getObject());
            }
        } finally {
            r.close();
        }
        DefaultScriptableObject propertiesO = new DefaultScriptableObject();
        for (URI property : propertyToValues.keySet()) {
            String propertyID = database.getPropertyId(property);
            PropertyRecord propertyRecord = database.getPropertyRecord(propertyID);
            DefaultScriptableObject o = new DefaultScriptableObject();
            o.put("propertyLabel", o, propertyRecord.label);
            o.put("valueType", o, propertyRecord.valueType);
            ScriptableArrayBuilder valueArrayBuilder = new ScriptableArrayBuilder();
            Set<Value> values = propertyToValues.get(property);
            if ("item".equals(propertyRecord.valueType)) {
                for (Value value : values) {
                    if (value instanceof URI) {
                        URI itemURI = (URI) value;
                        String itemID = database.getItemId(itemURI);
                        DefaultScriptableObject v = new DefaultScriptableObject();
                        v.put("id", v, itemID);
                        v.put("label", v, database.getItemLabel(itemID));
                        valueArrayBuilder.add(v);
                    } else {
                        valueArrayBuilder.add(((Resource) value).stringValue());
                    }
                }
            } else {
                for (Value value : values) {
                    valueArrayBuilder.add(Utilities.valueToString(value));
                }
            }
            o.put("values", o, valueArrayBuilder.toArray());
            propertiesO.put(propertyID, propertiesO, o);
        }
        result.put("propertyValues", result, propertiesO);
    } catch (Exception e) {
        _logger.error("Failed to generate default lens for item " + item, e);
    }
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Statement(org.openrdf.model.Statement) URI(org.openrdf.model.URI) DefaultScriptableObject(edu.mit.simile.backstage.util.DefaultScriptableObject) PropertyRecord(edu.mit.simile.backstage.model.data.Database.PropertyRecord) Value(org.openrdf.model.Value)

Aggregations

ScriptableArrayBuilder (edu.mit.simile.backstage.util.ScriptableArrayBuilder)8 DefaultScriptableObject (edu.mit.simile.backstage.util.DefaultScriptableObject)6 Value (org.openrdf.model.Value)5 ExpressionQueryResult (edu.mit.simile.backstage.model.data.ExpressionQueryResult)4 BindingSet (org.openrdf.query.BindingSet)4 TupleQueryResult (org.openrdf.query.TupleQueryResult)4 PropertyRecord (edu.mit.simile.backstage.model.data.Database.PropertyRecord)1 Component (edu.mit.simile.backstage.model.ui.Component)1 ExpressionFragment (edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.ExpressionFragment)1 Fragment (edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.Fragment)1 StringFragment (edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.StringFragment)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Scriptable (org.mozilla.javascript.Scriptable)1 Statement (org.openrdf.model.Statement)1 URI (org.openrdf.model.URI)1 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)1