Policies_Storage_Service/src/com/fincons/policiesstorage/restlet/DBPolicyConnection.java
/**
	 * Saves a policy node inside the Database using an OrientDB query
	 * 
	 * @param father
	 *            The ID of the father node
	 * @param namePolicy
	 *            The name of the policy to create
	 * @param valuePolicy
	 *            The string value of the policy
	 * @param username
	 *            The user that creates the policy
	 * @param blockly_xml
	 *            The xml value of the policy
	 * @param version
	 *            The default version for the first storage of the policy.
	 * @return JSON Object with the information about the new policy created,
	 *         the status of the query execution and a message
	 */
Saves a policy node inside the Database using an OrientDB query


Saves a policy node inside the Database using an OrientDB query
public JSONObject savePolicy(String father, String namePolicy, String valuePolicy, String username,
			String blockly_xml, int version) {
		logger.info("Start creation policy " + namePolicy);
		JSONObject result = null;
		try {
			String queryAddPolicy = Constants.CREATE_POLICY;
			Date date = new Date();
			Timestamp timestamp = (new Timestamp(date.getTime()));
			HashMap<String, Object> paramsPolicy = new HashMap<String, Object>();
			paramsPolicy.put("name", namePolicy);
			paramsPolicy.put("timestamp", timestamp);
			paramsPolicy.put("value", valuePolicy);
			paramsPolicy.put("created_by", username);
			paramsPolicy.put("blockly_xml", blockly_xml);
			paramsPolicy.put("version", version);
			Integer con_number = dbManager.connect(pathDB, usernameDB, passwordDB);
			JSONObject creationResult = new JSONObject();
			try {
				creationResult = (JSONObject) dbManager.executeQuery(con_number, queryAddPolicy, paramsPolicy);
			} catch (Exception e) {
				dbManager.rollback(con_number);
				logger.error("Error while creating Policy Node with name " + namePolicy, e);
				creationResult.put("status", "ERROR");
				creationResult.put("message", e.getMessage());
			}
			dbManager.disconnect(con_number);
			String newNodeID = creationResult.optString(Constants.ID_NODE);
			result = new JSONObject();
			String statusQuery = creationResult.optString("status");
			if (statusQuery.equalsIgnoreCase("ok")) {
				logger.info("Policy " + namePolicy + " successfully created");
				String queryCreateEdge = Constants.CREATE_EDGE;
				String fatherID = "#" + father.replace('_', ':');
				HashMap<String, Object> paramsEdge = new HashMap<String, Object>();
				paramsEdge.put("father", fatherID);
				paramsEdge.put("classFather", Constants.DIRECTORY_CLASS);
				paramsEdge.put("son", newNodeID);
				paramsEdge.put("classSon", Constants.POLICY_CLASS);
				logger.info("Start create connection between the policy and the directory");
				Integer con_number2 = dbManager.connect(pathDB, usernameDB, passwordDB);
				JSONObject edgeResult = new JSONObject();
				try {
					edgeResult = (JSONObject) dbManager.executeQuery(con_number2, queryCreateEdge, paramsEdge);
				} catch (Exception e) {
					dbManager.rollback(con_number2);
					logger.error("Error while creating Edge", e);
					creationResult.put("status", "ERROR");
					creationResult.put("message", e.getMessage());
				}
				dbManager.disconnect(con_number2);
				if (edgeResult.optString("status").equalsIgnoreCase("ok")) {
					result.put("status", "OK");
					result.put("message", "Policy successfully created");
					result.put("newNode", newNodeID);
				} else {
					result.put("status", "ERROR");
					result.put("message", "Policy creation failed");
				}
			}
		} catch (JSONException e) {
			logger.error("JSON Exception " + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
public JSONObject savePolicy ( String father , String namePolicy , String valuePolicy , String username , String blockly_xml , int version ) { logger . info ( "Start creation policy " + namePolicy ) ; JSONObject result = null ; try { String queryAddPolicy = Constants . CREATE_POLICY ; Date date = new Date ( ) ; Timestamp timestamp = ( new Timestamp ( date . getTime ( ) ) ) ; HashMap < String , Object > paramsPolicy = new HashMap < String , Object > ( ) ; paramsPolicy . put ( "name" , namePolicy ) ; paramsPolicy . put ( "timestamp" , timestamp ) ; paramsPolicy . put ( "value" , valuePolicy ) ; paramsPolicy . put ( "created_by" , username ) ; paramsPolicy . put ( "blockly_xml" , blockly_xml ) ; paramsPolicy . put ( "version" , version ) ; Integer con_number = dbManager . connect ( pathDB , usernameDB , passwordDB ) ; JSONObject creationResult = new JSONObject ( ) ; try { creationResult = ( JSONObject ) dbManager . executeQuery ( con_number , queryAddPolicy , paramsPolicy ) ; } catch ( Exception e ) { dbManager . rollback ( con_number ) ; logger . error ( "Error while creating Policy Node with name " + namePolicy , e ) ; creationResult . put ( "status" , "ERROR" ) ; creationResult . put ( "message" , e . getMessage ( ) ) ; } dbManager . disconnect ( con_number ) ; String newNodeID = creationResult . optString ( Constants . ID_NODE ) ; result = new JSONObject ( ) ; String statusQuery = creationResult . optString ( "status" ) ; if ( statusQuery . equalsIgnoreCase ( "ok" ) ) { logger . info ( "Policy " + namePolicy + " successfully created" ) ; String queryCreateEdge = Constants . CREATE_EDGE ; String fatherID = "#" + father . replace ( '_' , ':' ) ; HashMap < String , Object > paramsEdge = new HashMap < String , Object > ( ) ; paramsEdge . put ( "father" , fatherID ) ; paramsEdge . put ( "classFather" , Constants . DIRECTORY_CLASS ) ; paramsEdge . put ( "son" , newNodeID ) ; paramsEdge . put ( "classSon" , Constants . POLICY_CLASS ) ; logger . info ( "Start create connection between the policy and the directory" ) ; Integer con_number2 = dbManager . connect ( pathDB , usernameDB , passwordDB ) ; JSONObject edgeResult = new JSONObject ( ) ; try { edgeResult = ( JSONObject ) dbManager . executeQuery ( con_number2 , queryCreateEdge , paramsEdge ) ; } catch ( Exception e ) { dbManager . rollback ( con_number2 ) ; logger . error ( "Error while creating Edge" , e ) ; creationResult . put ( "status" , "ERROR" ) ; creationResult . put ( "message" , e . getMessage ( ) ) ; } dbManager . disconnect ( con_number2 ) ; if ( edgeResult . optString ( "status" ) . equalsIgnoreCase ( "ok" ) ) { result . put ( "status" , "OK" ) ; result . put ( "message" , "Policy successfully created" ) ; result . put ( "newNode" , newNodeID ) ; } else { result . put ( "status" , "ERROR" ) ; result . put ( "message" , "Policy creation failed" ) ; } } } catch ( JSONException e ) { logger . error ( "JSON Exception " + e . getMessage ( ) ) ; e . printStackTrace ( ) ; } return result ; }
Saves a policy node inside the Database using an OrientDB query
Saves a policy node inside the Database using an OrientDB query