Example usage for com.google.gson.internal LinkedTreeMap remove

List of usage examples for com.google.gson.internal LinkedTreeMap remove

Introduction

In this page you can find the example usage for com.google.gson.internal LinkedTreeMap remove.

Prototype

@Override
    public V remove(Object key) 

Source Link

Usage

From source file:org.cgiar.ccafs.marlo.action.center.json.autosave.AutoSaveWriterAction.java

License:Open Source License

@Override
public String execute() throws Exception {

    String fileId = "";
    String fileClass = "";
    String nameClass = "";
    String fileAction = "";

    status = new HashMap<String, Object>();

    if (autoSave.length > 0) {

        Gson gson = new Gson();
        byte ptext[] = autoSave[0].getBytes(ISO_8859_1);
        String value = new String(ptext, UTF_8);

        @SuppressWarnings("unchecked")

        LinkedTreeMap<String, Object> result = gson.fromJson(value, LinkedTreeMap.class);

        String userModifiedBy = fileId = (String) result.get("modifiedBy.id");
        if (result.containsKey("id")) {
            fileId = (String) result.get("id");
        } else {/*w  ww .j a  v  a2  s. c  o  m*/
            fileId = "XX";
        }

        if (result.containsKey("className")) {
            String ClassName = (String) result.get("className");
            String[] composedClassName = ClassName.split("\\.");
            nameClass = ClassName;
            fileClass = composedClassName[composedClassName.length - 1];
        }

        if (result.containsKey("actionName")) {
            fileAction = (String) result.get("actionName");
            fileAction = fileAction.replace("/", "_");
        }
        ArrayList<String> keysRemove = new ArrayList<>();

        for (Map.Entry<String, Object> entry : result.entrySet()) {
            if (entry.getKey().contains("__")) {
                keysRemove.add(entry.getKey());
            }
        }
        for (String string : keysRemove) {
            result.remove(string);
        }
        Date generatedDate = new Date();
        result.put("activeSince", generatedDate);

        String jSon = gson.toJson(result);

        if (nameClass.equals(CenterOutcome.class.getName())) {
            jSon = jSon.replaceAll("outcome\\.", "");
        }
        if (nameClass.equals(CenterOutput.class.getName())) {
            jSon = jSon.replaceAll("output\\.", "");
        }
        if (nameClass.equals(CenterProject.class.getName())) {
            jSon = jSon.replaceAll("project\\.", "");
        }
        if (nameClass.equals(CenterDeliverable.class.getName())) {
            jSon = jSon.replaceAll("deliverable\\.", "");
        }
        if (nameClass.equals(CapacityDevelopment.class.getName())) {
            jSon = jSon.replaceAll("capdev\\.", "");
        }

        try {

            String fileName = fileId + "_" + fileClass + "_" + fileAction + ".json";
            String pathFile = config.getAutoSaveFolder();
            Path path = Paths.get(pathFile);

            if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
                File file = new File(config.getAutoSaveFolder() + fileName);
                Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
                out.append(jSon).append("\r\n");
                ;

                out.flush();
                out.close();
            } else {
                Files.createDirectories(path);
                File file = new File(config.getAutoSaveFolder() + fileName);
                Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
                out.append(jSon).append("\r\n");
                ;

                out.flush();
                out.close();
            }
            status.put("status", true);
            SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss");
            status.put("modifiedBy",
                    userService.getUser(Long.parseLong(userModifiedBy)).getComposedCompleteName());
            status.put("activeSince", dt.format(generatedDate));
        } catch (IOException e) {
            status.put("status", false);
            e.printStackTrace();
        } catch (Exception e) {
            status.put("status", false);
            e.printStackTrace();
        }

    }

    return Action.SUCCESS;
}

From source file:org.cgiar.ccafs.marlo.action.json.autosave.AutoSaveWriterAction.java

License:Open Source License

@Override
public String execute() throws Exception {

    String fileId = "";
    String fileClass = "";
    String nameClass = "";
    String fileAction = "";

    status = new HashMap<String, Object>();

    if (autoSave.length > 0) {

        Gson gson = new Gson();
        byte ptext[] = autoSave[0].getBytes(ISO_8859_1);
        String value = new String(ptext, UTF_8);

        @SuppressWarnings("unchecked")

        LinkedTreeMap<String, Object> result = gson.fromJson(value, LinkedTreeMap.class);

        String userModifiedBy = fileId = (String) result.get("modifiedBy.id");
        if (result.containsKey("id")) {
            fileId = (String) result.get("id");
        } else {/*from   w  w w .j a v a 2 s . c  om*/
            fileId = "XX";
        }

        if (result.containsKey("className")) {
            String ClassName = (String) result.get("className");
            String[] composedClassName = ClassName.split("\\.");
            nameClass = ClassName;
            fileClass = composedClassName[composedClassName.length - 1];
        }

        if (result.containsKey("actionName")) {
            fileAction = (String) result.get("actionName");
            fileAction = fileAction.replace("/", "_");
        }
        ArrayList<String> keysRemove = new ArrayList<>();

        for (Map.Entry<String, Object> entry : result.entrySet()) {
            if (entry.getKey().contains("__")) {
                keysRemove.add(entry.getKey());
            }
        }
        for (String string : keysRemove) {
            result.remove(string);
        }
        Date generatedDate = new Date();
        result.put("activeSince", generatedDate);

        String jSon = gson.toJson(result);

        if (nameClass.equals(ResearchOutcome.class.getName())) {
            jSon = jSon.replaceAll("outcome\\.", "");
        }
        if (nameClass.equals(ResearchOutput.class.getName())) {
            jSon = jSon.replaceAll("output\\.", "");
        }

        try {

            String fileName = fileId + "_" + fileClass + "_" + fileAction + ".json";
            String pathFile = config.getAutoSaveFolder();
            System.out.println(pathFile);
            Path path = Paths.get(pathFile);

            if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
                File file = new File(config.getAutoSaveFolder() + fileName);
                Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
                out.append(jSon).append("\r\n");
                ;

                out.flush();
                out.close();
            } else {
                Files.createDirectories(path);
                File file = new File(config.getAutoSaveFolder() + fileName);
                Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
                out.append(jSon).append("\r\n");
                ;

                out.flush();
                out.close();
            }
            status.put("status", true);
            SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss");
            status.put("modifiedBy",
                    userService.getUser(Long.parseLong(userModifiedBy)).getComposedCompleteName());
            status.put("activeSince", dt.format(generatedDate));
        } catch (IOException e) {
            status.put("status", false);
            e.printStackTrace();
        } catch (Exception e) {
            status.put("status", false);
            e.printStackTrace();
        }

    }

    return Action.SUCCESS;
}