Search in sources :

Example 6 with ScriptNameValidator

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

the class JSDataSourceNode method newMethod.

/**
 * Creates a new foundset method with the specified code.
 *
 * @sample
 * var method = solutionModel.getDataSourceNode("db:/example_data/orders").newMethod("function doubleSize() { return 2*getSize(); }");
 *
 * application.output('Doubled orders for this customer: '+customers_to_orders.doubleSize())
 *
 * @param code the specified code for the foundset method
 *
 * @return a JSMethod object
 */
@JSFunction
public JSMethod newMethod(String code) {
    try {
        FlattenedSolution fs = application.getFlattenedSolution();
        TableNode tablenode = fs.getSolutionCopyTableNode(dataSource);
        if (tablenode == null)
            throw new RuntimeException("Couldnt create method for datasource: " + dataSource);
        String name = JSMethod.parseName(code);
        ScriptMethod method = tablenode.createNewFoundsetMethod(new ScriptNameValidator(fs), name, null);
        method.setDeclaration(code);
        ((FoundSetManager) application.getFoundSetManager()).reloadFoundsetMethod(dataSource, method);
        return new JSMethod(this, method, application, true);
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) TableNode(com.servoy.j2db.persistence.TableNode) 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)

Example 7 with ScriptNameValidator

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

the class JSDataSourceNode method newCalculation.

/**
 * Creates a new calculation for the given code and the type, if it builds on a column (name is a column name) then type will be ignored.
 *
 * @param code The code of the calculation, this must be a full function declaration.
 * @param type The type of the calculation, one of the JSVariable types.
 *
 * @sample
 * var calc = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER);
 * var calc2 = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation2() { return '20'; }");
 * var calc3 = solutionModel.getDataSourceNode("db:/example_data/employees").newCalculation("function myCalculation3() { return 'Hello World!'; }",	JSVariable.TEXT);
 *
 * var c = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation");
 * application.output("Name: " + c.getName() + ", Stored: " + c.isStored());
 *
 * var allCalcs = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculations();
 * for (var i = 0; i < allCalcs.length; i++) {
 * 	application.output(allCalcs[i]);
 * }
 */
@JSFunction
public JSCalculation newCalculation(String code, int type) {
    try {
        FlattenedSolution fs = application.getFlattenedSolution();
        TableNode tablenode = fs.getSolutionCopyTableNode(dataSource);
        String name = JSMethod.parseName(code);
        ScriptCalculation scriptCalculation = tablenode.createNewScriptCalculation(new ScriptNameValidator(fs), name, null, fs.getTable(dataSource));
        scriptCalculation.setDeclaration(code);
        scriptCalculation.setTypeAndCheck(type, application);
        TableScope tableScope = (TableScope) application.getScriptEngine().getTableScope(scriptCalculation.getTable());
        if (tableScope != null) {
            tableScope.put(scriptCalculation, scriptCalculation);
            ((FoundSetManager) application.getFoundSetManager()).flushSQLSheet(dataSource);
        }
        return new JSCalculation(this, scriptCalculation, application, true);
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) TableScope(com.servoy.j2db.scripting.TableScope) TableNode(com.servoy.j2db.persistence.TableNode) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 8 with ScriptNameValidator

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

the class JSSolutionModel method newMedia.

/**
 * Creates a new media object that can be assigned to a label or a button.
 *
 * @sample
 * var myMedia = solutionModel.newMedia('button01.gif',bytes)
 * //now set the imageMedia property of your label or button
 * //myButton.imageMedia = myMedia
 * // OR
 * //myLabel.imageMedia = myMedia
 *
 * @param name The name of the new media
 *
 * @param bytes The content
 *
 * @return a JSMedia object
 */
@JSFunction
public JSMedia newMedia(String name, byte[] bytes) {
    FlattenedSolution fs = application.getFlattenedSolution();
    try {
        Media media = fs.getSolutionCopy().createNewMedia(new ScriptNameValidator(fs), name);
        media.setPermMediaData(bytes);
        media.setMimeType(MimeTypes.getContentType(bytes));
        return new JSMedia(media, application.getFlattenedSolution(), true);
    } catch (RepositoryException e) {
        throw new RuntimeException("error createing new media with name " + name, e);
    }
}
Also used : Media(com.servoy.j2db.persistence.Media) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 9 with ScriptNameValidator

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

the class JSForm method newMethod.

/**
 * Creates a new form JSMethod - based on the specified code.
 *
 * @sample
 * var form = solutionModel.newForm('newForm1', myDatasource, null, true, 800, 600);
 * var method = form.newMethod('function aMethod(event){application.output("Hello world!");}');
 * var button = myListViewForm.newButton('Show message!',50,50,100,30,method);
 * forms['newForm1'].controller.show();
 *
 * @param code the specified code for the new method
 *
 * @return a new JSMethod object for this form
 */
@JSFunction
public JSMethod newMethod(String code) {
    checkModification();
    String name = JSMethod.parseName(code);
    try {
        ScriptMethod method = getForm().createNewScriptMethod(new ScriptNameValidator(application.getFlattenedSolution()), name);
        method.setDeclaration(code);
        // addMethodToScopes(method);
        refreshFromScopes();
        return new JSMethod(this, method, application, true);
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 10 with ScriptNameValidator

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

the class JSMethod method setCode.

@JSSetter
public void setCode(String content) {
    // if a default constant
    if (sm == null)
        return;
    checkModification();
    String name = parseName(content);
    if (!name.equals(sm.getName())) {
        try {
            sm.updateName(new ScriptNameValidator(application.getFlattenedSolution()), name);
        } catch (RepositoryException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new RuntimeException("Error updating the name from " + sm.getName() + " to " + name, e);
        }
    }
    sm.setDeclaration(content);
    if (parent instanceof JSDataSourceNode) {
        // foundset method
        application.getFoundSetManager().reloadFoundsetMethod(((JSDataSourceNode) parent).getSupportChild().getDataSource(), sm);
    } else if (parent == null && application.getScriptEngine().getScopesScope().has(sm.getScopeName(), null)) {
        // global method
        application.getScriptEngine().getScopesScope().getGlobalScope(sm.getScopeName()).put(sm, sm);
    }
}
Also used : RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSSetter(org.mozilla.javascript.annotations.JSSetter)

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