Policies_Storage_Service/src/com/fincons/policiesstorage/restlet/DBPolicyConnection.java
/**
	 * Updates the value of a policy inside the Database using an OrientDB query
	 * 
	 * @param father
	 *            The ID of the father node
	 * @param namePolicy
	 *            The name of the policy to modify
	 * @param policy
	 *            The new value of the policy
	 * @param username
	 *            The user that modified the policy
	 * @param blockly_xml
	 *            The xml value of the new policy
	 * @param policyID
	 *            The ID of the old policy
	 * @return JSON Object with the status of the opreation execution and a
	 *         message
	 */
Updates the value of a policy inside the Database using an OrientDB query


Updates the value of a policy inside the Database using an OrientDB query
public JSONObject updatePolicy(String father, String namePolicy, String policy, String username, String blockly_xml,
			String policyPath) {
		logger.info("Update policy \"" + namePolicy + "\" ...");
		String policyID = "";
		String getNodeQuery = Constants.SINGLENODE_QUERY;
		JSONObject policyNode_resultQuery = null;
		JSONObject finalResult = null;
		int version;
		String newPolicyID = "";
		try {
			JSONObject policyNodeRequested = getLastNodeFromRelativePath(policyPath);
			policyID = policyNodeRequested.optString(Constants.ID_NODE);
			HashMap<String, Object> paramsSingleNode = new HashMap<>();
			paramsSingleNode.put("id", policyID);
			finalResult = new JSONObject();
			Integer con_number = dbManager.connect(pathDB, usernameDB, passwordDB);
			try {
				policyNode_resultQuery = (JSONObject) dbManager.executeQuery(con_number, getNodeQuery,
						paramsSingleNode);
			} catch (Exception e2) {
				dbManager.rollback(con_number);
				logger.error("Error while reading Node " + policyID, e2);
				policyNode_resultQuery.put("status", "ERROR");
				policyNode_resultQuery.put("message", e2.getMessage());
			}
			dbManager.disconnect(con_number);
			if (policyNode_resultQuery.optString("status").equalsIgnoreCase("ok")) {
				JSONArray data = policyNode_resultQuery.getJSONArray("data");
				JSONObject policyNode = null;
				if (data != null) {
					policyNode = data.getJSONObject(0);
					version = policyNode.optInt(Constants.VERSION_NODE);
					version++;
					JSONObject createNewPolicy = savePolicy(father, namePolicy, policy, username, blockly_xml, version);
					if (createNewPolicy.optString("status").equalsIgnoreCase("ok")) {
						logger.info("Policy " + namePolicy
								+ " updated. Start delete connection between the old policy and the directory");
						newPolicyID = createNewPolicy.optString("newNode");
						String queryDeleteEdge = Constants.DELETE_EDGE;
						String fatherID = "#" + father.replace('_', ':');
						HashMap<String, Object> paramsDeleteEdge = new HashMap<String, Object>();
						paramsDeleteEdge.put("father", fatherID);
						paramsDeleteEdge.put("classFather", Constants.DIRECTORY_CLASS);
						paramsDeleteEdge.put("son", policyID);
						paramsDeleteEdge.put("classSon", Constants.POLICY_CLASS);
						logger.info("Start delete connection between the old policy and the directory");
						Integer con_number2 = dbManager.connect(pathDB, usernameDB, passwordDB);
						JSONObject edgeDeleteResult = new JSONObject();
						try {
							edgeDeleteResult = (JSONObject) dbManager.executeQuery(con_number2, queryDeleteEdge,
									paramsDeleteEdge);
						} catch (Exception e1) {
							dbManager.rollback(con_number2);
							logger.error("Error while deleting Edge", e1);
							edgeDeleteResult.put("status", "ERROR");
							edgeDeleteResult.put("message", e1.getMessage());
						}
						dbManager.disconnect(con_number2);
						if (edgeDeleteResult.optString("status").equalsIgnoreCase("ok")) {
							String queryCreateEdge = Constants.CREATE_EDGE;
							father = "#" + father.replace('_', ':');
							HashMap<String, Object> paramsEdge = new HashMap<String, Object>();
							paramsEdge.put("father", newPolicyID);
							paramsEdge.put("classFather", Constants.POLICY_CLASS);
							paramsEdge.put("son", policyID);
							paramsEdge.put("classSon", Constants.POLICY_CLASS);
							logger.info("Start create connection between the new version of policy and the old one");
							Integer con_number3 = dbManager.connect(pathDB, usernameDB, passwordDB);
							JSONObject edgeCreateResult = new JSONObject();
							try {
								edgeCreateResult = (JSONObject) dbManager.executeQuery(con_number3, queryCreateEdge,
										paramsEdge);
							} catch (Exception e) {
								dbManager.rollback(con_number3);
								logger.error("Error while creating Edge", e);
								edgeCreateResult.put("status", "ERROR");
								edgeCreateResult.put("message", e.getMessage());
							}
							dbManager.disconnect(con_number3);
							if (edgeCreateResult.optString("status").equalsIgnoreCase("ok")) {
								finalResult.put("status", "OK");
								finalResult.put("message", "Hierarchy successfully updated");
							} else {
								finalResult.put("status", "ERROR");
								finalResult.put("message", "Hierarchy update failed");
							}
						} else {
							finalResult.put("status", "ERROR");
							finalResult.put("message", "Edge not created");
						}
					}
				}
			} else {
				finalResult.put("status", "ERROR");
				finalResult.put("message", "Element not found");
				return finalResult;
			}
		} catch (JSONException e) {
			logger.error("JSON Exception" + e.getMessage());
			e.printStackTrace();
		}
		return finalResult;
	}
public JSONObject updatePolicy ( String father , String namePolicy , String policy , String username , String blockly_xml , String policyPath ) { logger . info ( "Update policy \"" + namePolicy + "\" ..." ) ; String policyID = "" ; String getNodeQuery = Constants . SINGLENODE_QUERY ; JSONObject policyNode_resultQuery = null ; JSONObject finalResult = null ; int version ; String newPolicyID = "" ; try { JSONObject policyNodeRequested = getLastNodeFromRelativePath ( policyPath ) ; policyID = policyNodeRequested . optString ( Constants . ID_NODE ) ; HashMap < String , Object > paramsSingleNode = new HashMap < > ( ) ; paramsSingleNode . put ( "id" , policyID ) ; finalResult = new JSONObject ( ) ; Integer con_number = dbManager . connect ( pathDB , usernameDB , passwordDB ) ; try { policyNode_resultQuery = ( JSONObject ) dbManager . executeQuery ( con_number , getNodeQuery , paramsSingleNode ) ; } catch ( Exception e2 ) { dbManager . rollback ( con_number ) ; logger . error ( "Error while reading Node " + policyID , e2 ) ; policyNode_resultQuery . put ( "status" , "ERROR" ) ; policyNode_resultQuery . put ( "message" , e2 . getMessage ( ) ) ; } dbManager . disconnect ( con_number ) ; if ( policyNode_resultQuery . optString ( "status" ) . equalsIgnoreCase ( "ok" ) ) { JSONArray data = policyNode_resultQuery . getJSONArray ( "data" ) ; JSONObject policyNode = null ; if ( data != null ) { policyNode = data . getJSONObject ( 0 ) ; version = policyNode . optInt ( Constants . VERSION_NODE ) ; version ++ ; JSONObject createNewPolicy = savePolicy ( father , namePolicy , policy , username , blockly_xml , version ) ; if ( createNewPolicy . optString ( "status" ) . equalsIgnoreCase ( "ok" ) ) { logger . info ( "Policy " + namePolicy + " updated. Start delete connection between the old policy and the directory" ) ; newPolicyID = createNewPolicy . optString ( "newNode" ) ; String queryDeleteEdge = Constants . DELETE_EDGE ; String fatherID = "#" + father . replace ( '_' , ':' ) ; HashMap < String , Object > paramsDeleteEdge = new HashMap < String , Object > ( ) ; paramsDeleteEdge . put ( "father" , fatherID ) ; paramsDeleteEdge . put ( "classFather" , Constants . DIRECTORY_CLASS ) ; paramsDeleteEdge . put ( "son" , policyID ) ; paramsDeleteEdge . put ( "classSon" , Constants . POLICY_CLASS ) ; logger . info ( "Start delete connection between the old policy and the directory" ) ; Integer con_number2 = dbManager . connect ( pathDB , usernameDB , passwordDB ) ; JSONObject edgeDeleteResult = new JSONObject ( ) ; try { edgeDeleteResult = ( JSONObject ) dbManager . executeQuery ( con_number2 , queryDeleteEdge , paramsDeleteEdge ) ; } catch ( Exception e1 ) { dbManager . rollback ( con_number2 ) ; logger . error ( "Error while deleting Edge" , e1 ) ; edgeDeleteResult . put ( "status" , "ERROR" ) ; edgeDeleteResult . put ( "message" , e1 . getMessage ( ) ) ; } dbManager . disconnect ( con_number2 ) ; if ( edgeDeleteResult . optString ( "status" ) . equalsIgnoreCase ( "ok" ) ) { String queryCreateEdge = Constants . CREATE_EDGE ; father = "#" + father . replace ( '_' , ':' ) ; HashMap < String , Object > paramsEdge = new HashMap < String , Object > ( ) ; paramsEdge . put ( "father" , newPolicyID ) ; paramsEdge . put ( "classFather" , Constants . POLICY_CLASS ) ; paramsEdge . put ( "son" , policyID ) ; paramsEdge . put ( "classSon" , Constants . POLICY_CLASS ) ; logger . info ( "Start create connection between the new version of policy and the old one" ) ; Integer con_number3 = dbManager . connect ( pathDB , usernameDB , passwordDB ) ; JSONObject edgeCreateResult = new JSONObject ( ) ; try { edgeCreateResult = ( JSONObject ) dbManager . executeQuery ( con_number3 , queryCreateEdge , paramsEdge ) ; } catch ( Exception e ) { dbManager . rollback ( con_number3 ) ; logger . error ( "Error while creating Edge" , e ) ; edgeCreateResult . put ( "status" , "ERROR" ) ; edgeCreateResult . put ( "message" , e . getMessage ( ) ) ; } dbManager . disconnect ( con_number3 ) ; if ( edgeCreateResult . optString ( "status" ) . equalsIgnoreCase ( "ok" ) ) { finalResult . put ( "status" , "OK" ) ; finalResult . put ( "message" , "Hierarchy successfully updated" ) ; } else { finalResult . put ( "status" , "ERROR" ) ; finalResult . put ( "message" , "Hierarchy update failed" ) ; } } else { finalResult . put ( "status" , "ERROR" ) ; finalResult . put ( "message" , "Edge not created" ) ; } } } } else { finalResult . put ( "status" , "ERROR" ) ; finalResult . put ( "message" , "Element not found" ) ; return finalResult ; } } catch ( JSONException e ) { logger . error ( "JSON Exception" + e . getMessage ( ) ) ; e . printStackTrace ( ) ; } return finalResult ; }
Updates the value of a policy inside the Database using an OrientDB query
Updates the value of a policy inside the Database using an OrientDB query