Search in sources :

Example 1 with ScriptNameValidator

use of com.servoy.j2db.persistence.ScriptNameValidator in project servoy-client by Servoy.

the class FlattenedSolution method getValuelistSortRelation.

/**
 * Get the internal relation that can be used to sort on this value list using the display values.
 * @param valueList
 * @param callingTable
 * @param dataProviderID
 * @param foundSetManager
 * @return
 * @throws RepositoryException
 */
public Relation getValuelistSortRelation(ValueList valueList, Table callingTable, String dataProviderID, IFoundSetManagerInternal foundSetManager) throws RepositoryException {
    if (callingTable == null || valueList == null) {
        return null;
    }
    String destDataSource;
    Relation[] relationSequence;
    String relationPrefix;
    switch(valueList.getDatabaseValuesType()) {
        case IValueListConstants.TABLE_VALUES:
            // create an internal relation
            relationSequence = null;
            // $NON-NLS-1$
            relationPrefix = "";
            destDataSource = valueList.getDataSource();
            break;
        case IValueListConstants.RELATED_VALUES:
            // replace the last relation in the sequence with an internal relation
            relationSequence = getRelationSequence(valueList.getRelationName());
            if (relationSequence == null) {
                return null;
            }
            if (relationSequence.length > 1) {
                for (Relation r : relationSequence) {
                    if (r.getJoinType() != INNER_JOIN) {
                        // outer join on the intermediate tables causes extra results that influence the sorting result
                        return null;
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            for (Relation r : relationSequence) {
                sb.append('-').append(r.getName());
            }
            relationPrefix = sb.toString();
            destDataSource = relationSequence[relationSequence.length - 1].getForeignDataSource();
            break;
        default:
            return null;
    }
    if (destDataSource == null || !DataSourceUtils.isSameServer(callingTable.getDataSource(), destDataSource)) {
        // do not create a cross-server relation
        return null;
    }
    Table destTable = (Table) foundSetManager.getTable(destDataSource);
    if (destTable == null) {
        return null;
    }
    String relationName = // $NON-NLS-1$
    Relation.INTERNAL_PREFIX + "VL-" + callingTable.getDataSource() + '-' + dataProviderID + relationPrefix + '-' + valueList.getName() + '-';
    synchronized (this) {
        Column callingColumn = callingTable.getColumn(dataProviderID);
        if (callingColumn == null) {
            return null;
        }
        Relation relation = getRelation(relationName);
        if (relation == null) {
            // create in internal relation
            String dp;
            int returnValues = valueList.getReturnDataProviders();
            if ((returnValues & 1) != 0) {
                dp = valueList.getDataProviderID1();
            } else if ((returnValues & 2) != 0) {
                dp = valueList.getDataProviderID2();
            } else if ((returnValues & 4) != 0) {
                dp = valueList.getDataProviderID3();
            } else {
                return null;
            }
            Column destColumn = destTable.getColumn(dp);
            if (destColumn == null) {
                return null;
            }
            // create internal value list relation
            QueryTable callingQTable = new QueryTable(callingTable.getSQLName(), callingTable.getDataSource(), callingTable.getCatalog(), callingTable.getSchema());
            QueryTable destQTable = new QueryTable(destTable.getSQLName(), destTable.getDataSource(), destTable.getCatalog(), destTable.getSchema());
            List<ISQLTableJoin> joins = new ArrayList<ISQLTableJoin>();
            ISQLTableJoin lastJoin = null;
            if (relationSequence == null) {
                // table values
                joins.add(lastJoin = new QueryJoin(relationName, callingQTable, destQTable, new AndCondition(), LEFT_OUTER_JOIN, false));
                if (// apply name as filter on column valuelist_name
                valueList.getUseTableFilter()) {
                    lastJoin.getCondition().addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, new QueryColumn(destQTable, DBValueList.NAME_COLUMN), valueList.getName()));
                }
            } else {
                // related values
                QueryTable primaryQTable = callingQTable;
                for (int i = 0; i < relationSequence.length; i++) {
                    Relation r = relationSequence[i];
                    QueryTable foreignQTable;
                    if (i == relationSequence.length - 1) {
                        // last one
                        foreignQTable = destQTable;
                    } else {
                        ITable relForeignTable = getTable(r.getForeignDataSource());
                        if (relForeignTable == null) {
                            return null;
                        }
                        foreignQTable = new QueryTable(relForeignTable.getSQLName(), relForeignTable.getDataSource(), relForeignTable.getCatalog(), relForeignTable.getSchema());
                    }
                    lastJoin = SQLGenerator.createJoin(this, r, primaryQTable, foreignQTable, false, new IGlobalValueEntry() {

                        public Object setDataProviderValue(String dpid, Object value) {
                            return null;
                        }

                        public Object getDataProviderValue(String dpid) {
                            // A value will be added when the relation is used, see SQLGenerator.createJoin
                            return new Placeholder(new ObjectPlaceholderKey<int[]>(null, dpid));
                        }

                        public boolean containsDataProvider(String dpid) {
                            return false;
                        }
                    });
                    joins.add(lastJoin);
                    primaryQTable = foreignQTable;
                }
            }
            // add condition for return dp id
            lastJoin.getCondition().addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, destColumn.queryColumn(destQTable), callingColumn.queryColumn(callingQTable)));
            relation = getSolutionCopy().createNewRelation(new ScriptNameValidator(this), relationName, callingTable.getDataSource(), destDataSource, LEFT_OUTER_JOIN);
            ISQLTableJoin join;
            if (joins.size() == 1) {
                join = lastJoin;
            } else {
                // combine joins
                join = new QueryCompositeJoin(relationName, joins);
            }
            relation.setRuntimeProperty(Relation.RELATION_JOIN, join);
        }
        return relation;
    }
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) Table(com.servoy.j2db.persistence.Table) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) ArrayList(java.util.ArrayList) QueryJoin(com.servoy.j2db.query.QueryJoin) QueryTable(com.servoy.j2db.query.QueryTable) AndCondition(com.servoy.j2db.query.AndCondition) Relation(com.servoy.j2db.persistence.Relation) IGlobalValueEntry(com.servoy.j2db.dataprocessing.IGlobalValueEntry) QueryCompositeJoin(com.servoy.j2db.query.QueryCompositeJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) CompareCondition(com.servoy.j2db.query.CompareCondition) ITable(com.servoy.j2db.persistence.ITable) JSONObject(org.json.JSONObject) IRootObject(com.servoy.j2db.persistence.IRootObject) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator)

Example 2 with ScriptNameValidator

use of com.servoy.j2db.persistence.ScriptNameValidator in project servoy-client by Servoy.

the class FlattenedSolution method clonePersist.

@SuppressWarnings({ "unchecked", "nls" })
public <T extends AbstractBase> T clonePersist(T persist, String newName, AbstractBase newParent) {
    T clone = (T) persist.clonePersist(persist.getParent() == newParent ? null : newParent);
    final Map<Object, Object> updatedElementIds = AbstractPersistFactory.resetUUIDSRecursively(clone, getPersistFactory(), false);
    if (persist.getParent() == newParent) {
        newParent.addChild(clone);
    }
    // make sure that this persist is not seen as a copy a real persist/form
    clone.setRuntimeProperty(CLONE_PROPERTY, null);
    if (clone instanceof ISupportUpdateableName) {
        try {
            ((ISupportUpdateableName) clone).updateName(new ScriptNameValidator(this), newName);
        } catch (Exception e) {
            if (newParent != null) {
                newParent.removeChild(clone);
            }
            throw new RuntimeException("name '" + newName + "' invalid for the clone of " + ((ISupportName) persist).getName() + ", error: " + e.getMessage());
        }
    }
    if (clone instanceof ISupportChilds) {
        clone.acceptVisitor(new IPersistVisitor() {

            public Object visit(IPersist o) {
                if (o instanceof AbstractBase) {
                    Map<String, Object> propertiesMap = ((AbstractBase) o).getPropertiesMap();
                    for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
                        Object elementId = updatedElementIds.get(entry.getValue());
                        if (elementId instanceof Integer) {
                            Element element = StaticContentSpecLoader.getContentSpec().getPropertyForObjectTypeByName(o.getTypeID(), entry.getKey());
                            if (element.getTypeID() == IRepository.ELEMENTS)
                                ((AbstractBase) o).setProperty(entry.getKey(), elementId);
                        } else if (entry.getValue() instanceof JSONObject) {
                            updateElementReferences((JSONObject) entry.getValue(), updatedElementIds);
                        }
                    }
                }
                return null;
            }
        });
    }
    if (securityAccess != null) {
        ConcurrentMap<Object, Integer> securityValues = securityAccess.getLeft();
        for (Object elementUUID : new HashSet(securityValues.keySet())) {
            if (updatedElementIds.containsKey(elementUUID.toString())) {
                UUID uuid = Utils.getAsUUID(updatedElementIds.get(elementUUID.toString()), false);
                if (uuid != null) {
                    securityValues.put(uuid, securityValues.get(elementUUID));
                }
            }
        }
    }
    flush(persist);
    getIndex().reload();
    return clone;
}
Also used : ISupportUpdateableName(com.servoy.j2db.persistence.ISupportUpdateableName) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) IScriptElement(com.servoy.j2db.persistence.IScriptElement) Element(com.servoy.j2db.persistence.ContentSpec.Element) AbstractBase(com.servoy.j2db.persistence.AbstractBase) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IGlobalValueEntry(com.servoy.j2db.dataprocessing.IGlobalValueEntry) JSONObject(org.json.JSONObject) IPersist(com.servoy.j2db.persistence.IPersist) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) JSONObject(org.json.JSONObject) IRootObject(com.servoy.j2db.persistence.IRootObject) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) UUID(com.servoy.j2db.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet)

Example 3 with ScriptNameValidator

use of com.servoy.j2db.persistence.ScriptNameValidator in project servoy-client by Servoy.

the class JSForm method newVariable.

/**
 * Creates a new form JSVariable - based on the name of the variable object , the  type  and it's default value , uses the SolutionModel JSVariable constants.
 *
 * This method does not require the form to be destroyed and recreated. Use this method if you want to change the form's model without destroying the runtime form</b>
 *
 * @sample
 * var form = solutionModel.newForm('newForm1', myDatasource, null, true, 800, 600);
 * var variable = form.newVariable('myVar', JSVariable.TEXT , "'This is a default value (with triple quotes)!'");
 * //or variable = form.newVariable('myVar', JSVariable.TEXT)
 * //variable.defaultValue = "'This is a default value (with triple quotes)!'" // setting the default value after the variable is created requires form recreation
 * //variable.defaultValue = "{a:'First letter',b:'Second letter'}"
 * var field = form.newField(variable, JSField.TEXT_FIELD, 100, 100, 200, 200);
 * forms['newForm1'].controller.show();
 *
 * @param name the specified name of the variable
 *
 * @param type the specified type of the variable (see Solution Model -> JSVariable node constants)
 *
 * @param defaultValue the default value as a javascript expression string
 *
 * @return a JSVariable object
 */
@JSFunction
public JSVariable newVariable(String name, int type, String defaultValue) {
    checkModification();
    try {
        ScriptVariable variable = getForm().createNewScriptVariable(new ScriptNameValidator(application.getFlattenedSolution()), name, type);
        variable.setDefaultValue(defaultValue);
        addVariableToScopes(variable);
        return new JSVariable(application, this, variable, true);
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 4 with ScriptNameValidator

use of com.servoy.j2db.persistence.ScriptNameValidator in project servoy-client by Servoy.

the class JSSolutionModel method newGlobalVariable.

/**
 * Creates a new global variable with the specified name and number type.
 *
 * NOTE: The global variable number type is based on the value assigned from the SolutionModel-JSVariable node; for example: JSVariable.INTEGER.
 *
 * @sample
 * var myGlobalVariable = solutionModel.newGlobalVariable('globals', 'newGlobalVariable', JSVariable.INTEGER);
 * myGlobalVariable.defaultValue = 12;
 * //myGlobalVariable.defaultValue = "{a:'First letter',b:'Second letter'}" // an js object, type must be media.
 * //myGlobalVariable.defaultValue = '"some text"'; // Use two pairs of quotes if you want to assign a String as default value.
 * @param scopeName the scope in which the variable is created
 * @param name the specified name for the global variable
 *
 * @param type the specified number type for the global variable
 *
 * @return a JSVariable object
 */
@JSFunction
public JSVariable newGlobalVariable(String scopeName, String name, int type) {
    FlattenedSolution fs = application.getFlattenedSolution();
    try {
        String scope = scopeName == null ? ScriptVariable.GLOBAL_SCOPE : scopeName;
        ScriptVariable variable = fs.getSolutionCopy().createNewScriptVariable(new ScriptNameValidator(application.getFlattenedSolution()), scope, name, type);
        application.getScriptEngine().getScopesScope().getGlobalScope(scope).put(variable);
        return new JSVariable(application, variable, true);
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 5 with ScriptNameValidator

use of com.servoy.j2db.persistence.ScriptNameValidator in project servoy-client by Servoy.

the class JSSolutionModel method newGlobalMethod.

/**
 * Creates a new global method with the specified code in a scope.
 *
 * @sample
 * var method = solutionModel.newGlobalMethod('globals', 'function myglobalmethod(){foundset.newRecord()}')
 *
 * @param scopeName the scope in which the method is created
 * @param code the specified code for the global method
 *
 * @return a JSMethod object
 */
@JSFunction
public JSMethod newGlobalMethod(String scopeName, String code) {
    FlattenedSolution fs = application.getFlattenedSolution();
    String name = JSMethod.parseName(code);
    try {
        String scope = scopeName == null ? ScriptVariable.GLOBAL_SCOPE : scopeName;
        ScriptMethod method = fs.getSolutionCopy().createNewGlobalScriptMethod(new ScriptNameValidator(application.getFlattenedSolution()), scope, name);
        method.setDeclaration(code);
        application.getScriptEngine().getScopesScope().getGlobalScope(scope).put(method, method);
        JSMethod jsMethod = new JSMethod(method, application, true);
        return jsMethod;
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

ScriptNameValidator (com.servoy.j2db.persistence.ScriptNameValidator)12 RepositoryException (com.servoy.j2db.persistence.RepositoryException)11 JSFunction (org.mozilla.javascript.annotations.JSFunction)7 FlattenedSolution (com.servoy.j2db.FlattenedSolution)5 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)3 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)3 JSSetter (org.mozilla.javascript.annotations.JSSetter)3 IGlobalValueEntry (com.servoy.j2db.dataprocessing.IGlobalValueEntry)2 IRootObject (com.servoy.j2db.persistence.IRootObject)2 ScriptVariable (com.servoy.j2db.persistence.ScriptVariable)2 TableNode (com.servoy.j2db.persistence.TableNode)2 TableScope (com.servoy.j2db.scripting.TableScope)2 JSONObject (org.json.JSONObject)2 AbstractBase (com.servoy.j2db.persistence.AbstractBase)1 Column (com.servoy.j2db.persistence.Column)1 Element (com.servoy.j2db.persistence.ContentSpec.Element)1 IColumn (com.servoy.j2db.persistence.IColumn)1 IPersist (com.servoy.j2db.persistence.IPersist)1 IPersistVisitor (com.servoy.j2db.persistence.IPersistVisitor)1 IScriptElement (com.servoy.j2db.persistence.IScriptElement)1