Search in sources :

Example 1 with AbstractActiveSolutionHandler

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

the class TemplateGenerator method getFormHTMLAndCSS.

public static Pair<String, String> getFormHTMLAndCSS(Solution solution, Form form, IServiceProvider sp, String formInstanceName) throws RepositoryException, RemoteException {
    if (form == null)
        return null;
    final IRepository repository = ApplicationServerRegistry.get().getLocalRepository();
    boolean enableAnchoring = sp != null ? Utils.getAsBoolean(sp.getRuntimeProperties().get("enableAnchors")) : Utils.getAsBoolean(Settings.getInstance().getProperty("servoy.webclient.enableAnchors", Boolean.TRUE.toString()));
    String overriddenStyleName = null;
    Pair<String, ArrayList<Pair<String, String>>> retval = formCache.getFormAndCssPair(form, formInstanceName, overriddenStyleName, repository);
    Form f = form;
    FlattenedSolution fsToClose = null;
    try {
        if (retval == null) {
            if (f.getExtendsID() > 0) {
                FlattenedSolution fs = sp == null ? null : sp.getFlattenedSolution();
                if (fs == null) {
                    try {
                        IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
                        fsToClose = fs = new FlattenedSolution(solution.getSolutionMetaData(), new AbstractActiveSolutionHandler(as) {

                            @Override
                            public IRepository getRepository() {
                                return repository;
                            }
                        });
                    } catch (RepositoryException e) {
                        Debug.error("Couldn't create flattened form for the template generator", e);
                    }
                }
                f = fs.getFlattenedForm(f);
                if (f == null) {
                    Debug.log("TemplateGenerator couldn't get a FlattenedForm for " + form + ", solution closed?");
                    f = form;
                }
            }
            StringBuffer html = new StringBuffer();
            TextualCSS css = new TextualCSS();
            IFormLayoutProvider layoutProvider = FormLayoutProviderFactory.getFormLayoutProvider(sp, solution, f, formInstanceName);
            int viewType = layoutProvider.getViewType();
            layoutProvider.renderOpenFormHTML(html, css);
            int startY = 0;
            Iterator<Part> parts = f.getParts();
            while (parts.hasNext()) {
                Part part = parts.next();
                int endY = part.getHeight();
                if (Part.rendersOnlyInPrint(part.getPartType())) {
                    startY = part.getHeight();
                    // is never shown (=printing only)
                    continue;
                }
                Color bgColor = ComponentFactory.getPartBackground(sp, part, f);
                if (part.getPartType() == Part.BODY && (viewType == FormController.TABLE_VIEW || viewType == FormController.LOCKED_TABLE_VIEW || viewType == IForm.LIST_VIEW || viewType == FormController.LOCKED_LIST_VIEW)) {
                    layoutProvider.renderOpenTableViewHTML(html, css, part);
                    // tableview == bodypart
                    createCellBasedView(f, f, html, css, layoutProvider.needsHeaders(), startY, endY, bgColor, sp, viewType, enableAnchoring, startY, endY);
                    layoutProvider.renderCloseTableViewHTML(html);
                } else {
                    layoutProvider.renderOpenPartHTML(html, css, part);
                    placePartElements(f, startY, endY, html, css, bgColor, enableAnchoring, sp);
                    layoutProvider.renderClosePartHTML(html, part);
                }
                startY = part.getHeight();
            }
            layoutProvider.renderCloseFormHTML(html);
            retval = new Pair<String, ArrayList<Pair<String, String>>>(html.toString(), css.getAsSelectorValuePairs());
            formCache.putFormAndCssPair(form, formInstanceName, overriddenStyleName, repository, retval);
        }
        Map<String, String> formIDToMarkupIDMap = null;
        if (sp instanceof IApplication) {
            Map runtimeProps = sp.getRuntimeProperties();
            Map<WebForm, Map<String, String>> clientFormsIDToMarkupIDMap = (Map<WebForm, Map<String, String>>) runtimeProps.get("WebFormIDToMarkupIDCache");
            if (clientFormsIDToMarkupIDMap == null) {
                clientFormsIDToMarkupIDMap = new WeakHashMap<WebForm, Map<String, String>>();
                runtimeProps.put("WebFormIDToMarkupIDCache", clientFormsIDToMarkupIDMap);
            }
            IForm wfc = ((IApplication) sp).getFormManager().getForm(formInstanceName);
            if (wfc instanceof FormController) {
                IFormUIInternal wf = ((FormController) wfc).getFormUI();
                if (wf instanceof WebForm) {
                    if (!((WebForm) wf).isUIRecreated())
                        formIDToMarkupIDMap = clientFormsIDToMarkupIDMap.get(wf);
                    if (formIDToMarkupIDMap == null) {
                        ArrayList<Pair<String, String>> formCSS = retval.getRight();
                        ArrayList<String> selectors = new ArrayList<String>(formCSS.size());
                        for (Pair<String, String> formCSSEntry : formCSS) selectors.add(formCSSEntry.getLeft());
                        formIDToMarkupIDMap = getWebFormIDToMarkupIDMap((WebForm) wf, selectors);
                        clientFormsIDToMarkupIDMap.put((WebForm) wf, formIDToMarkupIDMap);
                    }
                }
            }
        }
        String webFormCSS = getWebFormCSS(retval.getRight(), formIDToMarkupIDMap);
        // string the formcss/solutionname/ out of the url.
        webFormCSS = StripHTMLTagsConverter.convertMediaReferences(webFormCSS, solution.getName(), new ResourceReference("media"), "", false).toString();
        return new Pair<String, String>(retval.getLeft(), webFormCSS);
    } finally {
        if (fsToClose != null) {
            fsToClose.close(null);
        }
    }
}
Also used : IForm(com.servoy.j2db.IForm) Form(com.servoy.j2db.persistence.Form) WebForm(com.servoy.j2db.server.headlessclient.WebForm) WebForm(com.servoy.j2db.server.headlessclient.WebForm) ArrayList(java.util.ArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IForm(com.servoy.j2db.IForm) AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) ResourceReference(org.apache.wicket.ResourceReference) IFormUIInternal(com.servoy.j2db.IFormUIInternal) Pair(com.servoy.j2db.util.Pair) FormController(com.servoy.j2db.FormController) Color(java.awt.Color) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Point(java.awt.Point) IApplication(com.servoy.j2db.IApplication) Part(com.servoy.j2db.persistence.Part) IRepository(com.servoy.j2db.persistence.IRepository) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) TreeMap(java.util.TreeMap)

Example 2 with AbstractActiveSolutionHandler

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

the class FormElementHelper method getSharedFlattenedSolution.

private FlattenedSolution getSharedFlattenedSolution(FlattenedSolution fs) {
    FlattenedSolution flattenedSolution = globalFlattendSolutions.get(fs.getMainSolutionMetaData().getName());
    if (flattenedSolution == null) {
        try {
            flattenedSolution = new FlattenedSolution(true);
            flattenedSolution.setSolution(fs.getMainSolutionMetaData(), false, true, new AbstractActiveSolutionHandler(ApplicationServerRegistry.getService(IApplicationServer.class)) {

                @Override
                public IRepository getRepository() {
                    return ApplicationServerRegistry.get().getLocalRepository();
                }
            });
            FlattenedSolution alreadyCreated = globalFlattendSolutions.putIfAbsent(flattenedSolution.getName(), flattenedSolution);
            if (alreadyCreated != null) {
                flattenedSolution.close(null);
                flattenedSolution = alreadyCreated;
            }
        } catch (Exception e) {
            throw new RuntimeException("Can't create FlattenedSolution for: " + fs, e);
        }
    }
    return flattenedSolution;
}
Also used : AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IRepository(com.servoy.j2db.persistence.IRepository) JSONException(org.json.JSONException)

Example 3 with AbstractActiveSolutionHandler

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

the class SharedMediaResource method getResource.

private ResourceState getResource(final String iconId, final String solutionName) {
    return new ResourceState() {

        private String contentType;

        private int length;

        byte[] array = null;

        @Override
        public Time lastModifiedTime() {
            try {
                IRootObject solution = ApplicationServerRegistry.get().getLocalRepository().getActiveRootObject(solutionName, IRepository.SOLUTIONS);
                if (solution != null)
                    return Time.valueOf(solution.getLastModifiedTime());
            } catch (Exception e) {
                Debug.trace(e);
            }
            return time;
        }

        @Override
        public byte[] getData() {
            if (array == null) {
                boolean closeFS = false;
                try {
                    final IRepository repository = ApplicationServerRegistry.get().getLocalRepository();
                    FlattenedSolution fs = null;
                    try {
                        if (Session.exists() && ((WebClientSession) Session.get()).getWebClient() != null) {
                            fs = ((WebClientSession) Session.get()).getWebClient().getFlattenedSolution();
                        }
                        if (fs == null) {
                            SolutionMetaData solutionMetaData = (SolutionMetaData) repository.getRootObjectMetaData(solutionName, IRepository.SOLUTIONS);
                            if (solutionMetaData == null)
                                return new byte[0];
                            closeFS = true;
                            IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
                            fs = new FlattenedSolution(solutionMetaData, new AbstractActiveSolutionHandler(as) {

                                @Override
                                public IRepository getRepository() {
                                    return repository;
                                }
                            });
                        }
                        Media m = fs.getMedia(iconId);
                        if (m == null) {
                            try {
                                Integer iIconID = new Integer(iconId);
                                m = fs.getMedia(iIconID.intValue());
                            } catch (NumberFormatException ex) {
                                Debug.error("no media found for: " + iconId);
                            }
                        }
                        if (m != null) {
                            array = m.getMediaData();
                            contentType = m.getMimeType();
                        }
                    } finally {
                        if (closeFS && fs != null) {
                            fs.close(null);
                        }
                    }
                    if (array != null) {
                        if (contentType == null) {
                            contentType = MimeTypes.getContentType(array);
                        }
                        length = array.length;
                    }
                } catch (Exception ex) {
                    Debug.error(ex);
                }
            }
            return array == null ? new byte[0] : array;
        }

        /**
         * @see wicket.markup.html.DynamicWebResource.ResourceState#getLength()
         */
        @Override
        public int getLength() {
            return length;
        }

        @Override
        public String getContentType() {
            return contentType;
        }
    };
}
Also used : Media(com.servoy.j2db.persistence.Media) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) IRootObject(com.servoy.j2db.persistence.IRootObject) StringValueConversionException(org.apache.wicket.util.string.StringValueConversionException) SolutionMetaData(com.servoy.j2db.persistence.SolutionMetaData) AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) IRepository(com.servoy.j2db.persistence.IRepository)

Example 4 with AbstractActiveSolutionHandler

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

the class FormTemplateGenerator method isSingleValueComponent.

/**
 * @return false if the persist has no valuelist or at most one value in the valuelist, true otherwise
 */
public static boolean isSingleValueComponent(IFormElement persist) {
    Field field = (Field) persist;
    if (field.getValuelistID() > 0) {
        try {
            IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
            FlattenedSolution fs = new FlattenedSolution((SolutionMetaData) ApplicationServerRegistry.get().getLocalRepository().getRootObjectMetaData(((Solution) persist.getRootObject()).getName(), IRepository.SOLUTIONS), new AbstractActiveSolutionHandler(as) {

                @Override
                public IRepository getRepository() {
                    return ApplicationServerRegistry.get().getLocalRepository();
                }
            });
            ValueList valuelist = fs.getValueList(field.getValuelistID());
            return isSingleValue(valuelist);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : Field(com.servoy.j2db.persistence.Field) AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) ValueList(com.servoy.j2db.persistence.ValueList) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IRepository(com.servoy.j2db.persistence.IRepository) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 5 with AbstractActiveSolutionHandler

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

the class AngularIndexPageWriter method getFlattenedSolution.

private static Pair<FlattenedSolution, Boolean> getFlattenedSolution(String solutionName, String clientnr, HttpServletRequest request, HttpServletResponse response) {
    INGClientWebsocketSession wsSession = null;
    HttpSession httpSession = request.getSession(false);
    if (clientnr != null && httpSession != null) {
        wsSession = (INGClientWebsocketSession) WebsocketSessionManager.getSession(CLIENT_ENDPOINT, httpSession, Integer.parseInt(clientnr));
    }
    FlattenedSolution fs = null;
    boolean closeFS = false;
    if (wsSession != null) {
        fs = wsSession.getClient().getFlattenedSolution();
    }
    if (fs == null) {
        try {
            closeFS = true;
            IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
            if (applicationServerUnavailable(response, as)) {
                return new Pair<FlattenedSolution, Boolean>(null, Boolean.FALSE);
            }
            SolutionMetaData solutionMetaData = (SolutionMetaData) ApplicationServerRegistry.get().getLocalRepository().getRootObjectMetaData(solutionName, SOLUTIONS);
            if (solutionMissing(response, solutionName, solutionMetaData)) {
                return new Pair<FlattenedSolution, Boolean>(null, Boolean.FALSE);
            }
            fs = new FlattenedSolution(solutionMetaData, new AbstractActiveSolutionHandler(as) {

                @Override
                public IRepository getRepository() {
                    return ApplicationServerRegistry.get().getLocalRepository();
                }
            });
        } catch (Exception e) {
            Debug.error("error loading solution: " + solutionName + " for clientnr: " + clientnr, e);
        }
    }
    return new Pair<FlattenedSolution, Boolean>(fs, Boolean.valueOf(closeFS));
}
Also used : AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) HttpSession(javax.servlet.http.HttpSession) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) SolutionMetaData(com.servoy.j2db.persistence.SolutionMetaData) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) Pair(com.servoy.j2db.util.Pair)

Aggregations

AbstractActiveSolutionHandler (com.servoy.j2db.AbstractActiveSolutionHandler)7 FlattenedSolution (com.servoy.j2db.FlattenedSolution)7 IApplicationServer (com.servoy.j2db.server.shared.IApplicationServer)6 IRepository (com.servoy.j2db.persistence.IRepository)4 SolutionMetaData (com.servoy.j2db.persistence.SolutionMetaData)4 IOException (java.io.IOException)3 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 Pair (com.servoy.j2db.util.Pair)2 ArrayList (java.util.ArrayList)2 ServletException (javax.servlet.ServletException)2 HttpSession (javax.servlet.http.HttpSession)2 JSONException (org.json.JSONException)2 FormController (com.servoy.j2db.FormController)1 IApplication (com.servoy.j2db.IApplication)1 IForm (com.servoy.j2db.IForm)1 IFormUIInternal (com.servoy.j2db.IFormUIInternal)1 Field (com.servoy.j2db.persistence.Field)1 Form (com.servoy.j2db.persistence.Form)1 IRootObject (com.servoy.j2db.persistence.IRootObject)1 Media (com.servoy.j2db.persistence.Media)1