<commit_before>/*******************************************************************************
 * Copyright 2016 <NAME>
 * 
 * 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.
 ******************************************************************************/
//IMPLEMENTED

/*
 * Created on Sep 9, 2005
 */
package com.foc.list.filter;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Date;
import java.text.SimpleDateFormat;

import com.foc.ConfigInfo;
import com.foc.Globals;
import com.foc.business.dateShifter.DateShifterDesc;
import com.foc.db.DBManager;
import com.foc.desc.FocDesc;
import com.foc.desc.FocObject;
import com.foc.desc.field.FDateField;
import com.foc.desc.field.FField;
import com.foc.desc.field.FFieldPath;
import com.foc.desc.field.FMultipleChoiceField;
import com.foc.gui.FGComboBox;
import com.foc.gui.FGLabel;
import com.foc.gui.FPanel;
import com.foc.property.FDate;
import com.foc.property.FInt;
import com.foc.property.FMultipleChoice;
import com.foc.property.FProperty;
import com.foc.property.FPropertyListener;

/**
 * @author 01Barmaja
 */
public class DateCondition extends FilterCondition {
  private static final int FLD_FIRST_DATE = 1;
  private static final int FLD_LAST_DATE = 2;
  private static final int FLD_OPERATOR = 3;

  //Make the constansts public and add operator_equals
  /*private static final int OPERATOR_BETWEEN = 0;
  private static final int OPERATOR_GREATER_THAN = 1;
  private static final int OPERATOR_LESS_THAN = 2;*/
  public static final int OPERATOR_BETWEEN = 0;
  public static final int OPERATOR_GREATER_THAN = 1;
  public static final int OPERATOR_LESS_THAN = 2;
  public static final int OPERATOR_EQUALS = 3;
  public static final int OPERATOR_INDIFERENT= 4;
  
  private DateShifterDesc firstDateShifterDesc = null;
  private DateShifterDesc lastDateShifterDesc  = null;
  
  public DateCondition(FFieldPath dateFieldPath, String fieldPrefix){
    super(dateFieldPath, fieldPrefix);
  }
  
  public DateCondition(int fieldID){
  	super(fieldID);
  }

  public java.sql.Date getFirstDate(FocListFilter filter){
    FDate prop = (FDate) filter.getFocProperty(getFirstFieldID() + FLD_FIRST_DATE);
    return prop.getDate();
  }

  public java.sql.Date getLastDate(FocListFilter filter){
    FDate prop = (FDate) filter.getFocProperty(getFirstFieldID() + FLD_LAST_DATE);
    return prop.getDate();
  }

  public int getOperator(FocListFilter filter){
    FMultipleChoice prop = (FMultipleChoice) filter.getFocProperty(getFirstFieldID() + FLD_OPERATOR);
    return prop.getInteger();
  }
  
  public void setOperator(FocListFilter filter , int op){
    FInt operator = (FInt)filter.getFocProperty(getFirstFieldID() + FLD_OPERATOR);
    if (operator != null){
      operator.setInteger(op);
    }
  }
  
  public void setFirstDate(FocListFilter filter ,Date d){
    FDate date = (FDate)filter.getFocProperty(getFirstFieldID() + FLD_FIRST_DATE);
    if (date != null){
      date.setDate(d);
    }
  }
  
  public void setLastDate(FocListFilter filter ,Date d){
    FDate date = (FDate)filter.getFocProperty(getFirstFieldID() + FLD_LAST_DATE);
    if (date != null){
      date.setDate(d);
    }
  }
  
  // oooooooooooooooooooooooooooooooooo
  // oooooooooooooooooooooooooooooooooo
  // IMPLEMENTED
  // oooooooooooooooooooooooooooooooooo
  // oooooooooooooooooooooooooooooooooo
  
  public void fillProperties(FocObject focFatherObject){
  	new FMultipleChoice(focFatherObject, getFirstFieldID() + FLD_OPERATOR, OPERATOR_GREATER_THAN);
    new FDate(focFatherObject, getFirstFieldID() + FLD_FIRST_DATE, new Date(0));
    new FDate(focFatherObject, getFirstFieldID() + FLD_LAST_DATE, new Date(0));
  }
  
	@Override
	public void copyCondition(FocObject tarObject, FocObject srcObject, FilterCondition srcCondition) {
	  copyProperty(tarObject, getFirstFieldID() + FLD_OPERATOR  , srcObject, srcCondition.getFirstFieldID() + FLD_OPERATOR);
	  copyProperty(tarObject, getFirstFieldID() + FLD_FIRST_DATE, srcObject, srcCondition.getFirstFieldID() + FLD_FIRST_DATE);
	  copyProperty(tarObject, getFirstFieldID() + FLD_LAST_DATE , srcObject, srcCondition.getFirstFieldID() + FLD_LAST_DATE);
	}

  public boolean includeObject(FocListFilter filter, FocObject object){
    boolean include = true;
    
    FDate dateProp = (FDate) getFieldPath().getPropertyFromObject(object);
    Date date = dateProp.getDate(); 
    Date firstDate = getFirstDate(filter);
    Date lastDate = getLastDate(filter);
    
    int op = getOperator(filter);
    
    if(op != OPERATOR_INDIFERENT){
      if(!FDate.isEmpty(firstDate) && (op == OPERATOR_BETWEEN || op == OPERATOR_GREATER_THAN)){
        include = date.after(firstDate) || date.equals(firstDate);
      }
  
      if(include && !FDate.isEmpty(lastDate) && (op == OPERATOR_BETWEEN || op == OPERATOR_LESS_THAN)){
        include = date.before(lastDate) || date.equals(lastDate);
      }

      if(include && !FDate.isEmpty(firstDate) && (op == OPERATOR_EQUALS)){
        include = date.equals(firstDate);
      }
    }
    
    return include;
  }
  
  @Override
  public void forceFocObjectToConditionValueIfNeeded(FocListFilter filter, FocObject focObject) {
    if(getOperator(filter) == OPERATOR_EQUALS){
      FDate fDate = (FDate) getFieldPath().getPropertyFromObject(focObject);
      if(fDate != null){
        fDate.setDate(getFirstDate(filter));
      }
    }
  }

  public static int adjustTheOperation(int op, Date firstDate, Date lastDate) {
    if (op != OPERATOR_INDIFERENT) {
    	if(op == OPERATOR_BETWEEN && firstDate.getTime() < Globals.DAY_TIME) op = OPERATOR_LESS_THAN;
    	if(op == OPERATOR_BETWEEN && lastDate.getTime() < Globals.DAY_TIME) op = OPERATOR_GREATER_THAN;
	    if(op == OPERATOR_GREATER_THAN && firstDate.getTime() < Globals.DAY_TIME) op = OPERATOR_INDIFERENT;
	    if(op == OPERATOR_LESS_THAN && lastDate.getTime() < Globals.DAY_TIME) op = OPERATOR_INDIFERENT;
    }
    return op;
  }
  
  public static StringBuffer buildSQLWhere(int provider, String fieldName, int op, Date firstDate, Date lastDate) {
    //b01.foc.Globals.logString("Condition sql build not implemented yet");
    StringBuffer buffer = null;
    //Date firstDate = getFirstDate(filter);
    //Date lastDate = getLastDate(filter);
    //int op = getOperator(filter);
    
    buffer = new StringBuffer();
    
//    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
//    if(Globals.getDBManager().getProvider() == DBManager.PROVIDER_ORACLE){
//    	
//    }
    String firstDateFormat = "";
    String lastDateFormat  = "";
    if (provider == DBManager.PROVIDER_ORACLE) {
    	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
      firstDateFormat = dateFormat.format(firstDate);<commit_msg>Allow conditions on dates before 1/1/1970<commit_after>/*******************************************************************************
 * Copyright 2016 <NAME>
 * 
 * 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.
 ******************************************************************************/
//IMPLEMENTED

/*
 * Created on Sep 9, 2005
 */
package com.foc.list.filter;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Date;
import java.text.SimpleDateFormat;

import com.foc.ConfigInfo;
import com.foc.Globals;
import com.foc.business.dateShifter.DateShifterDesc;
import com.foc.db.DBManager;
import com.foc.desc.FocDesc;
import com.foc.desc.FocObject;
import com.foc.desc.field.FDateField;
import com.foc.desc.field.FField;
import com.foc.desc.field.FFieldPath;
import com.foc.desc.field.FMultipleChoiceField;
import com.foc.gui.FGComboBox;
import com.foc.gui.FGLabel;
import com.foc.gui.FPanel;
import com.foc.property.FDate;
import com.foc.property.FInt;
import com.foc.property.FMultipleChoice;
import com.foc.property.FProperty;
import com.foc.property.FPropertyListener;

/**
 * @author 01Barmaja
 */
public class DateCondition extends FilterCondition {
  private static final int FLD_FIRST_DATE = 1;
  private static final int FLD_LAST_DATE = 2;
  private static final int FLD_OPERATOR = 3;

  //Make the constansts public and add operator_equals
  /*private static final int OPERATOR_BETWEEN = 0;
  private static final int OPERATOR_GREATER_THAN = 1;
  private static final int OPERATOR_LESS_THAN = 2;*/
  public static final int OPERATOR_BETWEEN = 0;
  public static final int OPERATOR_GREATER_THAN = 1;
  public static final int OPERATOR_LESS_THAN = 2;
  public static final int OPERATOR_EQUALS = 3;
  public static final int OPERATOR_INDIFERENT= 4;
  
  private DateShifterDesc firstDateShifterDesc = null;
  private DateShifterDesc lastDateShifterDesc  = null;
  
  public DateCondition(FFieldPath dateFieldPath, String fieldPrefix){
    super(dateFieldPath, fieldPrefix);
  }
  
  public DateCondition(int fieldID){
  	super(fieldID);
  }

  public java.sql.Date getFirstDate(FocListFilter filter){
    FDate prop = (FDate) filter.getFocProperty(getFirstFieldID() + FLD_FIRST_DATE);
    return prop.getDate();
  }

  public java.sql.Date getLastDate(FocListFilter filter){
    FDate prop = (FDate) filter.getFocProperty(getFirstFieldID() + FLD_LAST_DATE);
    return prop.getDate();
  }

  public int getOperator(FocListFilter filter){
    FMultipleChoice prop = (FMultipleChoice) filter.getFocProperty(getFirstFieldID() + FLD_OPERATOR);
    return prop.getInteger();
  }
  
  public void setOperator(FocListFilter filter , int op){
    FInt operator = (FInt)filter.getFocProperty(getFirstFieldID() + FLD_OPERATOR);
    if (operator != null){
      operator.setInteger(op);
    }
  }
  
  public void setFirstDate(FocListFilter filter ,Date d){
    FDate date = (FDate)filter.getFocProperty(getFirstFieldID() + FLD_FIRST_DATE);
    if (date != null){
      date.setDate(d);
    }
  }
  
  public void setLastDate(FocListFilter filter ,Date d){
    FDate date = (FDate)filter.getFocProperty(getFirstFieldID() + FLD_LAST_DATE);
    if (date != null){
      date.setDate(d);
    }
  }
  
  // oooooooooooooooooooooooooooooooooo
  // oooooooooooooooooooooooooooooooooo
  // IMPLEMENTED
  // oooooooooooooooooooooooooooooooooo
  // oooooooooooooooooooooooooooooooooo
  
  public void fillProperties(FocObject focFatherObject){
  	new FMultipleChoice(focFatherObject, getFirstFieldID() + FLD_OPERATOR, OPERATOR_GREATER_THAN);
    new FDate(focFatherObject, getFirstFieldID() + FLD_FIRST_DATE, new Date(0));
    new FDate(focFatherObject, getFirstFieldID() + FLD_LAST_DATE, new Date(0));
  }
  
	@Override
	public void copyCondition(FocObject tarObject, FocObject srcObject, FilterCondition srcCondition) {
	  copyProperty(tarObject, getFirstFieldID() + FLD_OPERATOR  , srcObject, srcCondition.getFirstFieldID() + FLD_OPERATOR);
	  copyProperty(tarObject, getFirstFieldID() + FLD_FIRST_DATE, srcObject, srcCondition.getFirstFieldID() + FLD_FIRST_DATE);
	  copyProperty(tarObject, getFirstFieldID() + FLD_LAST_DATE , srcObject, srcCondition.getFirstFieldID() + FLD_LAST_DATE);
	}

  public boolean includeObject(FocListFilter filter, FocObject object){
    boolean include = true;
    
    FDate dateProp = (FDate) getFieldPath().getPropertyFromObject(object);
    Date date = dateProp.getDate(); 
    Date firstDate = getFirstDate(filter);
    Date lastDate = getLastDate(filter);
    
    int op = getOperator(filter);
    
    if(op != OPERATOR_INDIFERENT){
      if(!FDate.isEmpty(firstDate) && (op == OPERATOR_BETWEEN || op == OPERATOR_GREATER_THAN)){
        include = date.after(firstDate) || date.equals(firstDate);
      }
  
      if(include && !FDate.isEmpty(lastDate) && (op == OPERATOR_BETWEEN || op == OPERATOR_LESS_THAN)){
        include = date.before(lastDate) || date.equals(lastDate);
      }

      if(include && !FDate.isEmpty(firstDate) && (op == OPERATOR_EQUALS)){
        include = date.equals(firstDate);
      }
    }
    
    return include;
  }
  
  @Override
  public void forceFocObjectToConditionValueIfNeeded(FocListFilter filter, FocObject focObject) {
    if(getOperator(filter) == OPERATOR_EQUALS){
      FDate fDate = (FDate) getFieldPath().getPropertyFromObject(focObject);
      if(fDate != null){
        fDate.setDate(getFirstDate(filter));
      }
    }
  }

  public static int adjustTheOperation(int op, Date firstDate, Date lastDate) {
    if (op != OPERATOR_INDIFERENT) {
    	if(op == OPERATOR_BETWEEN && firstDate.getTime() < Globals.DAY_TIME && firstDate.getTime() > -Globals.DAY_TIME) op = OPERATOR_LESS_THAN;
    	if(op == OPERATOR_BETWEEN && lastDate.getTime() < Globals.DAY_TIME && lastDate.getTime() > -Globals.DAY_TIME) op = OPERATOR_GREATER_THAN;
	    if(op == OPERATOR_GREATER_THAN && firstDate.getTime() < Globals.DAY_TIME && firstDate.getTime() > -Globals.DAY_TIME) op = OPERATOR_INDIFERENT;
	    if(op == OPERATOR_LESS_THAN && lastDate.getTime() < Globals.DAY_TIME && lastDate.getTime() > -Globals.DAY_TIME) op = OPERATOR_INDIFERENT;
    }
    return op;
  }
  
  public static StringBuffer buildSQLWhere(int provider, String fieldName, int op, Date firstDate, Date lastDate) {
    //b01.foc.Globals.logString("Condition sql build not implemented yet");
    StringBuffer buffer = null;
    //Date firstDate = getFirstDate(filter);
    //Date lastDate = getLastDate(filter);
    //int op = getOperator(filter);
    
    buffer = new StringBuffer();
    
//    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
//    if(Globals.getDBManager().getProvider() == DBManager.PROVIDER_ORACLE){
//    	
//    }
    String firstDateFormat = "";
    String lastDateFormat  = "";
    if (provider == DBManager.PROVIDER_ORACLE) {
    	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
      firstDateFormat = dateFormat.format(firstDate);
{'fraction_non_alphanumeric': 0.12351991388589882, 'fraction_numerical': 0.004440258342303553, 'mean_word_length': 4.308928571428571, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, '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': '4034197', 'n_tokens_mistral': 4938, 'n_tokens_neox': 4791, 'n_words': 1060}