private void getRecursiveItemsFlattened(
     ArrayList<String> names, ArrayList<Long> ids, MetaUnitEntityItem ite, String shift) {
   if (ite.getSubitems() != null && ite.getSubitems().size() > 0)
     for (MetaUnitEntityItem item : ite.getSubitems()) {
       names.add(shift + item.getValue());
       ids.add(item.getId());
       getRecursiveItemsFlattened(names, ids, item, ". " + shift);
     }
 }
 public ArrayList<Long> union(ArrayList<Long> first, ArrayList<Long> second) {
   ArrayList<Long> first_cl = new ArrayList<Long>();
   for (Long id : first) {
     first_cl.add(id);
   }
   for (Long id : second) {
     if (!first_cl.contains(id)) first_cl.add(id);
   }
   return first_cl;
 }
 private ArrayList<MetaUnitEntityItemDTO> toEntitySubitemsDTOs(MetaUnitEntityItem item) {
   ArrayList<MetaUnitEntityItemDTO> subitems = new ArrayList<MetaUnitEntityItemDTO>();
   if (item.getSubitems() != null)
     for (MetaUnitEntityItem it : item.getSubitems()) {
       subitems.add(toEntityItemDTO_Light(it));
     }
   return subitems;
 }
 @Override
 public ArrayList<Long> getEntityItemSubitemsIDs(Long id_item) {
   MetaUnitEntityItem item = em.find(MetaUnitEntityItem.class, id_item);
   Collection<MetaUnitEntityItem> items = item.getSubitems();
   ArrayList<Long> arr = new ArrayList<Long>();
   for (MetaUnitEntityItem it : items) {
     arr.add(it.getId());
   }
   return arr;
 }
 @Override
 public ArrayList<String> getEntityItemTaggedEntitiesIdentifiers(Long id_item) {
   MetaUnitEntityItem item = em.find(MetaUnitEntityItem.class, id_item);
   List<String> ids = item.getTagged_entities_identifiers();
   ArrayList<String> arr = new ArrayList<String>();
   if (ids != null)
     for (String id : ids) {
       arr.add(id);
     }
   return arr;
 }
 @Override
 public ArrayList<VarDTO_Detailed> getVarDTOsAsOrdered(ArrayList<Long> ids) {
   ArrayList<VarDTO_Detailed> arr = new ArrayList<VarDTO_Detailed>();
   if (ids != null)
     for (Long key : ids) {
       VarDTO_Detailed dto = getVarDetailed(key, null, null);
       if (dto != null) arr.add(dto);
     }
   return arr;
   // return Var.getResearchVarsLightDTOsUnordered(em, ids);
 }
  private MetaUnitEntityItemDTO toEntityItemDTO_Light(MetaUnitEntityItem item) {
    MetaUnitEntityItemDTO dto =
        new MetaUnitEntityItemDTO(item.getId(), item.getValue(), item.getMapped_values());
    ArrayList<Long> ent_ids = new ArrayList<Long>();
    if (item.getTagged_entities_ids() != null)
      for (Long id : item.getTagged_entities_ids()) {
        ent_ids.add(id);
      }

    ArrayList<String> ent_idents = new ArrayList<String>();
    if (item.getTagged_entities_identifiers() != null)
      for (String ident : item.getTagged_entities_identifiers()) {
        ent_idents.add(ident);
      }
    dto.setTagged_entities_ids(ent_ids);
    dto.setTagged_entities_identifiers(ent_idents);

    ArrayList<MetaUnitEntityItemDTO> subitems = new ArrayList<MetaUnitEntityItemDTO>();
    dto.setSubitems(subitems);
    return dto;
  }
 public ArrayList<Long> intersection(ArrayList<Long> first, ArrayList<Long> second) {
   ArrayList<Long> first_cl = new ArrayList<Long>();
   for (Long id : second) {
     for (Long id_f : first) {
       if (id_f.equals(id)) {
         first_cl.add(id_f);
         break;
       }
     }
   }
   return first_cl;
 }
 @Override
 public ArrayList<Long> getEntityItemTaggedEntitiesIDs(Long id_item, String identif) {
   MetaUnitEntityItem item = em.find(MetaUnitEntityItem.class, id_item);
   List<Long> ids = item.getTagged_entities_ids();
   List<String> idents = item.getTagged_entities_identifiers();
   ArrayList<Long> arr = new ArrayList<Long>();
   int i = 0;
   if (ids != null)
     for (Long id : ids) {
       if (idents.get(i).equals(identif)) arr.add(id);
       i++;
     }
   return arr;
 }
 @Override
 public ArrayList<OrgDTO> getOrgList() {
   ArrayList<OrgDTO> list = new ArrayList<OrgDTO>();
   try {
     TypedQuery<Organization> q =
         em.createQuery("SELECT x FROM Organization x", Organization.class);
     List<Organization> res = (List<Organization>) q.getResultList();
     for (Organization org : res) {
       list.add(org.toDTO());
     }
   } finally {
   }
   return list;
 }
 @Override
 public ArrayList<VarDTO_Research> getVarsResearchNames(ArrayList<Long> keys) {
   ArrayList<VarDTO_Research> map = new ArrayList<VarDTO_Research>();
   for (Long k : keys) {
     Var v = em.find(Var.class, k);
     if (v != null) {
       v.setEM(em);
       VarDTO_Detailed dto = v.toDTO_Detailed(null, null, em);
       map.add(
           new VarDTO_Research(
               dto.getId(), dto.getResearch_id().intValue(), dto.getResearch_name()));
     }
   }
   return map;
 }
 public ArrayList<Long> getSubFiltered(ArrayList<Long> processed, FilterBaseDTO next_filter) {
   // TODO just rewritten from appengine variant. Here we can optimize on multiple filters in one
   // sql statement!!!!
   ArrayList<Long> ids = processed;
   //	if(next_filter instanceof FilterDiapasonDTO)
   //	{
   //		for(FilterBaseDTO dto_sub:((FilterDiapasonDTO)
   // next_filter).getMulti_dto_proxy().getFilters())
   //		{
   //			//here we suppose that multifilter is AND-filter (for diapa)
   //			ids = getSubFiltered(processed, dto_sub);
   //		}
   //	}else
   if (next_filter instanceof FilterMultiDTO) {
     ArrayList<Long> or_ids = new ArrayList<Long>();
     for (FilterBaseDTO dto_sub : ((FilterMultiDTO) next_filter).getFilters()) {
       // here we suppose that multifilter is OR-filter
       ArrayList<Long> curr_ids = getSubFiltered(processed, dto_sub);
       or_ids = union(or_ids, curr_ids);
     }
     if (((FilterMultiDTO) next_filter).getFilters().size() > 0) ids = or_ids;
   } else {
     String filt = next_filter.getFilter();
     if (filt != null && !filt.equals("")) {
       // Query q = pm.newQuery(SocioResearch.class);
       String q_str = "SELECT x FROM SocioResearch AS x WHERE ";
       if (next_filter.getTarget_class_name().equals("SocioResearch")) {
         q_str += filt;
         // q.setFilter(filt);
       }
       TypedQuery<SocioResearch> tq = em.createQuery(q_str, SocioResearch.class);
       ArrayList<Long> current_ids = new ArrayList<Long>();
       List<SocioResearch> res = tq.getResultList();
       for (SocioResearch research : res) {
         current_ids.add(research.getID());
       }
       ids = intersection(ids, current_ids);
     }
   }
   return ids;
 }
  private MetaUnitMultivaluedEntityDTO toMultivaluedEntityDTO(
      MetaUnitMultivaluedEntity m, boolean flattened_items) {
    MetaUnitMultivaluedEntityDTO dto = new MetaUnitMultivaluedEntityDTO();
    try {
      dto.setId(m.getId());
      dto.setDesc(m.getDescription());
      dto.setUnique_name(m.getUnique_name());
      dto.setIsCatalogizable(m.getIsCatalogizable() != 0);
      dto.setIsSplittingEnabled(m.getIsSplittingEnabled() != 0);
      dto.setIsMultiselected(m.getIsMultiSelected() != 0);
      Collection<MetaUnitEntityItem> items = m.getItems();
      ArrayList<String> names = new ArrayList<String>();
      ArrayList<Long> ids = new ArrayList<Long>();
      for (MetaUnitEntityItem item : items) {
        names.add(item.getValue());
        ids.add(item.getId());
        if (flattened_items) getRecursiveItemsFlattened(names, ids, item, ". ");
        //                {
        //                    for(MetaUnitEntityItem it:item.getSubitems())
        //                    {
        //                        names.add(".  "+it.getValue());
        //                        ids.add(it.getId());
        //                    }
        //                }
      }
      dto.setItem_ids(ids);
      dto.setItem_names(names);
      ArrayList<Long> tagged_ent = new ArrayList<Long>();
      if (m.getTagged_entities() != null)
        for (Long ent : m.getTagged_entities()) {
          tagged_ent.add(ent);
        }
      dto.setTagged_entities(tagged_ent);
      ArrayList<MetaUnitDTO> sub_meta_units_dtos = new ArrayList<MetaUnitDTO>();
      if (m.getSub_meta_units() != null)
        for (MetaUnit unit : m.getSub_meta_units()) {
          if (unit != null) {
            if (unit instanceof MetaUnitMultivaluedEntity) {
              MetaUnitDTO dt;
              // DONE FOR SPLIITING DOWNLOAD OF DEEP TREE
              if (((MetaUnitMultivalued) unit).getIsSplittingEnabled() != 0) {
                dt =
                    new MetaUnitMultivaluedEntityDTO(
                        unit.getId(), unit.getUnique_name(), unit.getDescription());
                ((MetaUnitMultivaluedDTO) dt)
                    .setIsCatalogizable(((MetaUnitMultivalued) unit).getIsCatalogizable() != 0);
                ((MetaUnitMultivaluedDTO) dt)
                    .setIsSplittingEnabled(
                        ((MetaUnitMultivalued) unit).getIsSplittingEnabled() != 0);
                ((MetaUnitMultivaluedEntityDTO) dt)
                    .setIsMultiselected(
                        ((MetaUnitMultivaluedEntity) unit).getIsMultiSelected() != 0);
                Collection<MetaUnitEntityItem> items2 =
                    ((MetaUnitMultivaluedEntity) unit).getItems();
                ArrayList<String> names2 = new ArrayList<String>();
                ArrayList<Long> ids2 = new ArrayList<Long>();
                for (MetaUnitEntityItem item : items2) {
                  names2.add(item.getValue());
                  ids2.add(item.getId());
                }
                ((MetaUnitMultivaluedEntityDTO) dt).setItem_ids(ids2);
                ((MetaUnitMultivaluedEntityDTO) dt).setItem_names(names2);

              } else {
                dt = toMultivaluedEntityDTO((MetaUnitMultivaluedEntity) unit, flattened_items);
              }
              if (dt != null) sub_meta_units_dtos.add(dt);
            } else if (unit instanceof MetaUnitMultivaluedStructure) {
              MetaUnitDTO dt;
              // DONE FOR SPLIITING DOWNLOAD OF DEEP TREE
              if (((MetaUnitMultivalued) unit).getIsSplittingEnabled() != 0) {
                dt =
                    new MetaUnitMultivaluedStructureDTO(
                        unit.getId(), unit.getUnique_name(), unit.getDescription());
                ((MetaUnitMultivaluedDTO) dt)
                    .setIsCatalogizable(((MetaUnitMultivalued) unit).getIsCatalogizable() != 0);
                ((MetaUnitMultivaluedDTO) dt)
                    .setIsSplittingEnabled(
                        ((MetaUnitMultivalued) unit).getIsSplittingEnabled() != 0);
              } else {
                dt = toMultivaluedStructureDTO((MetaUnitMultivaluedStructure) unit);
              }
              if (dt != null) sub_meta_units_dtos.add(dt);
            } else {
              sub_meta_units_dtos.add(unit.toDTO());
            }
          }
        }
      dto.setSub_meta_units(sub_meta_units_dtos);

      return dto;
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      return dto;
    }
  }