Package tactic

Source Code of tactic.TacticServerStub

// -----------------------------------------------------------------------------
//
//  Copyright (c) 2012, Southpaw Technology Inc., All Rights Reserved
//
//  PROPRIETARY INFORMATION.  This software is proprietary to
//  Southpaw Technology Inc., and is not to be reproduced, transmitted,
//  or disclosed in any way without written permission.
//
// -----------------------------------------------------------------------------
//
//

package tactic;

import java.util.*;
import java.io.*;

import java.util.logging.Level;
import java.util.logging.Logger;

import jep.Jep;
import jep.JepException;

import org.codehaus.jackson.map.ObjectMapper;

class TacticException extends Exception {
}
public class TacticServerStub {
    private Jep jep;
    private ObjectMapper mapper;
    private String protocol;



    public TacticServerStub() {
        try {

            jep = new Jep();

            ClassLoader loader = TacticServerStub.class.getClassLoader();
            String path = loader.getResource("tactic/TacticServerStub.class").toString();
            path = path.substring(5, path.length() );
            System.out.println(path);
            File file = new File(path);
            String dirname = file.getParent();

            //protocol = "xmlrpc";
            protocol = "local";
            jep.set("protocol", protocol);
            // put in some dummy values
            jep.set("function", "foo");
            jep.set("kwargs", "{}");

            jep.runScript(dirname + "/jep_init.py");

        } catch (JepException ex) {
            Logger.getLogger(TacticServerStub.class.getName()).log(Level.SEVERE, null, ex);
        }


        mapper = new ObjectMapper();
    }


    private String execute(String function, Map kwargs) {
        try {
            jep.set("function", function);


            // convert kwargs to a json string
            if (kwargs == null) {
                jep.set("kwargs", "{}");
            }
            else {
                Writer kwargs_writer = new StringWriter();
                mapper.writeValue(kwargs_writer, kwargs);
                String kwargs_json = kwargs_writer.toString();
                jep.set("kwargs", kwargs_json);
            }

            // execute the python script



            //jep.runScript("./delegator.py");
            jep.eval("delegate()");


            String ret_val = (String) jep.getValue("ret_val");
            return ret_val;
        } catch (jep.JepException e) {
            System.out.println("Error getting value from Jep");
            System.out.println(e);
        } catch (IOException e) {
            System.out.println("Error getting value from Jep");
            System.out.println(e);
        }
        return null;
    }


    private String get_string_return(String ret_val) {
        if (ret_val == null) {
            ret_val = "";
        }
        else {
            try {
                ret_val = mapper.readValue(ret_val, String.class);
            } catch( IOException e) {
                System.out.println(e);
            }
        }
        return ret_val;
    }


    private ArrayList get_array_return(String ret_str) {
        try {
            ArrayList<Map> ret_val = mapper.readValue(ret_str, ArrayList.class);
            return ret_val;

        } catch (IOException e) {

            System.out.println("Error:");
            System.out.println(e);
            return new ArrayList();
        }
    }


    private Map get_map_return(String ret_str) {
        try {
            Map<String,Object> ret_val = mapper.readValue(ret_str, HashMap.class);
            return ret_val;
        } catch (IOException e) {
            System.out.println("Error:");
            System.out.println(e);
            return new HashMap();
        }
    }


    //
    // Misc functions
    //
    public void set_project(String project_code) {
        if (protocol == "local") {
            try {
                jep.eval("from pyasm.biz import Project");
                jep.eval("Project.set_project(\""+project_code+"\")");

            } catch (JepException ex) {
                Logger.getLogger(TacticServerStub.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        Map kwargs = new HashMap<String,Object>();
        kwargs.put("project_code", project_code);
        execute("set_project", kwargs);
    }



    //
    // Version methods
    //

    public String get_server_version() {
        String ret_val = execute("get_server_version", null);
        return get_string_return(ret_val);
    }


    public String get_server_api_version() {
        String ret_val = execute("get_server_api_version", null);
        return get_string_return(ret_val);
    }



    //
    // Simple Ping Test
    //
    public String ping() {
        String ret_val = execute("ping", null);
        return get_string_return(ret_val);
    }


    public Map get_connection_info() {
        String ret_val = execute("get_connection_info", null);
        return get_map_return(ret_val);
    }



    //
    // Database methods
    //
    public ArrayList query(String search_type) {
        Map kwargs = new HashMap<String,Object>();
        return query(search_type, kwargs);
    }

    public ArrayList query(String search_type, Map kwargs) {
        kwargs.put("search_type", search_type);
        String ret_str = execute("query", kwargs);
        return get_array_return(ret_str);
    }




    public Map insert(String search_key, Map data) {
        Map kwargs = new HashMap<String,Object>();
        return insert(search_key, data, kwargs);
    }

    public Map insert(Map sobject, Map data) {
        Map kwargs = new HashMap<String,Object>();
        return insert(sobject, data, kwargs);
    }

    public Map insert(Map sobject, Map data, Map kwargs) {
        String search_key = (String) sobject.get("__search_key__");
        return insert(search_key, data, kwargs);
    }

    public Map insert(String search_type, Map data, Map kwargs) {
        kwargs.put("search_type", search_type);
        kwargs.put("data", data);
        String ret_str = execute("insert", kwargs);
        return get_map_return(ret_str);
    }




    public Map update(String search_key, Map update_data) {
        Map kwargs = new HashMap<String,Object>();
        return update(search_key, update_data, kwargs);
    }

    public Map update(Map sobject, Map update_data) {
        Map kwargs = new HashMap<String,Object>();
        return update(sobject, update_data, kwargs);
    }

    public Map update(Map sobject, Map update_data, Map kwargs) {
        String search_key = (String) sobject.get("__search_key__");
        return update(search_key, update_data, kwargs);
    }

    public Map update(String search_key, Map update_data, Map kwargs) {
        kwargs.put("search_key", search_key);
        kwargs.put("data", update_data);
        String ret_str = execute("update", kwargs);
        return get_map_return(ret_str);
    }



    /*
     * Expression method
     */
    public ArrayList<Map> eval(String expression) {
        Map kwargs = new HashMap<String,Object>();
        return eval(expression, kwargs);
    }

    public ArrayList<Map> eval(String expression, Map kwargs) {
        kwargs.put("expression", expression);
        String ret_str = execute("eval", kwargs);
        return get_array_return(ret_str);
    }

    /*
     * SObject methods
     */

    public Map retire_sobject(String search_key) {
        Map kwargs = new HashMap<String,Object>();
        return retire_sobject(search_key, kwargs);
    }

    public Map retire_sobject(String search_key, Map kwargs) {
        kwargs.put("search_key", search_key);
        String ret_str = execute("retire_sobject", kwargs);
        return get_map_return(ret_str);
    }


    public Map reactivate_sobject(String search_key) {
        Map kwargs = new HashMap<String,Object>();
        return reactivate_sobject(search_key, kwargs);
    }

    public Map reactivate_sobject(String search_key, Map kwargs) {
        kwargs.put("search_key", search_key);
        String ret_str = execute("reactivate_sobject", kwargs);
        return get_map_return(ret_str);
    }




    public Map delete_sobject(String search_key) {
        Map kwargs = new HashMap<String,Object>();
        return delete_sobject(search_key, kwargs);
    }

    public Map delete_sobject(String search_key, Map kwargs) {
        kwargs.put("search_key", search_key);
        String ret_str = execute("delete_sobject", kwargs);
        return get_map_return(ret_str);
    }




    public Map clone_sobject(String search_key, Map data) {
        Map kwargs = new HashMap<String,Object>();
        return clone_sobject(search_key, kwargs);
    }

    public Map clone_sobject(String search_key, Map data, Map kwargs) {
        kwargs.put("search_key", search_key);
        kwargs.put("data", data);
        String ret_str = execute("clone_sobject", kwargs);
        return get_map_return(ret_str);
    }


    /*
     * Check-in/Check-out methods
     */
    public void upload_file(String file_path) {
        upload_file(file_path, null);
    }

    public void upload_file(String file_path, String ticket) {
        Map kwargs = new HashMap<String,Object>();
        kwargs.put("path", file_path);
        if (ticket != null) {
            kwargs.put("ticket", ticket);
        }

        execute("upload_file", kwargs);
    }



    public Map simple_checkin(String search_key, String context, String file_path) {
        Map kwargs = new HashMap<String,Object>();
        return simple_checkin(search_key, context, file_path, kwargs);
    }


    public Map simple_checkin(String search_key, String context, String file_path, Map kwargs) {
        kwargs.put("search_key", search_key);
        kwargs.put("context", context);
        kwargs.put("file_path", file_path);

        String ret_str = execute("simple_checkin", kwargs);
        return get_map_return(ret_str);
    }

}

TOP

Related Classes of tactic.TacticServerStub

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.