Search in sources :

Example 1 with ClientState

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

the class NGFormManager method makeSolutionSettings.

public void makeSolutionSettings(Solution s) {
    Solution solution = s;
    Iterator<Form> e = application.getFlattenedSolution().getForms(true);
    // add all forms first, they may be referred to in the login form
    Form first = application.getFlattenedSolution().getForm(solution.getFirstFormID());
    boolean firstFormCanBeInstantiated = application.getFlattenedSolution().formCanBeInstantiated(first);
    if (!firstFormCanBeInstantiated) {
        Solution[] modules = application.getFlattenedSolution().getModules();
        if (modules != null) {
            for (Solution module : modules) {
                if (module.getFirstFormID() > 0) {
                    first = application.getFlattenedSolution().getForm(module.getFirstFormID());
                    firstFormCanBeInstantiated = application.getFlattenedSolution().formCanBeInstantiated(first);
                    if (firstFormCanBeInstantiated)
                        break;
                }
            }
        }
    }
    while (e.hasNext()) {
        Form form = e.next();
        if (application.getFlattenedSolution().formCanBeInstantiated(form)) {
            if (!firstFormCanBeInstantiated)
                first = form;
            firstFormCanBeInstantiated = true;
        }
        // add anyway, the form may be used in scripting
        addForm(form, form.equals(first));
    }
    if (firstFormCanBeInstantiated) {
        // start in browse mode
        application.getModeManager().setMode(IModeManager.EDIT_MODE);
    }
    boolean showLoginForm = (solution.getLoginFormID() > 0 && solution.getMustAuthenticate() && application.getUserUID() == null);
    if (application.getUserUID() == null) {
        ScriptMethod onBeforeLogin = application.getFlattenedSolution().getScriptMethod(solution.getOnBeforeLoginMethodID());
        if (onBeforeLogin != null) {
            try {
                Object[] paramArray = null;
                Object[] clientArray = ((ClientState) application).getPreferedSolutionMethodArguments();
                if (clientArray != null && clientArray.length > 1) {
                    paramArray = new Object[] { clientArray[1] };
                }
                application.getScriptEngine().getScopesScope().executeGlobalFunction(onBeforeLogin.getScopeName(), onBeforeLogin.getName(), Utils.arrayMerge(paramArray, Utils.parseJSExpressions(solution.getFlattenedMethodArguments("onBeforeLoginMethodID"))), false, false);
            } catch (Exception e1) {
                application.reportError(// $NON-NLS-1$
                Messages.getString("servoy.formManager.error.ExecutingOpenSolutionMethod", new Object[] { onBeforeLogin.getName() }), e1);
            }
        }
    }
    if (solution.getLoginFormID() > 0 && solution.getMustAuthenticate() && application.getUserUID() == null) {
        Form login = application.getFlattenedSolution().getForm(solution.getLoginFormID());
        if (application.getFlattenedSolution().formCanBeInstantiated(login) && loginForm == null) {
            // must set the login form early so its even correct if onload of login form is called
            loginForm = login;
            showFormInMainPanel(login.getName());
            // stop and recall this method from security.login(...)!
            return;
        }
    } else if (showLoginForm) {
        // there was a login in onBeforeLogin, so only second call to makeSolutionSettings should go further
        return;
    }
    IBasicMainContainer currentContainer = getCurrentContainer();
    if (solution.getLoginFormID() > 0 && solution.getMustAuthenticate() && application.getUserUID() != null && loginForm != null) {
        if (currentContainer.getController() != null && loginForm.getName().equals(currentContainer.getController().getForm().getName())) {
            hideFormIfVisible(currentContainer.getController());
            currentContainer.setController(null);
        }
        // clear and continue
        loginForm = null;
    }
    ScriptMethod sm = application.getFlattenedSolution().getScriptMethod(solution.getOnOpenMethodID());
    Object[] solutionOpenMethodArgs = null;
    String preferedSolutionMethodName = ((ClientState) application).getPreferedSolutionMethodNameToCall();
    if (preferedSolutionMethodName == null && ((ClientState) application).getPreferedSolutionMethodArguments() != null) {
        solutionOpenMethodArgs = ((ClientState) application).getPreferedSolutionMethodArguments();
    }
    if (sm != null) {
        try {
            application.getScriptEngine().getScopesScope().executeGlobalFunction(sm.getScopeName(), sm.getName(), Utils.arrayMerge(solutionOpenMethodArgs, Utils.parseJSExpressions(solution.getFlattenedMethodArguments("onOpenMethodID"))), false, false);
            if (application.getSolution() == null)
                return;
        } catch (Exception e1) {
            // $NON-NLS-1$
            application.reportError(Messages.getString("servoy.formManager.error.ExecutingOpenSolutionMethod", new Object[] { sm.getName() }), e1);
        }
    }
    if (first != null && getCurrentForm() == null) {
        // we only set if the solution startup did not yet show a form already
        showFormInMainPanel(first.getName());
    }
    if (preferedSolutionMethodName != null && (application.getFlattenedSolution().isMainSolutionLoaded() || solution.getSolutionType() == SolutionMetaData.LOGIN_SOLUTION)) {
        try {
            Object[] args = ((ClientState) application).getPreferedSolutionMethodArguments();
            Pair<String, String> scope = ScopesUtils.getVariableScope(preferedSolutionMethodName);
            GlobalScope gs = application.getScriptEngine().getScopesScope().getGlobalScope(scope.getLeft());
            if (// make sure the function is found before resetting preferedSolutionMethodName
            gs != null && gs.get(scope.getRight()) instanceof Function) {
                // avoid stack overflows when an execute method URL is used to open the solution, and that method does call JSSecurity login
                ((ClientState) application).resetPreferedSolutionMethodNameToCall();
                Object result = application.getScriptEngine().getScopesScope().executeGlobalFunction(scope.getLeft(), scope.getRight(), args, false, false);
                if (application.getSolution().getSolutionType() == SolutionMetaData.AUTHENTICATOR) {
                    application.getRuntimeProperties().put(IServiceProvider.RT_OPEN_METHOD_RESULT, result);
                }
            } else if (application.getFlattenedSolution().isMainSolutionLoaded()) {
                Debug.error("Preferred method '" + preferedSolutionMethodName + "' not found in " + application.getFlattenedSolution() + ".");
                ((ClientState) application).resetPreferedSolutionMethodNameToCall();
            }
        } catch (Exception e1) {
            // $NON-NLS-1$
            application.reportError(// $NON-NLS-1$
            Messages.getString("servoy.formManager.error.ExecutingOpenSolutionMethod", new Object[] { preferedSolutionMethodName }), e1);
        }
    }
}
Also used : ClientState(com.servoy.j2db.ClientState) GlobalScope(com.servoy.j2db.scripting.GlobalScope) IBasicMainContainer(com.servoy.j2db.IBasicMainContainer) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) Function(org.mozilla.javascript.Function) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) Solution(com.servoy.j2db.persistence.Solution)

Example 2 with ClientState

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

the class FoundSetManager method addTableFilterParam.

public boolean addTableFilterParam(String filterName, String serverName, ITable table, TableFilterdefinition tableFilterdefinition) throws ServoyException {
    TableFilter filter = new TableFilter(filterName, serverName, table == null ? null : table.getName(), table == null ? null : table.getSQLName(), tableFilterdefinition);
    List<TableFilter> params = tableFilterParams.get(serverName);
    if (params == null) {
        tableFilterParams.put(serverName, params = new ArrayList<TableFilter>());
    }
    if (// do not add the same filter, will add same AND-condition anyway
    !filter.isContainedIn(params)) {
        params.add(filter);
        for (ITable affectedtable : getFilterUpdateAffectedTables(getDataSource(table), tableFilterdefinition)) {
            fireTableEvent(affectedtable);
        }
    }
    if (Messages.isI18NTable(serverName, table != null ? table.getName() : null, application)) {
        ((ClientState) application).refreshI18NMessages(false);
    }
    return true;
}
Also used : ClientState(com.servoy.j2db.ClientState) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ITable(com.servoy.j2db.persistence.ITable)

Example 3 with ClientState

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

the class JSDatabaseManager method js_switchServer.

/**
 * Switches a named server to another named server with the same datamodel (recommended to be used in an onOpen method for a solution).
 * return true if successful.
 * Note that this only works if source and destination server are of the same database type.
 *
 * @sample
 * //dynamically changes a server for the entire solution, destination database server must contain the same tables/columns!
 * //will fail if there is a lock, transaction , if repository_server is used or if destination server is invalid
 * //in the solution keep using the sourceName every where to reference the server!
 * var success = databaseManager.switchServer('crm', 'crm1')
 *
 * @param sourceName The name of the source database server connection
 * @param destinationName The name of the destination database server connection.
 *
 * @return true if the switch could be done.
 */
public boolean js_switchServer(String sourceName, String destinationName) throws ServoyException {
    checkAuthorized();
    if (IServer.REPOSITORY_SERVER.equals(sourceName))
        return false;
    if (IServer.REPOSITORY_SERVER.equals(destinationName))
        return false;
    if (((FoundSetManager) application.getFoundSetManager()).hasTransaction())
        return false;
    if (((FoundSetManager) application.getFoundSetManager()).hasLocks(null))
        return false;
    IServer server = null;
    try {
        server = application.getSolution().getServer(destinationName);
    } catch (Exception ex) {
        Debug.error(ex);
    }
    try {
        if (server == null || !server.isValid())
            return false;
    } catch (RemoteException e) {
        Debug.error(e);
        return false;
    }
    DataServerProxy pds = application.getDataServerProxy();
    if (pds == null) {
        // no dataserver access yet?
        return false;
    }
    pds.switchServer(sourceName, destinationName);
    // flush all
    ((FoundSetManager) application.getFoundSetManager()).flushCachedDatabaseData(null);
    // register existing used tables to server
    ((FoundSetManager) application.getFoundSetManager()).registerClientTables(sourceName);
    if (sourceName.equals(application.getSolution().getI18nServerName())) {
        ((ClientState) application).refreshI18NMessages(true);
    }
    return true;
}
Also used : ClientState(com.servoy.j2db.ClientState) IServer(com.servoy.j2db.persistence.IServer) RemoteException(java.rmi.RemoteException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) SQLException(java.sql.SQLException) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Aggregations

ClientState (com.servoy.j2db.ClientState)3 ApplicationException (com.servoy.j2db.ApplicationException)1 IBasicMainContainer (com.servoy.j2db.IBasicMainContainer)1 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)1 Form (com.servoy.j2db.persistence.Form)1 IServer (com.servoy.j2db.persistence.IServer)1 ITable (com.servoy.j2db.persistence.ITable)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1 Solution (com.servoy.j2db.persistence.Solution)1 GlobalScope (com.servoy.j2db.scripting.GlobalScope)1 ServoyException (com.servoy.j2db.util.ServoyException)1 RemoteException (java.rmi.RemoteException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Function (org.mozilla.javascript.Function)1 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)1