/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolóxico de Galicia
 * Copyright (C) 2010-2012 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.web.workreports;

import java.util.Date;

import org.libreplan.business.workreports.entities.WorkReport;
import org.libreplan.business.workreports.entities.WorkReportLine;

/**
 * DTO used to show the list of {@link WorkReport WorkReports}.
 *
 * @author Manuel Rego Casasnovas <mrego@igalia.com>
 */
public class WorkReportDTO {

    public WorkReportDTO(WorkReport workReport) {
        this.workReport = workReport;
        this.dateStart = this.getDateStartWorkReport(workReport);
        this.dateFinish = this.getDateFinishWorkReport(workReport);
        this.type = workReport.getWorkReportType().getName();
    }

    private WorkReport workReport;

    private Date dateStart;

    private Date dateFinish;

    private String type;

    public WorkReport getWorkReport() {
        return workReport;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public Date getDateFinish() {
        return dateFinish;
    }

    public String getType() {
        return type;
    }

    public Date getDateStartWorkReport(WorkReport workReport) {
        if (workReport != null) {

            if (workReport.getWorkReportType().getDateIsSharedByLines()) {
                return workReport.getDate();
            }

            // find the start date in its lines
            Date dateStart = null;
            if (workReport.getWorkReportLines().size() > 0) {
                WorkReportLine line0 = (WorkReportLine) workReport
                    .getWorkReportLines().toArray()[0];
                dateStart = line0.getDate();
                for (WorkReportLine line : workReport.getWorkReportLines()) {
                     if ((dateStart != null) && (line.getDate() != null)
                            && (line.getDate().before(dateStart))) {
                        dateStart = line.getDate();
                    }
                }
            }
            return dateStart;
        }
        return null;
    }

    public Date getDateFinishWorkReport(WorkReport workReport) {
        if (workReport != null) {
            if (workReport.getWorkReportType().getDateIsSharedByLines()) {
                return workReport.getDate();
            }

            Date dateFinish = null;
            if (workReport.getWorkReportLines().size() > 0) {
                WorkReportLine line0 = (WorkReportLine) workReport
                    .getWorkReportLines().toArray()[0];
                dateFinish = line0.getDate();
                for (WorkReportLine line : workReport.getWorkReportLines()) {
                    if ((dateFinish != null) && (line.getDate() != null)
                            && (line.getDate().after(dateFinish))) {
                        dateFinish = line.getDate();
                    }
                }
            }
            return dateFinish;
        }
        return null;
    }

    public String getCode() {
        return workReport.getCode();
    }

}

@@ -1084,16 +1084,83 @@
 ortLine;
+%0Aimport org.libreplan.business.workreports.entities.WorkReportType;
 %0A%0A/**%0A *
@@ -1529,32 +1529,33 @@
 orkReport);%0A
+%0A
         
 this.type = 
@@ -1546,14 +1546,34 @@
     
-this.t
+WorkReportType workReportT
 ype 
@@ -1608,19 +1608,202 @@
 pe()
-.getName();
+;%0A        this.type = workReportType.getName();%0A        if (workReportType.isMonthlyTimesheetsType()) %7B%0A            this.type += %22 - %22 + workReport.getResource().getShortDescription();%0A        %7D
 %0A   

Add info about resource in monthly timesheets in work reports list