<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.
 ******************************************************************************/
// DISPLAY FILTER
// ARRAY
// LIST
// ACCESS
// DATABASE
// ELEMENT HASH

/*
 * Created on Oct 14, 2004
 */
package com.foc.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import com.foc.Globals;
import com.foc.access.AccessSubject;
import com.foc.admin.UserSession;
import com.foc.api.IFocList;
import com.foc.api.IFocObject;
import com.foc.business.company.Company;
import com.foc.business.workflow.WFSite;
import com.foc.business.workflow.implementation.IWorkflowDesc;
import com.foc.business.workflow.implementation.WorkflowDesc;
import com.foc.db.SQLFilter;
import com.foc.desc.FocConstructor;
import com.foc.desc.FocDesc;
import com.foc.desc.FocFieldEnum;
import com.foc.desc.FocObject;
import com.foc.desc.field.FField;
import com.foc.desc.field.FFieldPath;
import com.foc.desc.parsers.ParsedFocDesc;
import com.foc.desc.parsers.join.ParsedJoin;
import com.foc.event.FocEvent;
import com.foc.event.FocListener;
import com.foc.formula.FocSimpleFormulaContext;
import com.foc.formula.Formula;
import com.foc.gui.FAbstractListPanel;
import com.foc.gui.table.FTable;
import com.foc.property.FDate;
import com.foc.property.FInt;
import com.foc.property.FList;
import com.foc.property.FObject;
import com.foc.property.FProperty;
import com.foc.property.FReference;
import com.foc.shared.dataStore.IFocData;
import com.foc.shared.json.B01JsonBuilder;
import com.foc.util.FocMath;
import com.foc.util.IFocIterator;
import com.foc.util.Utils;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;

/**
 * @author 01Barmaja - <NAME>
 */
@SuppressWarnings("serial")
public class FocList extends AccessSubject implements IFocList, Container {
	public static final String FILTER_KEY_FOR_INCREMENTAL_UPDATE = "_INCREMENTAL_UPDATE";
	
  public static final int NONE           = 0;
  public static final int LOAD_IF_NEEDED = 1;
  public static final int FORCE_RELOAD   = 2;
  
  private ConcurrentHashMap<FocObject, FocListElement> elements      = null;
  private ConcurrentHashMap<Long, FocListElement>      elementsByRef = null;
  
  private SQLFilter      filter     = null;
  private FocListGroupBy sqlGroupBy = null;
  private ArrayList<FocListener> listeners = null;
   
  private FAbstractListPanel selectionPanel             = null;
  private FAbstractListPanel attachedListPanel          = null;//Gets the attached list panel 
  private int                viewIDForSelectionBrowse   =    0;
  private long               nextTempReference          =    1;
  private int                nextOrder                  =    0;
  private FocListOrder       listOrder                  = null;//Priority 1
  private Comparator         orderComparator            = null;//Priority 2 - This one for external unser defined orders
  private FocListListener    focListListener            = null; 
  private FObject            selectionProperty          = null;
  private FocList            listRequestingTheSelection = null;

  private char              flags                                    = 0;
  private static final char FLG_WITH_DEPRICATION                     = 1;
  private static final char FLG_SLEEP_LISTENERS                      = 2;
  private static final char FLG_LOADED                               = 4;
  private static final char FLG_DIRECT_IMPACT_ON_DATABASE            = 8;
  private static final char FLG_DIRECTLY_EDITABLE                    = 16;
  private static final char FLG_WAIT_FOR_VALIDATION_TO_ADD           = 32;
  private static final char FLG_KEEP_NEW_LINE_FOCUS_UNTIL_VALIDATION = 64;
  private static final char FLG_COLLECTION_BEHAVIOUR                 = 128;
  private static final char FLG_DISABLE_RESORT_AFTER_ADD             = 256;
  private static final char FLG_IS_LOADING                           = 512;
  private static final char FLG_DISABLE_SORT                         = 1024;
  private static final char FLG_AUTO_REFRESH                         = 2048;
  
  // Image of the real list.
  // It is set to null when modified
  // and constructed automatically when needed.
  private ArrayList<FocListElement> arrayList = null;
  private ReferencesCollection      referencesCollection_Vaadin = null;//ForVaadin implementation

  private FocObject masterObject = null;
  private HashMap<Integer, FocObject> foreignObjectsMap = null;//THE KEYS ARE THE FIELD IDS IN THE SLAVE DESC
  private FocLink   focLink        = null;
  private FList     fatherProperty = null;// When the FocList is in a property this
                                      // is used to notify the proprty of
                                      // modifications

  private Object validityCheckObject = null;
  
  private long    lastLoadTime         = 0;
  private boolean checkIfRecentEnough  = false;
  public static final long EXPIRY_TIME = 1 * 60 * 1000; 
  
  // oooooooooooooooooooooooooooooooooo
  // MAIN
  // oooooooooooooooooooooooooooooooooo
  // oooooooooooooooooooooooooooooooooo
  
  public synchronized void init(FocObject masterObject, FocLink focLink, SQLFilter filter) {
    this.focLink = focLink;
    this.masterObject = masterObject;
    this.filter = filter;

    if(this.filter == null){
      this.filter = new SQLFilter(null, 0);
    }

    this.focLink.adaptSQLFilter(this, this.filter);

    setWaitForValidationToAddObject(true);
    setKeepNewLineFocusUntilValidation(true);
    
    addLogicalDeleteFilter();
    if(getListOrder() == null && getFocDesc() != null && getFocDesc().hasOrderField()){
    	FocListOrder order = new FocListOrder(FField.FLD_ORDER);
    	setListOrder(order);
    }
  }

  private synchronized void initForForeignKeyLink(HashMap<Integer, FocObject> foreignObjectsMap, FocLinkForeignKey focLinkForeignKey, SQLFilter filter){
    setForeignObjectsMap(foreignObjectsMap);
    if(this.foreignObjectsMap.size() == 1){
      Iterator iter = this.foreignObjectsMap.keySet().iterator();
      if(iter != null && iter.hasNext()){
        int key = (Integer)iter.next();
        FocObject uniqueForeignObject = foreignObjectsMap.get(key);
        uniqueForeignObject.setTransactionalWithChildren(focLinkForeignKey.isTransactionalWithChildren());
        setFatherSubject(uniqueForeignObject);
      }
    }
    init(null,focLinkForeignKey,filter);
  }

  /**
   * This constructor is mainly for simple lists used to store some objects We
   * do not need any link or filter, neether a master object
   */
  public FocList(FocLink focLink) {
    super(Globals.getDefaultAccessControl());
    init(null, focLink, null);
  }

  public FocList(FocObject masterObject, FocLink focLink, SQLFilter filter) {
    super(Globals.getDefaultAccessControl());
    if(FocLinkForeignKey.class.isInstance(focLink)){
      HashMap<Integer, FocObject> foreignMap = new HashMap<Integer, FocObject>();
      FocLinkForeignKey focLinkFK = (FocLinkForeignKey) focLink;
      int key = focLinkFK.getUniqueForeignKeyFieldID();<commit_msg>Helper method in FocList to create FocList with parameter FocDesc<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.
 ******************************************************************************/
// DISPLAY FILTER
// ARRAY
// LIST
// ACCESS
// DATABASE
// ELEMENT HASH

/*
 * Created on Oct 14, 2004
 */
package com.foc.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import com.foc.Globals;
import com.foc.access.AccessSubject;
import com.foc.admin.UserSession;
import com.foc.api.IFocList;
import com.foc.api.IFocObject;
import com.foc.business.company.Company;
import com.foc.business.workflow.WFSite;
import com.foc.business.workflow.implementation.IWorkflowDesc;
import com.foc.business.workflow.implementation.WorkflowDesc;
import com.foc.db.SQLFilter;
import com.foc.desc.FocConstructor;
import com.foc.desc.FocDesc;
import com.foc.desc.FocFieldEnum;
import com.foc.desc.FocObject;
import com.foc.desc.field.FField;
import com.foc.desc.field.FFieldPath;
import com.foc.desc.parsers.ParsedFocDesc;
import com.foc.desc.parsers.join.ParsedJoin;
import com.foc.event.FocEvent;
import com.foc.event.FocListener;
import com.foc.formula.FocSimpleFormulaContext;
import com.foc.formula.Formula;
import com.foc.gui.FAbstractListPanel;
import com.foc.gui.table.FTable;
import com.foc.property.FDate;
import com.foc.property.FInt;
import com.foc.property.FList;
import com.foc.property.FObject;
import com.foc.property.FProperty;
import com.foc.property.FReference;
import com.foc.shared.dataStore.IFocData;
import com.foc.shared.json.B01JsonBuilder;
import com.foc.util.FocMath;
import com.foc.util.IFocIterator;
import com.foc.util.Utils;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;

/**
 * @author 01Barmaja - <NAME>
 */
@SuppressWarnings("serial")
public class FocList extends AccessSubject implements IFocList, Container {
	public static final String FILTER_KEY_FOR_INCREMENTAL_UPDATE = "_INCREMENTAL_UPDATE";
	
  public static final int NONE           = 0;
  public static final int LOAD_IF_NEEDED = 1;
  public static final int FORCE_RELOAD   = 2;
  
  private ConcurrentHashMap<FocObject, FocListElement> elements      = null;
  private ConcurrentHashMap<Long, FocListElement>      elementsByRef = null;
  
  private SQLFilter      filter     = null;
  private FocListGroupBy sqlGroupBy = null;
  private ArrayList<FocListener> listeners = null;
   
  private FAbstractListPanel selectionPanel             = null;
  private FAbstractListPanel attachedListPanel          = null;//Gets the attached list panel 
  private int                viewIDForSelectionBrowse   =    0;
  private long               nextTempReference          =    1;
  private int                nextOrder                  =    0;
  private FocListOrder       listOrder                  = null;//Priority 1
  private Comparator         orderComparator            = null;//Priority 2 - This one for external unser defined orders
  private FocListListener    focListListener            = null; 
  private FObject            selectionProperty          = null;
  private FocList            listRequestingTheSelection = null;

  private char              flags                                    = 0;
  private static final char FLG_WITH_DEPRICATION                     = 1;
  private static final char FLG_SLEEP_LISTENERS                      = 2;
  private static final char FLG_LOADED                               = 4;
  private static final char FLG_DIRECT_IMPACT_ON_DATABASE            = 8;
  private static final char FLG_DIRECTLY_EDITABLE                    = 16;
  private static final char FLG_WAIT_FOR_VALIDATION_TO_ADD           = 32;
  private static final char FLG_KEEP_NEW_LINE_FOCUS_UNTIL_VALIDATION = 64;
  private static final char FLG_COLLECTION_BEHAVIOUR                 = 128;
  private static final char FLG_DISABLE_RESORT_AFTER_ADD             = 256;
  private static final char FLG_IS_LOADING                           = 512;
  private static final char FLG_DISABLE_SORT                         = 1024;
  private static final char FLG_AUTO_REFRESH                         = 2048;
  
  // Image of the real list.
  // It is set to null when modified
  // and constructed automatically when needed.
  private ArrayList<FocListElement> arrayList = null;
  private ReferencesCollection      referencesCollection_Vaadin = null;//ForVaadin implementation

  private FocObject masterObject = null;
  private HashMap<Integer, FocObject> foreignObjectsMap = null;//THE KEYS ARE THE FIELD IDS IN THE SLAVE DESC
  private FocLink   focLink        = null;
  private FList     fatherProperty = null;// When the FocList is in a property this
                                      // is used to notify the proprty of
                                      // modifications

  private Object validityCheckObject = null;
  
  private long    lastLoadTime         = 0;
  private boolean checkIfRecentEnough  = false;
  public static final long EXPIRY_TIME = 1 * 60 * 1000; 
  
  // oooooooooooooooooooooooooooooooooo
  // MAIN
  // oooooooooooooooooooooooooooooooooo
  // oooooooooooooooooooooooooooooooooo
  
  public synchronized void init(FocObject masterObject, FocLink focLink, SQLFilter filter) {
    this.focLink = focLink;
    this.masterObject = masterObject;
    this.filter = filter;

    if(this.filter == null){
      this.filter = new SQLFilter(null, 0);
    }

    this.focLink.adaptSQLFilter(this, this.filter);

    setWaitForValidationToAddObject(true);
    setKeepNewLineFocusUntilValidation(true);
    
    addLogicalDeleteFilter();
    if(getListOrder() == null && getFocDesc() != null && getFocDesc().hasOrderField()){
    	FocListOrder order = new FocListOrder(FField.FLD_ORDER);
    	setListOrder(order);
    }
  }

  private synchronized void initForForeignKeyLink(HashMap<Integer, FocObject> foreignObjectsMap, FocLinkForeignKey focLinkForeignKey, SQLFilter filter){
    setForeignObjectsMap(foreignObjectsMap);
    if(this.foreignObjectsMap.size() == 1){
      Iterator iter = this.foreignObjectsMap.keySet().iterator();
      if(iter != null && iter.hasNext()){
        int key = (Integer)iter.next();
        FocObject uniqueForeignObject = foreignObjectsMap.get(key);
        uniqueForeignObject.setTransactionalWithChildren(focLinkForeignKey.isTransactionalWithChildren());
        setFatherSubject(uniqueForeignObject);
      }
    }
    init(null,focLinkForeignKey,filter);
  }

  public FocList(FocDesc focDesc) {
  	this(new FocLinkSimple(focDesc));
  }
  
  /**
   * This constructor is mainly for simple lists used to store some objects We
   * do not need any link or filter, neether a master object
   */
  public FocList(FocLink focLink) {
    super(Globals.getDefaultAccessControl());
    init(null, focLink, null);
  }

  public FocList(FocObject masterObject, FocLink focLink, SQLFilter filter) {
    super(Globals.getDefaultAccessControl());
    if(FocLinkForeignKey.class.isInstance(focLink)){
      HashMap<Integer, FocObject> foreignMap = new HashMap<Integer, FocObject>();
      FocLinkForeignKey focLinkFK = (FocLinkForeignKey) focLink;
      int key = focLinkFK.getUniqueForeignKeyFieldID();
{'fraction_non_alphanumeric': 0.09692716236722307, 'fraction_numerical': 0.008093070308548306, 'mean_word_length': 3.1966038737065534, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2249237', 'n_tokens_mistral': 4769, 'n_tokens_neox': 4547, 'n_words': 1179}