<commit_before>/*
 * Copyright 2003 - 2011 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */


package org.efaps.esjp.projects.task;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.efaps.admin.dbproperty.DBProperties;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Return;
import org.efaps.admin.event.Return.ReturnValues;
import org.efaps.admin.program.esjp.EFapsRevision;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.db.Instance;
import org.efaps.db.InstanceQuery;
import org.efaps.db.MultiPrintQuery;
import org.efaps.db.QueryBuilder;
import org.efaps.db.SelectBuilder;
import org.efaps.esjp.ci.CIProjects;
import org.efaps.esjp.ui.html.HtmlTable;
import org.efaps.util.EFapsException;
import org.joda.time.DateTime;
import org.joda.time.Interval;

/**
 * TODO comment!
 *
 * @author The eFaps Team
 * @version $Id$
 */
@EFapsUUID("5829d1d9-bfb4-430e-93c4-a6e9467ea663")
@EFapsRevision("$Rev$")
public abstract class GantHtmlTable_Base
{
    /**
     * Create a html table that represents the tasks.
     * @param _parameter Parameter as passed by the eFaps API
     * @return new Return
     * @throws EFapsException on error
     */
    public Return getGantHtmlTableFieldValue(final Parameter _parameter)
        throws EFapsException
    {
        final Return ret = new Return();
        final List<Instance> instances = new ArrayList<Instance>();
        if (_parameter.getInstance().getType().isKindOf(CIProjects.ProjectAbstract.getType())) {
            final QueryBuilder queryBldr = new QueryBuilder(CIProjects.TaskAbstract);
            queryBldr.addWhereAttrEqValue(CIProjects.TaskAbstract.ProjectAbstractLink,
                            _parameter.getInstance().getId());
            final InstanceQuery query = queryBldr.getQuery();
            instances.addAll(query.execute());
        } else {
            instances.add(_parameter.getInstance());
        }

        final Map<Instance, ATask> tasks = new HashMap<Instance, ATask>();
        DateTime projectDate = null;
        DateTime projectDueDate = null;
        DateTime maxDate = null;
        DateTime minDate = null;
        final MultiPrintQuery multi = new MultiPrintQuery(instances);
        final SelectBuilder selDate = new SelectBuilder()
            .linkto(CIProjects.TaskAbstract.ProjectAbstractLink)
            .attribute(CIProjects.ProjectAbstract.Date);
        final SelectBuilder selDue = new SelectBuilder()
            .linkto(CIProjects.TaskAbstract.ProjectAbstractLink)
            .attribute(CIProjects.ProjectAbstract.DueDate);
        final SelectBuilder selPar = new SelectBuilder().linkto(CIProjects.TaskAbstract.ParentTaskAbstractLink).oid();
        multi.addSelect(selDate, selDue, selPar);
        multi.addAttribute(CIProjects.TaskAbstract.DateFrom, CIProjects.TaskAbstract.DateUntil,
                        CIProjects.TaskAbstract.Name, CIProjects.TaskAbstract.Description);
        add2MultiPrint(_parameter, multi);
        multi.execute();
        while (multi.next()) {
            final ATask task = getTask(_parameter, multi.getCurrentInstance(),
                            Instance.get(multi.<String>getSelect(selPar)));
            tasks.put(multi.getCurrentInstance(), task);
            final DateTime dateFrom = multi.<DateTime>getAttribute(CIProjects.TaskAbstract.DateFrom);
            final DateTime dateUntil = multi.<DateTime>getAttribute(CIProjects.TaskAbstract.DateUntil);
            task.addAttribute(CIProjects.TaskAbstract.DateFrom.name, dateFrom);
            task.addAttribute(CIProjects.TaskAbstract.DateUntil.name, dateUntil);
            task.addAttribute(CIProjects.TaskAbstract.Name.name,
                            multi.getAttribute(CIProjects.TaskAbstract.Name));
            task.addAttribute(CIProjects.TaskAbstract.Description.name,
                            multi.getAttribute(CIProjects.TaskAbstract.Description));
            add2Attributes(_parameter, task, multi);
            if (projectDate == null) {
                projectDate = multi.<DateTime>getSelect(selDate);
                projectDueDate = multi.<DateTime>getSelect(selDue);
                minDate = projectDate;
                maxDate = projectDueDate;
            }

            if (dateFrom.isBefore(minDate)) {
                minDate = dateFrom;
            }
            if (dateUntil.isAfter(maxDate)) {
                maxDate = dateUntil;
            }
        }
        final List<ATask> roots = new ArrayList<ATask>();
        for (final Entry<Instance, ATask> entry : tasks.entrySet()) {
            if (entry.getValue().isChild()) {
                tasks.get(entry.getValue().getParentInstance()).add2Children(entry.getValue());
            } else {
                roots.add(entry.getValue());
            }
        }

        final DateTime startDate = minDate.isBefore(projectDate) ? minDate : projectDate;
        final DateTime endDate = maxDate.isAfter(projectDueDate) ? maxDate : projectDueDate;
        DateTime current = startDate;
        final List<DateTime> dates = new ArrayList<DateTime>();
        while (current.isBefore(endDate)) {
            dates.add(current);
            current = current.plusDays(1);
        }

        final HtmlTable html = new HtmlTable();
        html.table().tr().th(DBProperties.getProperty("org.efaps.esjp.projects.task.GantHtmlTable.Task"),
                        "border: 1px solid grey;", 0, 2);
        int i = 0;
        int j = 0;
        for (final DateTime date : dates) {
            i++;
            if (date.getDayOfWeek() == 7) {
                j++;
                html.th(DBProperties.getFormatedDBProperty("org.efaps.esjp.projects.task.GantHtmlTable.Week", j),
                                "border: 1px solid grey;", i, 0);
                i = 0;
            }
        }
        html.trC().tr();
        for (final DateTime date : dates) {
            html.td(date.toString("dd-MMM"));
        }
        html.trC();
        addRows(_parameter, html, roots, dates, 0);
        add2Table(_parameter, html, roots, dates);
        html.tableC();
        ret.put(ReturnValues.SNIPLETT, html.toString());
        return ret;
    }

    /**
     * @param _parameter    Parameter as passed by the eFasp API
     * @param _html         html to append to
     * @param _tasks        list of tasks
     * @param _dates        list of dates
     * @throws EFapsException on error
     */
    protected void add2Table(final Parameter _parameter,
                           final HtmlTable _html,
                           final List<ATask> _tasks,
                           final List<DateTime> _dates)
        throws EFapsException<commit_msg>- bugFix: missing last column header in Gant Html Table for projects<commit_after>/*
 * Copyright 2003 - 2011 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */


package org.efaps.esjp.projects.task;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.efaps.admin.dbproperty.DBProperties;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Return;
import org.efaps.admin.event.Return.ReturnValues;
import org.efaps.admin.program.esjp.EFapsRevision;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.db.Instance;
import org.efaps.db.InstanceQuery;
import org.efaps.db.MultiPrintQuery;
import org.efaps.db.QueryBuilder;
import org.efaps.db.SelectBuilder;
import org.efaps.esjp.ci.CIProjects;
import org.efaps.esjp.ui.html.HtmlTable;
import org.efaps.util.EFapsException;
import org.joda.time.DateTime;
import org.joda.time.Interval;

/**
 * TODO comment!
 *
 * @author The eFaps Team
 * @version $Id$
 */
@EFapsUUID("5829d1d9-bfb4-430e-93c4-a6e9467ea663")
@EFapsRevision("$Rev$")
public abstract class GantHtmlTable_Base
{
    /**
     * Create a html table that represents the tasks.
     * @param _parameter Parameter as passed by the eFaps API
     * @return new Return
     * @throws EFapsException on error
     */
    public Return getGantHtmlTableFieldValue(final Parameter _parameter)
        throws EFapsException
    {
        final Return ret = new Return();
        final List<Instance> instances = new ArrayList<Instance>();
        if (_parameter.getInstance().getType().isKindOf(CIProjects.ProjectAbstract.getType())) {
            final QueryBuilder queryBldr = new QueryBuilder(CIProjects.TaskAbstract);
            queryBldr.addWhereAttrEqValue(CIProjects.TaskAbstract.ProjectAbstractLink,
                            _parameter.getInstance().getId());
            final InstanceQuery query = queryBldr.getQuery();
            instances.addAll(query.execute());
        } else {
            instances.add(_parameter.getInstance());
        }

        final Map<Instance, ATask> tasks = new HashMap<Instance, ATask>();
        DateTime projectDate = null;
        DateTime projectDueDate = null;
        DateTime maxDate = null;
        DateTime minDate = null;
        final MultiPrintQuery multi = new MultiPrintQuery(instances);
        final SelectBuilder selDate = new SelectBuilder()
            .linkto(CIProjects.TaskAbstract.ProjectAbstractLink)
            .attribute(CIProjects.ProjectAbstract.Date);
        final SelectBuilder selDue = new SelectBuilder()
            .linkto(CIProjects.TaskAbstract.ProjectAbstractLink)
            .attribute(CIProjects.ProjectAbstract.DueDate);
        final SelectBuilder selPar = new SelectBuilder().linkto(CIProjects.TaskAbstract.ParentTaskAbstractLink).oid();
        multi.addSelect(selDate, selDue, selPar);
        multi.addAttribute(CIProjects.TaskAbstract.DateFrom, CIProjects.TaskAbstract.DateUntil,
                        CIProjects.TaskAbstract.Name, CIProjects.TaskAbstract.Description);
        add2MultiPrint(_parameter, multi);
        multi.execute();
        while (multi.next()) {
            final ATask task = getTask(_parameter, multi.getCurrentInstance(),
                            Instance.get(multi.<String>getSelect(selPar)));
            tasks.put(multi.getCurrentInstance(), task);
            final DateTime dateFrom = multi.<DateTime>getAttribute(CIProjects.TaskAbstract.DateFrom);
            final DateTime dateUntil = multi.<DateTime>getAttribute(CIProjects.TaskAbstract.DateUntil);
            task.addAttribute(CIProjects.TaskAbstract.DateFrom.name, dateFrom);
            task.addAttribute(CIProjects.TaskAbstract.DateUntil.name, dateUntil);
            task.addAttribute(CIProjects.TaskAbstract.Name.name,
                            multi.getAttribute(CIProjects.TaskAbstract.Name));
            task.addAttribute(CIProjects.TaskAbstract.Description.name,
                            multi.getAttribute(CIProjects.TaskAbstract.Description));
            add2Attributes(_parameter, task, multi);
            if (projectDate == null) {
                projectDate = multi.<DateTime>getSelect(selDate);
                projectDueDate = multi.<DateTime>getSelect(selDue);
                minDate = projectDate;
                maxDate = projectDueDate;
            }

            if (dateFrom.isBefore(minDate)) {
                minDate = dateFrom;
            }
            if (dateUntil.isAfter(maxDate)) {
                maxDate = dateUntil;
            }
        }
        final List<ATask> roots = new ArrayList<ATask>();
        for (final Entry<Instance, ATask> entry : tasks.entrySet()) {
            if (entry.getValue().isChild()) {
                tasks.get(entry.getValue().getParentInstance()).add2Children(entry.getValue());
            } else {
                roots.add(entry.getValue());
            }
        }

        final DateTime startDate = minDate.isBefore(projectDate) ? minDate : projectDate;
        final DateTime endDate = maxDate.isAfter(projectDueDate) ? maxDate : projectDueDate;
        DateTime current = startDate;
        final List<DateTime> dates = new ArrayList<DateTime>();
        while (current.isBefore(endDate)) {
            dates.add(current);
            current = current.plusDays(1);
        }

        final HtmlTable html = new HtmlTable();
        html.table().tr().th(DBProperties.getProperty("org.efaps.esjp.projects.task.GantHtmlTable.Task"),
                        "border: 1px solid grey;", 0, 2);
        int i = 0;
        int j = 0;
        for (final DateTime date : dates) {
            i++;
            if (date.getDayOfWeek() == 7) {
                j++;
                html.th(DBProperties.getFormatedDBProperty("org.efaps.esjp.projects.task.GantHtmlTable.Week", j),
                                "border: 1px solid grey;", i, 0);
                i = 0;
            }
        }
        if (i > 0) {
            html.th(DBProperties.getFormatedDBProperty("org.efaps.esjp.projects.task.GantHtmlTable.Week", j + 1),
                            "border: 1px solid grey;", i, 0);
        }
        html.trC().tr();
        for (final DateTime date : dates) {
            html.td(date.toString("dd-MMM"));
        }
        html.trC();
        addRows(_parameter, html, roots, dates, 0);
        add2Table(_parameter, html, roots, dates);
        html.tableC();
        ret.put(ReturnValues.SNIPLETT, html.toString());
        return ret;
    }

    /**
     * @param _parameter    Parameter as passed by the eFasp API
     * @param _html         html to append to
     * @param _tasks        list of tasks
     * @param _dates        list of dates
     * @throws EFapsException on error
     */
    protected void add2Table(final Parameter _parameter,
                           final HtmlTable _html,
                           final List<ATask> _tasks,
                           final List<DateTime> _dates)
        throws EFapsException
{'fraction_non_alphanumeric': 0.10233879492600423, 'fraction_numerical': 0.0067389006342494714, 'mean_word_length': 2.428539071347678, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6273985', 'n_tokens_mistral': 4207, 'n_tokens_neox': 4086, 'n_words': 942}