/** * */ package org.gcube.portlets.user.performfishanalytics.server; import java.util.ArrayList; import java.util.List; import org.gcube.portlets.user.performfishanalytics.shared.Area; import org.gcube.portlets.user.performfishanalytics.shared.KPI; import org.gcube.portlets.user.performfishanalytics.shared.Period; import org.gcube.portlets.user.performfishanalytics.shared.Population; import org.gcube.portlets.user.performfishanalytics.shared.PopulationType; import org.gcube.portlets.user.performfishanalytics.shared.PopulationTypeProperties; import org.gcube.portlets.user.performfishanalytics.shared.Quarter; import org.gcube.portlets.user.performfishanalytics.shared.ReferencePopulationType; import org.gcube.portlets.user.performfishanalytics.shared.Species; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * The Class ToAvoidIndirectSerialization. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * Jan 22, 2019 */ public class ToAvoidIndirectSerialization { protected static Logger log = LoggerFactory.getLogger(ToAvoidIndirectSerialization.class); //TO FIX Type 'org.eclipse.persistence.indirection.IndirectList' //was not included in the set of types which can be serialized //by this SerializationPolicy or its Class object could not be loaded. //For security purposes, this type will not be serialized.: instance = {IndirectList: not instantiated} /** * To gwt serializable. * * @param listPopulationType the list population type * @param population the population * @param fetchProperties the fetch properties * @return the list */ @SuppressWarnings("unchecked") public static List toGWTSerializable(List listPopulationType, Population population, boolean fetchProperties){ if(listPopulationType==null) return new ArrayList(1); List gwtSerializableList = new ArrayList(listPopulationType.size()); for (PopulationType populationType : listPopulationType){ log.trace("Converting Population Type: "+populationType); if(fetchProperties){ List listSpecies = populationType.getListSpecies(); populationType.setListSpecies((List) toListPopulationProperties(listSpecies, null)); List listQuarter = populationType.getListQuarter(); populationType.setListQuarter((List) toListPopulationProperties(listQuarter, null)); List listArea = populationType.getListArea(); populationType.setListArea((List) toListPopulationProperties(listArea, null)); List listPeriod = populationType.getListPeriod(); populationType.setListPeriod((List) toListPopulationProperties(listPeriod, null)); }else{ populationType.setListSpecies(null); populationType.setListQuarter(null); populationType.setListArea(null); populationType.setListPeriod(null); } populationType.setListKPI(null); populationType.setPopulation(population); gwtSerializableList.add(populationType); } if(population!=null) population.setListPopulationType(gwtSerializableList); return gwtSerializableList; } /** * Sets the population types. * * @param list the list * @param type the type * @return the list */ public static List setPopulationTypes(List list, PopulationType type){ if(list==null) return null; for (ReferencePopulationType refencePopulationType : list) { refencePopulationType.setPopulationType(type); } return list; } /** * To list population properties. * * @param list the list * @param type the type * @return the list */ public static List toListPopulationProperties(List list, PopulationType type){ List listGWT = new ArrayList(); for (PopulationTypeProperties populationTypeProperties : list) { PopulationTypeProperties pop = toPopulationProperties(populationTypeProperties, type); listGWT.add(pop); } return listGWT; } /** * To population properties. * * @param the generic type * @param object the object * @param type the type * @return the t */ @SuppressWarnings("unchecked") public static T toPopulationProperties(T object, PopulationType type){ if(object instanceof Area){ return (T) new Area(object.getId(), object.getName(), object.getDescription(), type); }else if(object instanceof Species){ return (T) new Species(object.getId(), object.getName(), object.getDescription(), type); }else if(object instanceof Quarter){ return (T) new Quarter(object.getId(), object.getName(), object.getDescription(), type); }else if(object instanceof Period){ return (T) new Period(object.getId(), object.getName(), object.getDescription(), type); }else if(object instanceof KPI){ KPI toKPI = (KPI) object; KPI gwtKPI = new KPI(toKPI.getId(),toKPI.getCode(),toKPI.getName(),toKPI.getDescription(), null,type,toKPI.getDeepIndex()); gwtKPI.setLeaf(toKPI.getListKPI()==null || toKPI.getListKPI().isEmpty()); return (T) gwtKPI; } return null; } }