/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

package ipipan.poliqarp.gui;

import java.util.*;

public class DateConstraintModel extends MetaDataConstraintModel
{
   private boolean from, to; 
   private Date dateFrom, dateTo;
   
   public DateConstraintModel(String name)
   {
      super(name);
      from = to = false;
      dateFrom = new GregorianCalendar(1801, 0, 1).getTime();
      dateTo = new GregorianCalendar(2099, 11, 31).getTime();
   }

   public boolean getFrom()
   {
      return from;
   }

   public boolean getTo()
   {
      return to;
   }

   public void setFromTo(boolean from, boolean to)
   {
      this.from = from;
      this.to = to;
   }

   public Date getDateFrom()
   {
      return dateFrom;
   }

   public Date getDateTo()
   {
      return dateTo;
   }

   public void setDateFrom(Date date)
   {
      dateFrom = date;
   }

   public void setDateTo(Date date)
   {
      dateTo = date;
   }

   public String getQueryConstraint() 
   {
      if (!(from || to))
         return null;
      StringBuilder res = new StringBuilder("(");
      Calendar c = new GregorianCalendar();
      if (from) {
         res.append(name);
         res.append(">");
         c.setTime(dateFrom);
         int day = c.get(c.DATE);
         int month = c.get(c.MONTH) + 1;
         int year = c.get(c.YEAR);
         res.append(year);
         res.append("-");
         res.append(month);
         res.append("-");
         res.append(day);
      }
      if (from && to)
         res.append(" & ");
      if (to) {
         res.append(name);
         res.append("<");
         c.setTime(dateTo);
         int day = c.get(c.DATE);
         int month = c.get(c.MONTH) + 1;
         int year = c.get(c.YEAR);
         res.append(year);
         res.append("-");
         res.append(month);
         res.append("-");
         res.append(day);
      }
      res.append(")");
      return res.toString();
   }
   
   public MetaDataConstraint getWidget()
   {
      return new DateConstraint(this);
   }
}
