/*
 * 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-2011 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;

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 void setWorkReport(WorkReport workReport) {
        this.workReport = workReport;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateFinish() {
        return dateFinish;
    }

    public void setDateFinish(Date dateFinish) {
        this.dateFinish = dateFinish;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = 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();
    }

    public void setCode(String code) {
        workReport.setCode(code);
    }
}

@@ -201,17 +201,17 @@
 2010-201
-1
+2
  Igalia,
@@ -1086,16 +1086,144 @@
 tLine;%0A%0A
+/**%0A * DTO used to show the list of %7B@link WorkReport WorkReports%7D.%0A *%0A * @author Manuel Rego Casasnovas %3Cmrego@igalia.com%3E%0A */%0A
 public c
@@ -1745,531 +1745,175 @@
 lic 
-void setWorkReport(WorkReport workReport) %7B%0A        this.workReport = workReport;%0A    %7D%0A%0A    public Date getDateStart() %7B%0A        return dateStart;%0A    %7D%0A%0A    public void setDateStart(Date dateStart) %7B%0A        this.dateStart = dateStart;%0A    %7D%0A%0A    public Date getDateFinish() %7B%0A        return dateFinish;%0A    %7D%0A%0A    public void setDateFinish(Date dateFinish) %7B%0A        this.dateFinish = dateFinish;%0A    %7D%0A%0A    public String getType() %7B%0A        return type;%0A    %7D%0A%0A    public void setType(String type) %7B%0A        this.type =
+Date getDateStart() %7B%0A        return dateStart;%0A    %7D%0A%0A    public Date getDateFinish() %7B%0A        return dateFinish;%0A    %7D%0A%0A    public String getType() %7B%0A        return
  typ
@@ -3838,85 +3838,6 @@
  %7D%0A%0A
-    public void setCode(String code) %7B%0A        workReport.setCode(code);%0A    %7D%0A
 %7D%0A

Remove unneeded set methods in WorkReportDTO