Search in sources :

Example 6 with FlattenedSolution

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

the class NGClientWebsocketSession method onOpen.

@SuppressWarnings("nls")
@Override
public void onOpen(final Map<String, List<String>> requestParams) {
    super.onOpen(requestParams);
    if (requestParams == null) {
        CurrentWindow.get().cancelSession("Solution name is required");
        return;
    }
    if (requestParams.containsKey("clienttype")) {
        clientType = Utils.getAsInteger(requestParams.get("clienttype").get(0), 1);
        if (clientType == 2)
            client.getRuntimeProperties().put("NG2", Boolean.TRUE);
        else
            client.getRuntimeProperties().remove("NG2");
    } else {
        clientType = 1;
        client.getRuntimeProperties().remove("NG2");
    }
    lastSentStyleSheets = null;
    final StartupArguments args = new StartupArguments(requestParams);
    final String solutionName = args.getSolutionName();
    if (Utils.stringIsEmpty(solutionName)) {
        CurrentWindow.get().cancelSession("Invalid solution name");
        return;
    }
    if (!client.isEventDispatchThread())
        J2DBGlobals.setServiceProvider(client);
    try {
        FlattenedSolution solution = client.getFlattenedSolution();
        if (solution != null) {
            // test for the main solution meta data else a login solution will constantly be closed even if it is for the right main solution.
            if (solution.getSolution() != null && !solutionName.equals(solution.getMainSolutionMetaData().getName())) {
                client.closeSolution(true, null);
            } else {
                if (solution.isMainSolutionLoaded() || solution.getSolution() != null && solution.getSolution().getSolutionType() == SolutionMetaData.LOGIN_SOLUTION) {
                    // this is needed for the situation when the solution is already loaded and the deeplink url was changed (different arg values for instance)
                    String method = args.getMethodName();
                    String firstArgument = args.getFirstArgument();
                    if (method != null) {
                        try {
                            client.getScriptEngine().getScopesScope().executeGlobalFunction(null, method, (args.toJSMap().isEmpty() ? null : new Object[] { firstArgument, args.toJSMap() }), false, false);
                        } catch (Exception e1) {
                            // $NON-NLS-1$
                            client.reportError(Messages.getString("servoy.formManager.error.ExecutingOpenSolutionMethod", new Object[] { method }), e1);
                        }
                    }
                }
                client.getRuntimeWindowManager().setCurrentWindowName(String.valueOf(CurrentWindow.get().getNr()));
                IWebFormController currentForm = client.getFormManager().getCurrentForm();
                if (currentForm != null) {
                    // we have to call setcontroller again so that switchForm is called and the form is loaded into the reloaded/new window.
                    startHandlingEvent();
                    try {
                        client.getClientFunctions().clear();
                        sendUIProperties();
                        client.getRuntimeWindowManager().getCurrentWindow().setController(currentForm);
                        sendSolutionCSSURL(solution.getSolution());
                    } finally {
                        stopHandlingEvent();
                    }
                    return;
                }
            }
        }
        getEventDispatcher().addEvent(new Runnable() {

            @Override
            public void run() {
                try {
                    sendUIProperties();
                    // the solution was not loaded or another was loaded, now create a main window and load the solution.
                    client.getRuntimeWindowManager().createMainWindow(CurrentWindow.get().getNr());
                    client.handleArguments(args.getFirstArgument() != null ? new String[] { args.getSolutionName(), args.getMethodName(), args.getFirstArgument() } : new String[] { args.getSolutionName(), args.getMethodName() }, args);
                    client.loadSolution(solutionName);
                    client.showInfoPanel();
                } catch (RepositoryException e) {
                    Debug.error("Failed to load the solution: " + solutionName, e);
                    sendInternalError(e);
                }
            }
        });
    } catch (Exception e) {
        Debug.error(e);
        sendInternalError(e);
    } finally {
        if (!client.isEventDispatchThread())
            J2DBGlobals.setServiceProvider(null);
    }
}
Also used : StartupArguments(com.servoy.j2db.scripting.StartupArguments) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 7 with FlattenedSolution

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

the class MediaPropertyType method wrap.

@Override
public MediaWrapper wrap(Object value, MediaWrapper previousValue, PropertyDescription propertyDescription, IWrappingContext dataConverterContext) {
    if (previousValue != null && Utils.equalObjects(value, previousValue.mediaUrl)) {
        return previousValue;
    }
    IServoyDataConverterContext servoyDataConverterContext = ((IContextProvider) dataConverterContext.getWebObject()).getDataConverterContext();
    FlattenedSolution flattenedSolution = servoyDataConverterContext.getSolution();
    INGApplication application = servoyDataConverterContext.getApplication();
    String url = getMediaUrl(value, flattenedSolution, application);
    if (url != null)
        return new MediaWrapper(value, url);
    if (value != null && !Utils.equalObjects(value, Integer.valueOf(0)))
        Debug.log("cannot convert media " + value + " using converter context " + servoyDataConverterContext);
    return null;
}
Also used : MediaWrapper(com.servoy.j2db.server.ngclient.property.types.MediaPropertyType.MediaWrapper) INGApplication(com.servoy.j2db.server.ngclient.INGApplication) IContextProvider(com.servoy.j2db.server.ngclient.IContextProvider) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IServoyDataConverterContext(com.servoy.j2db.server.ngclient.IServoyDataConverterContext)

Example 8 with FlattenedSolution

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

the class MediaPropertyType method getMediaUrl.

public static String getMediaUrl(Object value, FlattenedSolution flattenedSolution, INGApplication application) {
    String url = null;
    Media media = null;
    if (value instanceof CharSequence) {
        value = ((CharSequence) value).toString();
    }
    if (value instanceof Integer) {
        if (((Integer) value).intValue() == 0) {
            // 0 means no media
            return null;
        }
        media = flattenedSolution.getMedia(((Integer) value).intValue());
    } else if (value instanceof String && ((String) value).toLowerCase().startsWith(MediaURLStreamHandler.MEDIA_URL_DEF)) {
        media = flattenedSolution.getMedia(((String) value).substring(MediaURLStreamHandler.MEDIA_URL_DEF.length()));
    } else {
        if (value != null) {
            media = flattenedSolution.getMedia(value.toString());
            if (media == null) {
                media = (Media) flattenedSolution.searchPersist(value.toString());
            }
        }
    }
    if (media != null) {
        url = "resources/" + MediaResourcesServlet.FLATTENED_SOLUTION_ACCESS + "/" + media.getRootObject().getName() + "/" + media.getName();
        Dimension imageSize = ImageLoader.getSize(media.getMediaData());
        boolean paramsAdded = false;
        if (imageSize != null) {
            paramsAdded = true;
            url += "?imageWidth=" + imageSize.width + "&imageHeight=" + imageSize.height;
        }
        if (application != null) {
            Solution sc = flattenedSolution.getSolutionCopy(false);
            if (sc != null && sc.getMedia(media.getName()) != null) {
                if (paramsAdded)
                    url += "&";
                else
                    url += "?";
                url += "clientnr=" + application.getWebsocketSession().getSessionKey().getClientnr() + "&lm:" + sc.getLastModifiedTime();
            }
        }
    } else if (value instanceof String && ((String) value).startsWith("resources/" + MediaResourcesServlet.FLATTENED_SOLUTION_ACCESS)) {
        url = (String) value;
    } else if (value != null) {
        Debug.warn("Invalid media value received: " + value + ", cannot resolve it.");
    }
    return url;
}
Also used : JSMedia(com.servoy.j2db.scripting.solutionmodel.JSMedia) Media(com.servoy.j2db.persistence.Media) Dimension(java.awt.Dimension) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution)

Example 9 with FlattenedSolution

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

the class ServoyFunctionPropertyType method toJSON.

public JSONWriter toJSON(JSONWriter writer, String key, Object object, PropertyDescription pd, DataConversion clientConversion, FlattenedSolution fs, FormElement fe, WebFormComponent formComponent) throws JSONException {
    Map<String, Object> map = new HashMap<>();
    if (object != null && fs != null) {
        // $NON-NLS-1$
        String[] components = object.toString().split("-");
        if (components.length == 5) {
            String scriptString = null;
            // this is a uuid
            ScriptMethod sm = fs.getScriptMethod(object.toString());
            if (sm != null) {
                ISupportChilds parent = sm.getParent();
                if (parent instanceof Solution) {
                    scriptString = "scopes." + sm.getScopeName() + "." + sm.getName();
                } else if (parent instanceof Form) {
                    if (formComponent != null) {
                        // use the real, runtime form
                        scriptString = formComponent.getDataAdapterList().getForm().getForm().getName() + "." + sm.getName();
                    } else {
                        scriptString = ((Form) parent).getName() + "." + sm.getName();
                    }
                } else if (parent instanceof TableNode && fe != null) {
                    scriptString = "entity." + fe.getForm().getName() + "." + sm.getName();
                }
                object = scriptString;
            } else
                Debug.log("can't find a scriptmethod for: " + object);
        }
    }
    try {
        if (object instanceof String) {
            addScriptToMap((String) object, map);
        } else if (object instanceof NativeFunction) {
            nativeFunctionToJSON((NativeFunction) object, map);
        } else if (object instanceof FunctionWrapper && ((FunctionWrapper) object).getWrappedFunction() instanceof NativeFunction) {
            nativeFunctionToJSON((NativeFunction) ((FunctionWrapper) object).getWrappedFunction(), map);
        } else if (object instanceof Map) {
            map = new HashMap<String, Object>((Map<String, Object>) object);
            if (map.get("script") instanceof String)
                addScriptToMap((String) map.get("script"), map);
        }
    } catch (Exception ex) {
        Debug.error(ex);
    }
    return JSONUtils.toBrowserJSONFullValue(writer, key, map.size() == 0 ? null : map, null, clientConversion, null);
}
Also used : FunctionWrapper(com.servoy.j2db.scripting.FunctionWrapper) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) HashMap(java.util.HashMap) Form(com.servoy.j2db.persistence.Form) JSForm(com.servoy.j2db.scripting.solutionmodel.JSForm) NativeFunction(org.mozilla.javascript.NativeFunction) JSONException(org.json.JSONException) TableNode(com.servoy.j2db.persistence.TableNode) JSONObject(org.json.JSONObject) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) HashMap(java.util.HashMap) Map(java.util.Map) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution)

Example 10 with FlattenedSolution

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

the class FormComponentPropertyType method toTemplateJSONValue.

@Override
public JSONWriter toTemplateJSONValue(JSONWriter writer, String key, Object formElementValue, PropertyDescription pd, DataConversion clientConversion, FormElementContext formElementContext) throws JSONException {
    if (formElementValue == null)
        return writer;
    FlattenedSolution fs = formElementContext.getFlattenedSolution();
    Form form = getForm(formElementValue, fs);
    if (form != null) {
        if (pd.getConfig() instanceof ComponentTypeConfig && ((ComponentTypeConfig) pd.getConfig()).forFoundset != null) {
            clientConversion.convert(FormComponentPropertyType.TYPE_NAME);
        }
        // else this info below seems to be needed client-side even for non-foundset linked form components; but it's just some dumb simple JSON, not a client side type
        JSONUtils.addKeyIfPresent(writer, key);
        // we output here a uuid that is a uuid that must be used to get the compiled template from the $formcomponentCache
        String uuid = FormElementHelper.INSTANCE.getFormComponentCache(formElementContext.getFormElement(), pd, (JSONObject) formElementValue, form, fs).getHtmlTemplateUUIDForAngular();
        writer.object();
        writer.key("uuid");
        writer.value(uuid);
        writer.key("formHeight");
        writer.value(form.getSize().height);
        writer.key("formWidth");
        writer.value(form.getSize().width);
        writer.key("absoluteLayout");
        writer.value(!form.isResponsiveLayout());
        writer.endObject();
    }
    return writer;
}
Also used : JSONObject(org.json.JSONObject) Form(com.servoy.j2db.persistence.Form) JSForm(com.servoy.j2db.scripting.solutionmodel.JSForm) FlattenedSolution(com.servoy.j2db.FlattenedSolution) ComponentTypeConfig(com.servoy.j2db.server.ngclient.property.ComponentTypeConfig)

Aggregations

FlattenedSolution (com.servoy.j2db.FlattenedSolution)79 JSFunction (org.mozilla.javascript.annotations.JSFunction)27 Form (com.servoy.j2db.persistence.Form)20 RepositoryException (com.servoy.j2db.persistence.RepositoryException)17 ArrayList (java.util.ArrayList)17 Relation (com.servoy.j2db.persistence.Relation)15 Media (com.servoy.j2db.persistence.Media)10 Solution (com.servoy.j2db.persistence.Solution)10 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)9 IBaseSMForm (com.servoy.base.solutionmodel.IBaseSMForm)7 AbstractActiveSolutionHandler (com.servoy.j2db.AbstractActiveSolutionHandler)7 ISMForm (com.servoy.j2db.solutionmodel.ISMForm)7 FormController (com.servoy.j2db.FormController)6 TableNode (com.servoy.j2db.persistence.TableNode)6 ValueList (com.servoy.j2db.persistence.ValueList)6 JSForm (com.servoy.j2db.scripting.solutionmodel.JSForm)6 IApplicationServer (com.servoy.j2db.server.shared.IApplicationServer)6 ITable (com.servoy.j2db.persistence.ITable)5 QueryTable (com.servoy.j2db.query.QueryTable)5 SafeArrayList (com.servoy.j2db.util.SafeArrayList)5