<commit_before>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id: PortletTableLayout.java 5032 2006-08-17 18:15:06Z novotny $
 */
package org.gridsphere.layout;

import org.gridsphere.layout.view.TableLayoutView;
import org.gridsphere.portlet.impl.SportletProperties;
import org.gridsphere.portlet.service.PortletServiceException;
import org.gridsphere.portlet.service.spi.PortletServiceFactory;
import org.gridsphere.portletcontainer.ApplicationPortlet;
import org.gridsphere.portletcontainer.GridSphereEvent;
import org.gridsphere.provider.portlet.jsr.PortletServlet;
import org.gridsphere.services.core.jcr.JCRService;
import org.gridsphere.services.core.registry.PortletRegistryService;

import javax.portlet.PortletRequest;
import java.io.Serializable;
import java.util.*;

/**
 * The <code>PortletTableLayout</code> is a concrete implementation of the <code>PortletFrameLayout</code>
 * that organizes portlets into a grid with a provided number of columns.
 * Portlets are arranged in column-wise order starting from the left most column.
 */
public class PortletTableLayout extends PortletFrameLayout implements Serializable, Cloneable {

    public static final String PORTLET_COL = "gs_col";
    public static final String PORTLET_ADD_PORTLET = "gs_addPortlet";
    public static final String PORTLET_NO_ACTION = "gs_none";

    public static final String PORTLET_ADD_CONTENT = "gs_addContent";

    protected transient TableLayoutView tableView = null;

    protected transient PortletRegistryService registryService;
    protected transient JCRService contentService;

    /**
     * css Style of the table
     */
    protected String style = "";

    /**
     * Constructs an instance of PortletTableLayout
     */
    public PortletTableLayout() {
    }

    /**
     * Returns the CSS style name for the grid-layout.
     *
     * @return css style name
     */
    public String getStyle() {
        return style;
    }

    /**
     * Sets the CSS style name for the grid-layout.
     * This needs to be set if you want to have transparent portlets, if there is
     * no background there can't be a real transparent portlet.
     * Most likely one sets just the background in that one.
     *
     * @param style css style of the that layout
     */
    public void setStyle(String style) {
        this.style = style;
    }

    public List<ComponentIdentifier> init(PortletRequest req, List<ComponentIdentifier> list) {
        try {
            registryService = (PortletRegistryService) PortletServiceFactory.createPortletService(PortletRegistryService.class, true);
            contentService = (JCRService) PortletServiceFactory.createPortletService(JCRService.class, true);
        } catch (PortletServiceException e) {
            log.error("Unable to create instance of PortletRegistryService!");
        }
        tableView = (TableLayoutView) getRenderClass(req, "TableLayout");
        return super.init(req, list);
    }

    private PortletComponent getMaximizedComponent(List components) {
        PortletComponent p;
        for (int i = 0; i < components.size(); i++) {
            p = (PortletComponent) components.get(i);
            if (p instanceof PortletLayout) {
                PortletComponent layout = this.getMaximizedComponent(((PortletLayout) p).getPortletComponents());
                if (layout != null) {
                    p = layout;
                }
            }
            if (p.getWidth().equals("100%")) {
                return p;
            }
        }
        return null;
    }

    protected void addPortlet(GridSphereEvent event) {

        PortletRequest req = event.getActionRequest();

        String portletId = req.getParameter(PORTLET_ADD_PORTLET);

        if (portletId.equals(PORTLET_NO_ACTION)) return;

        String colStr = req.getParameter(PORTLET_COL);
        int col = Integer.valueOf(colStr).intValue();

        // first loop thru rows to see if one is empty for the requested column
        if (!components.isEmpty()) {
            Object o = components.get(0);
            if (o instanceof PortletFrameLayout) {
                PortletFrameLayout r = (PortletFrameLayout) o;
                List cols = r.getPortletComponents();

                Object c = cols.get(col);

                if (c instanceof PortletFrameLayout) {
                    PortletFrameLayout existingColumn = (PortletFrameLayout) c;

                    if (!portletId.equals("")) {
                        PortletFrame frame = new PortletFrame();
                        frame.setPortletClass(portletId);
                        existingColumn.addPortletComponent(frame);
                    }
                }
            }
        }
        req.setAttribute(SportletProperties.INIT_PAGE, "true");
    }

    protected void addContent(GridSphereEvent event) {

        PortletRequest req = event.getActionRequest();

        String textFile = req.getParameter(PORTLET_ADD_CONTENT);

        if (textFile.equals(PORTLET_NO_ACTION)) return;

        String colStr = req.getParameter(PORTLET_COL);
        int col = Integer.valueOf(colStr).intValue();

        // first loop thru rows to see if one is empty for the requested column
        if (!components.isEmpty()) {
            Object o = components.get(0);
            if (o instanceof PortletFrameLayout) {
                PortletFrameLayout r = (PortletFrameLayout) o;
                List cols = r.getPortletComponents();

                Object c = cols.get(col);

                if (c instanceof PortletFrameLayout) {
                    PortletFrameLayout existingColumn = (PortletFrameLayout) c;

                    if (!textFile.equals("")) {
                        PortletContent content = new PortletContent();
                        content.setInclude("jcr://" + textFile);
                        existingColumn.addPortletComponent(content);
                    }
                }
            }
        }
        req.setAttribute(SportletProperties.INIT_PAGE, "true");
    }

    public void customActionPerformed(GridSphereEvent event) {
        if (event.hasAction()) {
            if (event.getAction().getName().equals(PORTLET_ADD_PORTLET)) {
                addPortlet(event);
            }
            if (event.getAction().getName().equals(PORTLET_ADD_CONTENT)) {
                addContent(event);
            }
        }
    }

    public Map getAllPortletsToAdd(GridSphereEvent event) {
        PortletRequest req = event.getRenderRequest();

        Map<String, String> allPortlets = new HashMap<String, String>();
        Collection<ApplicationPortlet> appColl = registryService.getAllApplicationPortlets();
        Locale locale = req.getLocale();
        for (ApplicationPortlet appPortlet : appColl) {
            String concID = appPortlet.getConcretePortletID();
            // we don't want to list PortletServlet loader!
            if (concID.startsWith(PortletServlet.class.getName())) continue;
            String dispName = appPortlet.getDisplayName(locale);
            allPortlets.put(concID, dispName);
        }
        return allPortlets;
    }

    public Map getAllContentToAdd(GridSphereEvent event) {
        Map<String, String> allContent = new HashMap<String, String>();
        List<String> nodeNames = contentService.getAllNodeNames();
        for (String nodeName : nodeNames) {
            allContent.put(nodeName, nodeName);
        }
        return allContent;
    }

    public void doRender(GridSphereEvent event) {
        super.doRender(event);
        PortletRequest req = event.getRenderRequest();
        StringBuffer table = new StringBuffer();
        PortletComponent p;
<commit_msg>Added MoTD Portlet as well as some changes to Content, no use of JCR 170 in portal code directly, only in one service.<commit_after>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id: PortletTableLayout.java 5032 2006-08-17 18:15:06Z novotny $
 */
package org.gridsphere.layout;

import org.gridsphere.layout.view.TableLayoutView;
import org.gridsphere.portlet.impl.SportletProperties;
import org.gridsphere.portlet.service.PortletServiceException;
import org.gridsphere.portlet.service.spi.PortletServiceFactory;
import org.gridsphere.portletcontainer.ApplicationPortlet;
import org.gridsphere.portletcontainer.GridSphereEvent;
import org.gridsphere.provider.portlet.jsr.PortletServlet;
import org.gridsphere.services.core.jcr.ContentDocument;
import org.gridsphere.services.core.jcr.ContentException;
import org.gridsphere.services.core.jcr.JCRService;
import org.gridsphere.services.core.registry.PortletRegistryService;

import javax.portlet.PortletRequest;
import java.io.Serializable;
import java.util.*;

/**
 * The <code>PortletTableLayout</code> is a concrete implementation of the <code>PortletFrameLayout</code>
 * that organizes portlets into a grid with a provided number of columns.
 * Portlets are arranged in column-wise order starting from the left most column.
 */
public class PortletTableLayout extends PortletFrameLayout implements Serializable, Cloneable {

    public static final String PORTLET_COL = "gs_col";
    public static final String PORTLET_ADD_PORTLET = "gs_addPortlet";
    public static final String PORTLET_NO_ACTION = "gs_none";

    public static final String PORTLET_ADD_CONTENT = "gs_addContent";

    protected transient TableLayoutView tableView = null;

    protected transient PortletRegistryService registryService;
    protected transient JCRService contentService;

    /**
     * css Style of the table
     */
    protected String style = "";

    /**
     * Constructs an instance of PortletTableLayout
     */
    public PortletTableLayout() {
    }

    /**
     * Returns the CSS style name for the grid-layout.
     *
     * @return css style name
     */
    public String getStyle() {
        return style;
    }

    /**
     * Sets the CSS style name for the grid-layout.
     * This needs to be set if you want to have transparent portlets, if there is
     * no background there can't be a real transparent portlet.
     * Most likely one sets just the background in that one.
     *
     * @param style css style of the that layout
     */
    public void setStyle(String style) {
        this.style = style;
    }

    public List<ComponentIdentifier> init(PortletRequest req, List<ComponentIdentifier> list) {
        try {
            registryService = (PortletRegistryService) PortletServiceFactory.createPortletService(PortletRegistryService.class, true);
            contentService = (JCRService) PortletServiceFactory.createPortletService(JCRService.class, true);
        } catch (PortletServiceException e) {
            log.error("Unable to create instance of PortletRegistryService!");
        }
        tableView = (TableLayoutView) getRenderClass(req, "TableLayout");
        return super.init(req, list);
    }

    private PortletComponent getMaximizedComponent(List components) {
        PortletComponent p;
        for (int i = 0; i < components.size(); i++) {
            p = (PortletComponent) components.get(i);
            if (p instanceof PortletLayout) {
                PortletComponent layout = this.getMaximizedComponent(((PortletLayout) p).getPortletComponents());
                if (layout != null) {
                    p = layout;
                }
            }
            if (p.getWidth().equals("100%")) {
                return p;
            }
        }
        return null;
    }

    protected void addPortlet(GridSphereEvent event) {

        PortletRequest req = event.getActionRequest();

        String portletId = req.getParameter(PORTLET_ADD_PORTLET);

        if (portletId.equals(PORTLET_NO_ACTION)) return;

        String colStr = req.getParameter(PORTLET_COL);
        int col = Integer.valueOf(colStr).intValue();

        // first loop thru rows to see if one is empty for the requested column
        if (!components.isEmpty()) {
            Object o = components.get(0);
            if (o instanceof PortletFrameLayout) {
                PortletFrameLayout r = (PortletFrameLayout) o;
                List cols = r.getPortletComponents();

                Object c = cols.get(col);

                if (c instanceof PortletFrameLayout) {
                    PortletFrameLayout existingColumn = (PortletFrameLayout) c;

                    if (!portletId.equals("")) {
                        PortletFrame frame = new PortletFrame();
                        frame.setPortletClass(portletId);
                        existingColumn.addPortletComponent(frame);
                    }
                }
            }
        }
        req.setAttribute(SportletProperties.INIT_PAGE, "true");
    }

    protected void addContent(GridSphereEvent event) {

        PortletRequest req = event.getActionRequest();

        String textFile = req.getParameter(PORTLET_ADD_CONTENT);

        if (textFile.equals(PORTLET_NO_ACTION)) return;

        String colStr = req.getParameter(PORTLET_COL);
        int col = Integer.valueOf(colStr).intValue();

        // first loop thru rows to see if one is empty for the requested column
        if (!components.isEmpty()) {
            Object o = components.get(0);
            if (o instanceof PortletFrameLayout) {
                PortletFrameLayout r = (PortletFrameLayout) o;
                List cols = r.getPortletComponents();

                Object c = cols.get(col);

                if (c instanceof PortletFrameLayout) {
                    PortletFrameLayout existingColumn = (PortletFrameLayout) c;

                    if (!textFile.equals("")) {
                        PortletContent content = new PortletContent();
                        content.setInclude("jcr://" + textFile);
                        existingColumn.addPortletComponent(content);
                    }
                }
            }
        }
        req.setAttribute(SportletProperties.INIT_PAGE, "true");
    }

    public void customActionPerformed(GridSphereEvent event) {
        if (event.hasAction()) {
            if (event.getAction().getName().equals(PORTLET_ADD_PORTLET)) {
                addPortlet(event);
            }
            if (event.getAction().getName().equals(PORTLET_ADD_CONTENT)) {
                addContent(event);
            }
        }
    }

    public Map getAllPortletsToAdd(GridSphereEvent event) {
        PortletRequest req = event.getRenderRequest();

        Map<String, String> allPortlets = new HashMap<String, String>();
        Collection<ApplicationPortlet> appColl = registryService.getAllApplicationPortlets();
        Locale locale = req.getLocale();
        for (ApplicationPortlet appPortlet : appColl) {
            String concID = appPortlet.getConcretePortletID();
            // we don't want to list PortletServlet loader!
            if (concID.startsWith(PortletServlet.class.getName())) continue;
            String dispName = appPortlet.getDisplayName(locale);
            allPortlets.put(concID, dispName);
        }
        return allPortlets;
    }

    public Map getAllContentToAdd(GridSphereEvent event) {
        Map<String, String> allContent = new HashMap<String, String>();
        List<ContentDocument> docs = null;
        try {
            docs = contentService.listChildContentDocuments("");
            for (int i = 0; i < docs.size(); i++) {
                ContentDocument doc = docs.get(i);
                allContent.put(doc.getTitle(), doc.getTitle());
            }
        } catch (ContentException e) {
            e.printStackTrace();
        }
        return allContent;
    }

    public void doRender(GridSphereEvent event) {
        super.doRender(event);
        PortletRequest req = event.getRenderRequest();
        StringBuffer table = new StringBuffer();
        PortletComponent p;

{'fraction_non_alphanumeric': 0.09116056833899158, 'fraction_numerical': 0.0032692065887086636, 'mean_word_length': 2.4363793475912723, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1079566', 'n_tokens_mistral': 4310, 'n_tokens_neox': 4105, 'n_words': 1045}