Search in sources :

Example 1 with IRepository

use of com.servoy.j2db.persistence.IRepository 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 IRepository

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

the class CmdManager method propertyChange.

public void propertyChange(PropertyChangeEvent evt) {
    String name = evt.getPropertyName();
    if (// $NON-NLS-1$
    "repository".equals(name)) {
        final IRepository repository = (IRepository) evt.getNewValue();
        // $NON-NLS-1$
        Action cmdnewsolution = actions.get("cmdnewsolution");
        if (cmdnewsolution != null)
            cmdnewsolution.setEnabled(repository != null);
        if (autoOpenSolutionSelectDialog) {
            // $NON-NLS-1$
            final Action cmdopensolution = actions.get("cmdopensolution");
            if (cmdopensolution != null) {
                application.invokeLater(new Runnable() {

                    public void run() {
                        cmdopensolution.setEnabled(repository != null);
                        if (repository != null) {
                            try {
                                if (repository.getRootObjectMetaDatasForType(IRepository.SOLUTIONS).length != 0 && application.getSolution() == null && (application.getFlattenedSolution() == null || !application.getFlattenedSolution().isLoadingSolution())) {
                                    executeCmd((ICmd) cmdopensolution, null);
                                }
                            } catch (Exception ex) {
                                Debug.error(ex);
                            }
                        }
                    }
                });
            }
        }
    } else if (// $NON-NLS-1$
    "solution".equals(name)) {
        Solution solution = (Solution) evt.getNewValue();
        ableFormRelatedBrowseActions(solution != null);
        ableFormRelatedDataEditActions(solution != null);
        undoAction.setEnabled(solution != null);
        redoAction.setEnabled(solution != null);
        // $NON-NLS-1$
        Action cmdclose = actions.get("cmdclose");
        if (cmdclose != null)
            cmdclose.setEnabled(solution != null);
        // $NON-NLS-1$
        Action cmdsolutionsettings = actions.get("cmdsolutionsettings");
        if (cmdsolutionsettings != null)
            cmdsolutionsettings.setEnabled(solution != null);
        // TODO:could be optimized by using fast method getFormCount
        if (solution != null && application.getFlattenedSolution().getForms(false).hasNext()) {
            ableFormRelatedActions(true);
        } else {
            ableFormRelatedActions(false);
        }
    } else if (// $NON-NLS-1$
    "mode".equals(name)) {
        int oldmode = ((Integer) evt.getOldValue()).intValue();
        // $NON-NLS-1$
        Action menuselectaction = actions.get("menuselectaction");
        int mode = ((Integer) evt.getNewValue()).intValue();
        switch(mode) {
            case IModeManager.FIND_MODE:
                break;
            case IModeManager.PREVIEW_MODE:
                break;
            case IModeManager.EDIT_MODE:
            default:
                if (menuselectaction != null)
                    menuselectaction.setEnabled(true);
        }
        ableFormRelatedFindActions(mode == IModeManager.FIND_MODE);
        // $NON-NLS-1$
        Action cmdfindmode = actions.get("cmdfindmode");
        if (cmdfindmode != null)
            cmdfindmode.setEnabled(mode == IModeManager.EDIT_MODE);
        if (mode == IModeManager.FIND_MODE) {
            ableFormRelatedBrowseActions(false);
            ableFormRelatedDataEditActions(true);
        } else {
            ableFormRelatedBrowseActions(mode == IModeManager.EDIT_MODE);
        }
    } else if (// $NON-NLS-1$
    "formCreated".equals(name)) {
        ableFormRelatedActions(evt.getNewValue() != null);
    } else if (// $NON-NLS-1$
    "undomanager".equals(name)) {
        String sUndoRedo = (String) evt.getOldValue();
        Boolean bValue = (Boolean) evt.getNewValue();
        Action menuUndoRedo = actions.get(sUndoRedo);
        if (menuUndoRedo != null)
            menuUndoRedo.setEnabled(bValue.booleanValue());
    }
}
Also used : Action(javax.swing.Action) ICmd(com.servoy.j2db.cmd.ICmd) IRepository(com.servoy.j2db.persistence.IRepository) Solution(com.servoy.j2db.persistence.Solution)

Example 3 with IRepository

use of com.servoy.j2db.persistence.IRepository 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 4 with IRepository

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

the class WebClientSession method startSessionClient.

@SuppressWarnings("nls")
public IWebClientApplication startSessionClient(RootObjectMetaData sd, String method, StartupArguments argumentsScope) throws Exception {
    String firstArgument = argumentsScope.getFirstArgument();
    IWebClientApplication webClient = getWebClient();
    if (webClient != null) {
        boolean solutionLoaded = webClient.getSolution() != null;
        if (solutionLoaded && !webClient.closeSolution(false, null)) {
            // not allowed to close solution?
            return webClient;
        }
        if (solutionLoaded && isSignedIn() && !Utils.getAsBoolean(Settings.getInstance().getProperty("servoy.allowSolutionBrowsing", "true")) && !sd.getName().equals(keepCredentialsSolutionName)) {
            webClient.logout(null);
        }
        if (!isSignedIn()) {
            SolutionMetaData smd = (SolutionMetaData) sd;
            IRepository repository = ApplicationServerRegistry.get().getLocalRepository();
            Solution sol = (Solution) repository.getActiveRootObject(smd.getName(), IRepository.SOLUTIONS);
            if (sol.getLoginSolutionName() == null && sol.getLoginFormID() <= 0 && smd.getMustAuthenticate()) {
                // signin first
                throw new RestartResponseAtInterceptPageException(SignIn.class);
            }
        }
        keepCredentialsSolutionName = null;
    }
    if (webClient == null || webClient.isShutDown()) {
        HttpServletRequest req = ((WebRequest) RequestCycle.get().getRequest()).getHttpServletRequest();
        httpSession = req.getSession();
        webClient = createWebClient(req, credentials, method, firstArgument == null ? null : new Object[] { firstArgument, argumentsScope.toJSMap() }, sd.getName());
        webClient.handleArguments(new String[] { sd.getName() }, argumentsScope);
        if (RequestCycle.get() != null) {
            // if this is inside a request cycle set the service provider.
            // will be reset by the detach of the RequestCycle.
            J2DBGlobals.setServiceProvider(webClient);
        }
        setAttribute("servoy_webclient", webClient);
    } else {
        webClient.handleArguments(firstArgument != null ? new String[] { sd.getName(), method, firstArgument } : new String[] { sd.getName(), method }, argumentsScope);
    }
    // fake first load
    webClient.handleClientUserUidChanged(null, "");
    if (webClient.getSolution() != null)
        getSolutionLastModifiedTime(webClient.getSolution());
    else {
        if (webClient.getPreferedSolutionNameToLoadOnInit() != null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("s", webClient.getPreferedSolutionNameToLoadOnInit());
            map.put("m", webClient.getPreferedSolutionMethodNameToCall());
            if (webClient.getPreferedSolutionMethodArguments() != null && webClient.getPreferedSolutionMethodArguments().length > 0) {
                map.put("a", webClient.getPreferedSolutionMethodArguments()[0]);
            }
            throw new RestartResponseException(SolutionLoader.class, new PageParameters(map));
        }
    }
    return webClient;
}
Also used : HashMap(java.util.HashMap) RestartResponseAtInterceptPageException(org.apache.wicket.RestartResponseAtInterceptPageException) PageParameters(org.apache.wicket.PageParameters) SolutionMetaData(com.servoy.j2db.persistence.SolutionMetaData) HttpServletRequest(javax.servlet.http.HttpServletRequest) WebRequest(org.apache.wicket.protocol.http.WebRequest) RestartResponseException(org.apache.wicket.RestartResponseException) IWebClientApplication(com.servoy.j2db.IWebClientApplication) IRepository(com.servoy.j2db.persistence.IRepository) Solution(com.servoy.j2db.persistence.Solution)

Example 5 with IRepository

use of com.servoy.j2db.persistence.IRepository 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)

Aggregations

IRepository (com.servoy.j2db.persistence.IRepository)9 FlattenedSolution (com.servoy.j2db.FlattenedSolution)5 AbstractActiveSolutionHandler (com.servoy.j2db.AbstractActiveSolutionHandler)4 Solution (com.servoy.j2db.persistence.Solution)3 IApplicationServer (com.servoy.j2db.server.shared.IApplicationServer)3 IForm (com.servoy.j2db.IForm)2 Form (com.servoy.j2db.persistence.Form)2 SolutionMetaData (com.servoy.j2db.persistence.SolutionMetaData)2 WebForm (com.servoy.j2db.server.headlessclient.WebForm)2 Pair (com.servoy.j2db.util.Pair)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 FormController (com.servoy.j2db.FormController)1 IApplication (com.servoy.j2db.IApplication)1 IFormUIInternal (com.servoy.j2db.IFormUIInternal)1 IMessagesCallback (com.servoy.j2db.IMessagesCallback)1 IServiceProvider (com.servoy.j2db.IServiceProvider)1 IWebClientApplication (com.servoy.j2db.IWebClientApplication)1 ICmd (com.servoy.j2db.cmd.ICmd)1