Search in sources :

Example 66 with FlattenedSolution

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

the class AngularIndexPageWriter method writeStartupJs.

/**
 * @param request
 * @param servletResponse
 * @param solutionName
 * @throws IOException
 * @throws ServletException
 * @throws JSONException
 */
public static void writeStartupJs(HttpServletRequest request, HttpServletResponse response, String solutionName) throws IOException, ServletException {
    if (request.getCharacterEncoding() == null)
        request.setCharacterEncoding("UTF8");
    String uri = request.getRequestURI();
    String clientnr = getClientNr(uri, request);
    Pair<FlattenedSolution, Boolean> pair = getFlattenedSolution(solutionName, clientnr, request, response);
    JSONObject json = new JSONObject();
    json.put("pathName", request.getRequestURI().replaceAll("[^/]*/[^/]*/startup.js$", "index.html"));
    json.put("querystring", HTTPUtils.generateQueryString(request.getParameterMap(), request.getCharacterEncoding()));
    // in case there is a forwarding proxy
    String ipaddr = request.getHeader("X-Forwarded-For");
    if (ipaddr == null) {
        ipaddr = request.getRemoteAddr();
    }
    json.put("ipaddr", ipaddr);
    // in case there is a forwarding proxy
    String remoteHost = request.getHeader("X-Forwarded-Host");
    if (remoteHost == null) {
        remoteHost = request.getRemoteHost();
    }
    json.put("hostaddr", remoteHost);
    if (pair.getLeft() != null) {
        Solution solution = pair.getLeft().getSolution();
        json.put("orientation", solution.getTextOrientation());
        JSONObject defaultTranslations = new JSONObject();
        defaultTranslations.put("servoy.ngclient.reconnecting", getSolutionDefaultMessage(solution, request.getLocale(), "servoy.ngclient.reconnecting"));
        json.put("defaultTranslations", defaultTranslations);
    }
    StringBuilder sb = new StringBuilder(256);
    sb.append("window.svyData=");
    sb.append(json.toString());
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/javascript");
    response.setContentLengthLong(sb.length());
    response.getWriter().write(sb.toString());
    if (pair.getRight().booleanValue())
        pair.getLeft().close(null);
}
Also used : JSONObject(org.json.JSONObject) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Utils.getAsBoolean(com.servoy.j2db.util.Utils.getAsBoolean) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution)

Example 67 with FlattenedSolution

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

the class ChildrenJSONGenerator method visit.

@SuppressWarnings("nls")
@Override
public Object visit(IPersist o) {
    if (o == skip)
        return IPersistVisitor.CONTINUE_TRAVERSAL;
    if (!isSecurityVisible(o))
        return IPersistVisitor.CONTINUE_TRAVERSAL;
    if (o instanceof IFormElement) {
        FormElement fe = null;
        if (part != null) {
            int startPos = form.getPartStartYPos(part.getID());
            int endPos = part.getHeight();
            Point location = CSSPositionUtils.getLocation((IFormElement) o);
            if (location != null && (startPos > location.y || endPos <= location.y)) {
                return IPersistVisitor.CONTINUE_TRAVERSAL;
            }
        }
        if (cache != null) {
            // this is for form component elements finding
            fe = cache.getFormElement((IFormElement) o, this.context.getSolution(), null, designer);
        }
        if (fe == null && formUI != null) {
            List<FormElement> cachedFormElements = formUI.getFormElements();
            for (FormElement cachedFE : cachedFormElements) {
                if (Utils.equalObjects(cachedFE.getPersistIfAvailable(), o)) {
                    fe = cachedFE;
                    break;
                }
            }
        }
        fe = fe != null ? fe : FormElementHelper.INSTANCE.getFormElement((IFormElement) o, this.context.getSolution(), null, designer);
        writer.object();
        writeFormElement(writer, o, form, fe, formUI, context, designer);
        if (o instanceof WebComponent) {
            WebObjectSpecification spec = fe.getWebComponentSpec();
            if (spec != null) {
                Collection<PropertyDescription> properties = spec.getProperties(FormComponentPropertyType.INSTANCE);
                if (properties.size() > 0) {
                    boolean isResponsive = false;
                    List<String> children = new ArrayList<>();
                    for (PropertyDescription pd : properties) {
                        Object propertyValue = fe.getPropertyValue(pd.getName());
                        Form frm = FormComponentPropertyType.INSTANCE.getForm(propertyValue, context.getSolution());
                        if (frm == null)
                            continue;
                        isResponsive = frm.isResponsiveLayout();
                        // listformcomponents that are responsive must be also send over here (the components are also send over in the FormComponentSabloValue)
                        // this will result in duplicate component data, but we need the structure (and the component names in the right place)
                        // if (!isResponsive && pd.getConfig() instanceof ComponentTypeConfig && ((ComponentTypeConfig)pd.getConfig()).forFoundset != null)
                        // continue;
                        children.add("children_" + pd.getName());
                        writer.key("children_" + pd.getName());
                        writer.array();
                        FormComponentCache fccc = FormElementHelper.INSTANCE.getFormComponentCache(fe, pd, (JSONObject) propertyValue, frm, context.getSolution());
                        if (isResponsive) {
                            // layout containers are not in the cache we need to generate manually the model
                            frm.acceptVisitor(new ChildrenJSONGenerator(writer, context, frm, new IFormElementCache() {

                                @Override
                                public FormElement getFormElement(IFormElement component, FlattenedSolution flattendSol, PropertyPath path, boolean design) {
                                    for (FormElement formElement : fccc.getFormComponentElements()) {
                                        if (component.getID() == formElement.getPersistIfAvailable().getID()) {
                                            return formElement;
                                        }
                                    }
                                    return FormElementHelper.INSTANCE.getFormElement(component, flattendSol, path, design);
                                }
                            }, null, this.form, false, designer), PositionComparator.XY_PERSIST_COMPARATOR);
                        } else {
                            for (FormElement element : fccc.getFormComponentElements()) {
                                IFormElement persistOfElement = (IFormElement) element.getPersistIfAvailable();
                                persistOfElement.acceptVisitor(new ChildrenJSONGenerator(writer, context, null, null, null, this.form, false, designer), FORM_INDEX_WITH_HIERARCHY_COMPARATOR);
                            }
                        }
                        writer.endArray();
                    }
                    writer.key("formComponent");
                    writer.array();
                    children.stream().forEach((child) -> writer.value(child));
                    writer.endArray();
                }
            }
        }
        writer.endObject();
    } else if (o instanceof LayoutContainer) {
        writer.object();
        LayoutContainer layoutContainer = (LayoutContainer) o;
        writeLayoutContainer(writer, layoutContainer, formUI, designer);
        writer.key("children");
        writer.array();
        o.acceptVisitor(new ChildrenJSONGenerator(writer, context, o, cache, null, this.form, false, designer), PositionComparator.XY_PERSIST_COMPARATOR);
        writer.endArray();
        writer.endObject();
        return IPersistVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
    }
    return IPersistVisitor.CONTINUE_TRAVERSAL;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) FormComponentCache(com.servoy.j2db.server.ngclient.FormElementHelper.FormComponentCache) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) ArrayList(java.util.ArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Point(java.awt.Point) IFormElement(com.servoy.j2db.persistence.IFormElement) Point(java.awt.Point) PropertyDescription(org.sablo.specification.PropertyDescription) IFormElement(com.servoy.j2db.persistence.IFormElement) WebComponent(com.servoy.j2db.persistence.WebComponent) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) JSONObject(org.json.JSONObject)

Example 68 with FlattenedSolution

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

the class FormElementHelper method getFormComponentCache.

public FormComponentCache getFormComponentCache(INGFormElement formElement, PropertyDescription pd, JSONObject formElementValue, Form form, FlattenedSolution fs) {
    ConcurrentMap<UUID, Map<String, FormComponentCache>> cache = formElement.getDesignId() != null ? formComponentElementsForDesign : formComponentElements;
    Solution solutionCopy = fs.getSolutionCopy(false);
    FlattenedSolution usedFS = getSharedFlattenedSolution(fs);
    if (solutionCopy != null && solutionCopy.getForm(formElement.getForm().getName()) != null) {
        usedFS = fs;
        // if the form is a solution model for we can't use the standard caches.
        cache = solutionCopy.getRuntimeProperty(SOLUTION_MODEL_CACHE);
        if (cache == null) {
            cache = new ConcurrentHashMap<UUID, Map<String, FormComponentCache>>();
            solutionCopy.setRuntimeProperty(SOLUTION_MODEL_CACHE, cache);
        }
    }
    return getFormComponentFromCache(formElement, pd, formElementValue, form, usedFS, cache);
}
Also used : FlattenedSolution(com.servoy.j2db.FlattenedSolution) UUID(com.servoy.j2db.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) WeakHashMap(java.util.WeakHashMap) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution)

Example 69 with FlattenedSolution

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

the class ComponentPropertyType method toTemplateJSONValue.

@Override
public JSONWriter toTemplateJSONValue(final JSONWriter writer, String key, ComponentTypeFormElementValue formElementValue, PropertyDescription pd, DataConversion conversionMarkers, final FormElementContext formElementContext) throws JSONException {
    if (formElementValue == null)
        return writer;
    FlattenedSolution clientFlattenedSolution = (formElementContext != null && formElementContext.getContext() != null) ? formElementContext.getContext().getSolution() : null;
    if (!formElementValue.isSecurityViewable(clientFlattenedSolution)) {
        return writer;
    }
    // so that the client knows it must use the custom client side JS for what JSON it gets
    if (conversionMarkers != null)
        conversionMarkers.convert(ComponentPropertyType.TYPE_NAME);
    // create children of component as specified by this property
    final FormElementContext feContext = new FormElementContext(formElementValue.element, formElementContext.getContext(), null);
    JSONUtils.addKeyIfPresent(writer, key);
    writer.object();
    writeTemplateJSONContent(writer, formElementValue, forFoundsetTypedPropertyName(pd), feContext, new IModelWriter() {

        @Override
        public void writeComponentModel() throws JSONException {
            // TODO here we could remove record based props from fe.propertiesForTemplateJSON(); but normally record based props will not write any value in template anyway
            TypedData<Map<String, Object>> modelProperties = feContext.getFormElement().propertiesForTemplateJSON();
            writer.object();
            JSONUtils.writeDataWithConversions(FormElementToJSON.INSTANCE, writer, modelProperties.content, modelProperties.contentType, feContext);
            writer.endObject();
        }
    }, formElementValue.recordBasedProperties, true);
    writer.endObject();
    return writer;
}
Also used : TypedData(org.sablo.websocket.TypedData) FlattenedSolution(com.servoy.j2db.FlattenedSolution) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) FormElementContext(com.servoy.j2db.server.ngclient.FormElementContext)

Example 70 with FlattenedSolution

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

the class MediaResourcesServlet method sendFlattenedSolutionBasedMedia.

private boolean sendFlattenedSolutionBasedMedia(HttpServletRequest request, HttpServletResponse response, String rootSolutionName, String mediaName) throws IOException {
    FlattenedSolution fs = null;
    try {
        IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
        SolutionMetaData solutionMetaData = (SolutionMetaData) ApplicationServerRegistry.get().getLocalRepository().getRootObjectMetaData(rootSolutionName, IRepository.SOLUTIONS);
        if (solutionMetaData == null) {
            Debug.error("Solution '" + rootSolutionName + "' was not found when sending media data for '" + mediaName + "'.");
            return false;
        }
        fs = new FlattenedSolution(solutionMetaData, new AbstractActiveSolutionHandler(as) {

            @Override
            public IRepository getRepository() {
                return ApplicationServerRegistry.get().getLocalRepository();
            }
        });
    } catch (RepositoryException e) {
        Debug.error(e);
    }
    try {
        return findAndSendMediaData(request, response, mediaName, fs);
    } finally {
        fs.close(null);
    }
}
Also used : AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) RepositoryException(com.servoy.j2db.persistence.RepositoryException) SolutionMetaData(com.servoy.j2db.persistence.SolutionMetaData)

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