Search in sources :

Example 1 with XmlArrayList

use of com.thoughtworks.xstream.persistence.XmlArrayList in project spatial-portal by AtlasOfLivingAustralia.

the class MapComposer method loadUserSession.

public void loadUserSession(String sessionid) {
    onClick$removeAllLayers();
    Scanner scanner = null;
    try {
        String sfld = getSettingsSupplementary().getProperty(StringConstants.ANALYSIS_OUTPUT_DIR) + "session/" + sessionid;
        File sessfolder = new File(sfld);
        if (!sessfolder.exists()) {
            showMessage("Session information does not exist. Please provide a valid session id");
            return;
        }
        scanner = new Scanner(new File(sfld + "/details.txt"));
        // first grab the zoom level and bounding box
        String[] mapdetails = scanner.nextLine().split(",");
        BoundingBox bb = new BoundingBox();
        bb.setMinLongitude(Float.parseFloat(mapdetails[1]));
        bb.setMinLatitude(Float.parseFloat(mapdetails[2]));
        bb.setMaxLongitude(Float.parseFloat(mapdetails[3]));
        bb.setMaxLatitude(Float.parseFloat(mapdetails[4]));
        openLayersJavascript.setAdditionalScript(openLayersJavascript.zoomToBoundingBox(bb, true));
        String[] scatterplotNames = null;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.startsWith("scatterplotNames")) {
                scatterplotNames = line.substring(17).split("___");
            }
        }
        ArrayUtils.reverse(scatterplotNames);
        // ignore fields not found
        XStream xstream = new XStream(new DomDriver()) {

            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {

                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        if (definedIn == Object.class || !super.shouldSerializeMember(definedIn, fieldName))
                            System.out.println("faled to read: " + definedIn + ", " + fieldName);
                        return definedIn != Object.class ? super.shouldSerializeMember(definedIn, fieldName) : false;
                    }
                };
            }

            @Override
            public Object unmarshal(HierarchicalStreamReader reader) {
                Object o = super.unmarshal(reader);
                if (o instanceof BiocacheQuery)
                    ((BiocacheQuery) o).getFullQ(false);
                return o;
            }

            @Override
            public Object unmarshal(HierarchicalStreamReader reader, Object root) {
                Object o = super.unmarshal(reader, root);
                if (o instanceof BiocacheQuery)
                    ((BiocacheQuery) o).getFullQ(false);
                return o;
            }

            @Override
            public Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder) {
                Object o = super.unmarshal(reader, root, dataHolder);
                if (o instanceof BiocacheQuery)
                    ((BiocacheQuery) o).getFullQ(false);
                return o;
            }
        };
        PersistenceStrategy strategy = new FilePersistenceStrategy(new File(sfld), xstream);
        List list = new XmlArrayList(strategy);
        ListIterator it = list.listIterator(list.size());
        int scatterplotIndex = 0;
        while (it.hasPrevious()) {
            Object o = it.previous();
            MapLayer ml = null;
            if (o instanceof MapLayer) {
                ml = (MapLayer) o;
                LOGGER.debug("Loading " + ml.getName() + " -> " + ml.isDisplayed());
                addUserDefinedLayerToMenu(ml, false);
            } else if (o instanceof ScatterplotDataDTO) {
                ScatterplotDataDTO spdata = (ScatterplotDataDTO) o;
                loadScatterplot(spdata, "My Scatterplot " + scatterplotIndex++);
            }
            if (ml != null) {
                addUserDefinedLayerToMenu(ml, true);
            }
        }
    } catch (Exception e) {
        try {
            File f = new File("/data/sessions/" + sessionid + ".txt");
            PrintWriter pw = new PrintWriter(f);
            e.printStackTrace(pw);
            pw.close();
        } catch (Exception ex) {
        }
        LOGGER.error("Unable to load session data", e);
        showMessage("Unable to load session data");
    } finally {
        if (scanner != null) {
            scanner.close();
        }
        try {
            File f = new File("/data/sessions/ok/" + sessionid + ".txt");
            FileUtils.writeStringToFile(f, "ok");
        } catch (Exception ex) {
        }
    }
}
Also used : ScatterplotDataDTO(au.org.ala.spatial.dto.ScatterplotDataDTO) XStream(com.thoughtworks.xstream.XStream) HasMapLayer(au.org.emii.portal.menu.HasMapLayer) MapLayer(au.org.emii.portal.menu.MapLayer) XmlArrayList(com.thoughtworks.xstream.persistence.XmlArrayList) ParseException(org.json.simple.parser.ParseException) PersistenceStrategy(com.thoughtworks.xstream.persistence.PersistenceStrategy) FilePersistenceStrategy(com.thoughtworks.xstream.persistence.FilePersistenceStrategy) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) MapperWrapper(com.thoughtworks.xstream.mapper.MapperWrapper) DataHolder(com.thoughtworks.xstream.converters.DataHolder) BoundingBox(au.org.emii.portal.value.BoundingBox) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) JSONObject(org.json.simple.JSONObject) LegendObject(au.org.ala.legend.LegendObject) XmlArrayList(com.thoughtworks.xstream.persistence.XmlArrayList) List(java.util.List) FilePersistenceStrategy(com.thoughtworks.xstream.persistence.FilePersistenceStrategy)

Example 2 with XmlArrayList

use of com.thoughtworks.xstream.persistence.XmlArrayList in project spatial-portal by AtlasOfLivingAustralia.

the class MapComposer method saveSession.

private String saveSession() {
    String jsessionid = getCookieValue("JSESSIONID");
    if (jsessionid == null) {
        jsessionid = "test";
    }
    PrintWriter out = null;
    try {
        String sfld = getSettingsSupplementary().getProperty(StringConstants.ANALYSIS_OUTPUT_DIR) + "session/" + jsessionid;
        // if the session dir exists, then clear it,
        // if not let's create it.
        File sessfolder = new File(sfld + "/");
        if (!sessfolder.exists()) {
            sessfolder.mkdirs();
        } else {
            FileUtils.deleteDirectory(sessfolder);
            sessfolder.mkdirs();
        }
        StringBuilder sbSession = new StringBuilder();
        sbSession.append(String.valueOf(mapZoomLevel)).append(",").append(getLeftmenuSearchComposer().getViewportBoundingBox().toString());
        sbSession.append(System.getProperty("line.separator"));
        PersistenceStrategy strategy = new FilePersistenceStrategy(new File(sfld));
        List list = new XmlArrayList(strategy);
        String scatterplotNames = "";
        List udl = getPortalSession().getActiveLayers();
        Iterator iudl = udl.iterator();
        while (iudl.hasNext()) {
            MapLayer ml = (MapLayer) iudl.next();
            if (ml.getType() != LayerUtilitiesImpl.MAP) {
                if (ml.getSubType() == LayerUtilitiesImpl.SCATTERPLOT) {
                    list.add(ml.getScatterplotDataDTO());
                    scatterplotNames += ((scatterplotNames.length() > 1) ? "___" + ml.getName() : ml.getName());
                } else {
                    list.add(ml);
                }
            }
        }
        sbSession.append("scatterplotNames=").append(scatterplotNames);
        sbSession.append(System.getProperty("line.separator"));
        out = new PrintWriter(new BufferedWriter(new FileWriter(sfld + "/details.txt")));
        out.write(sbSession.toString());
        out.close();
    } catch (Exception e) {
        LOGGER.error("failed to save session", e);
    } finally {
        if (out != null) {
            out.close();
        }
    }
    return CommonData.getWebportalServer() + "?ss=" + jsessionid;
}
Also used : HasMapLayer(au.org.emii.portal.menu.HasMapLayer) MapLayer(au.org.emii.portal.menu.MapLayer) XmlArrayList(com.thoughtworks.xstream.persistence.XmlArrayList) ParseException(org.json.simple.parser.ParseException) PersistenceStrategy(com.thoughtworks.xstream.persistence.PersistenceStrategy) FilePersistenceStrategy(com.thoughtworks.xstream.persistence.FilePersistenceStrategy) XmlArrayList(com.thoughtworks.xstream.persistence.XmlArrayList) List(java.util.List) FilePersistenceStrategy(com.thoughtworks.xstream.persistence.FilePersistenceStrategy)

Aggregations

HasMapLayer (au.org.emii.portal.menu.HasMapLayer)2 MapLayer (au.org.emii.portal.menu.MapLayer)2 FilePersistenceStrategy (com.thoughtworks.xstream.persistence.FilePersistenceStrategy)2 PersistenceStrategy (com.thoughtworks.xstream.persistence.PersistenceStrategy)2 XmlArrayList (com.thoughtworks.xstream.persistence.XmlArrayList)2 List (java.util.List)2 ParseException (org.json.simple.parser.ParseException)2 LegendObject (au.org.ala.legend.LegendObject)1 ScatterplotDataDTO (au.org.ala.spatial.dto.ScatterplotDataDTO)1 BoundingBox (au.org.emii.portal.value.BoundingBox)1 XStream (com.thoughtworks.xstream.XStream)1 DataHolder (com.thoughtworks.xstream.converters.DataHolder)1 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)1 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)1 MapperWrapper (com.thoughtworks.xstream.mapper.MapperWrapper)1 JSONObject (org.json.simple.JSONObject)1