Policies_Storage_Service/src/com/fincons/policiesstorage/restlet/Dispatcher.java
/**
	 * Stores an element in the Database. The information about the element are
	 * in the string parameter of the method and in the path took with getPath()
	 * method. The element could be a folder or a policy to create or a policy
	 * to update.
	 * 
	 * @param parameters
	 *            The string with the parameters of the element to create in
	 *            JSON format
	 * @return JSON Object with the status code of the request and a message.
	 * @throws ResourceException
	 * @throws UnsupportedEncodingException
	 * @throws JSONException
	 */
Stores an element in the Database. The information about the element are
in the string parameter of the method and in the path took with getPath()
method. The element could be a folder or a policy to create or a policy
to update.

@param parameters
The string with the parameters of the element to create in
JSON format
@return JSON Object with the status code of the request and a message.
Stores an element in the Database . The information about the element are in the string parameter of the method and in the path took with getPath () method . The element could be a folder or a policy to create or a policy to update . @param parameters The string with the parameters of the element to create in JSON format @return JSON Object with the status code of the request and a message .
@Post
	public JsonRepresentation saveElement(String parameters) throws ResourceException, UnsupportedEncodingException {
		logger.info("Start savePolicy (json) : " + parameters);
		String path = getPath();
		logger.debug("Path from getPath(): " + path);
		String[] splittedPath = path.split("/");
		String namePolicy = URLDecoder.decode(splittedPath[splittedPath.length - 1], "utf-8");
		logger.debug("Name policy: " + namePolicy);
		String father = "";
		String policy = "";
		String username = "";
		String type = "";
		String blockly_xml = "";
		String policyID = "";
		try {
			JSONObject obj = new JSONObject(parameters);
			policy = obj.optString(Constants.POLICY);
			username = obj.optString("username");
			if (policy == null || policy == "") {
				type = obj.optString(Constants.TYPE);
			} else {
				blockly_xml = obj.optString("xml_text");
				policyID = obj.optString("id");
			}
		} catch (JSONException e1) {
			logger.error("Error Reading parameter", e1);
			getResponse().setStatus(new Status(400));
			return new JsonRepresentation(new JSONObject());
		}
		JSONObject response = null;
		int status = 500;
		String message = "";
		try {
			response = new JSONObject();
			String[] splitted = path.split("/");
			splitted = Arrays.copyOfRange(splitted, 0, splitted.length - 1);
			path = String.join("/", splitted);
			JSONObject fatherNode = dbPolicyConnection.getLastNodeFromRelativePath(path);
			if(fatherNode == null){
				response.put("stauts", 404);
				response.put("message", "One element of the path not found");
				getResponse().setStatus(new Status(404));
				return new JsonRepresentation(response);
			}
			father = fatherNode.optString(Constants.ID_NODE);
			JSONObject created = null;
			if (!type.isEmpty() || !type.equals("")) {
				created = dbPolicyConnection.saveFolder(father, namePolicy, username);
			} else if (policyID.isEmpty() || policyID.equals("")) {
				if (dbPolicyConnection.isValid(father, namePolicy)) {
					created = dbPolicyConnection.savePolicy(father, namePolicy, policy, username, blockly_xml,
							Constants.DEFAULT_VERSION);
				} else {
					try {
						response = new JSONObject();
						response.put("message", "Conflict, Policy with name " + namePolicy + " already exists");
						response.put("code", 409);
						getResponse().setStatus(new Status(409));
						return new JsonRepresentation(response);
					} catch (JSONException e) {
						logger.error("JSONException", e);
						e.printStackTrace();
					}
				}
			} else {
				created = dbPolicyConnection.updatePolicy(father, namePolicy, policy, username, blockly_xml, policyID);
			}
			if (created.optString("status").equalsIgnoreCase("ok")) {
				status = 201;
				response.put("code", status);
			}
			message = created.optString("message");
		} catch (Exception e) {
			logger.error("Exception", e);
			e.printStackTrace();
			response = new JSONObject();
			try {
				response.put("faultCode", Status.SERVER_ERROR_INTERNAL);
				response.put("message", "Internal Server Error");
			} catch (JSONException e1) {
				logger.error("JSONException", e1);
				e1.printStackTrace();
			}
			getResponse().setStatus(new Status(500));
			return new JsonRepresentation(response);
		}
		getResponse().setStatus(new Status(status));
		logger.info(message);
		return new JsonRepresentation(response);
	}
@ Post public JsonRepresentation saveElement ( String parameters ) throws ResourceException , UnsupportedEncodingException { logger . info ( "Start savePolicy (json) : " + parameters ) ; String path = getPath ( ) ; logger . debug ( "Path from getPath(): " + path ) ; String [ ] splittedPath = path . split ( "/" ) ; String namePolicy = URLDecoder . decode ( splittedPath [ splittedPath . length - 1 ] , "utf-8" ) ; logger . debug ( "Name policy: " + namePolicy ) ; String father = "" ; String policy = "" ; String username = "" ; String type = "" ; String blockly_xml = "" ; String policyID = "" ; try { JSONObject obj = new JSONObject ( parameters ) ; policy = obj . optString ( Constants . POLICY ) ; username = obj . optString ( "username" ) ; if ( policy == null || policy == "" ) { type = obj . optString ( Constants . TYPE ) ; } else { blockly_xml = obj . optString ( "xml_text" ) ; policyID = obj . optString ( "id" ) ; } } catch ( JSONException e1 ) { logger . error ( "Error Reading parameter" , e1 ) ; getResponse ( ) . setStatus ( new Status ( 400 ) ) ; return new JsonRepresentation ( new JSONObject ( ) ) ; } JSONObject response = null ; int status = 500 ; String message = "" ; try { response = new JSONObject ( ) ; String [ ] splitted = path . split ( "/" ) ; splitted = Arrays . copyOfRange ( splitted , 0 , splitted . length - 1 ) ; path = String . join ( "/" , splitted ) ; JSONObject fatherNode = dbPolicyConnection . getLastNodeFromRelativePath ( path ) ; if ( fatherNode == null ) { response . put ( "stauts" , 404 ) ; response . put ( "message" , "One element of the path not found" ) ; getResponse ( ) . setStatus ( new Status ( 404 ) ) ; return new JsonRepresentation ( response ) ; } father = fatherNode . optString ( Constants . ID_NODE ) ; JSONObject created = null ; if ( ! type . isEmpty ( ) || ! type . equals ( "" ) ) { created = dbPolicyConnection . saveFolder ( father , namePolicy , username ) ; } else if ( policyID . isEmpty ( ) || policyID . equals ( "" ) ) { if ( dbPolicyConnection . isValid ( father , namePolicy ) ) { created = dbPolicyConnection . savePolicy ( father , namePolicy , policy , username , blockly_xml , Constants . DEFAULT_VERSION ) ; } else { try { response = new JSONObject ( ) ; response . put ( "message" , "Conflict, Policy with name " + namePolicy + " already exists" ) ; response . put ( "code" , 409 ) ; getResponse ( ) . setStatus ( new Status ( 409 ) ) ; return new JsonRepresentation ( response ) ; } catch ( JSONException e ) { logger . error ( "JSONException" , e ) ; e . printStackTrace ( ) ; } } } else { created = dbPolicyConnection . updatePolicy ( father , namePolicy , policy , username , blockly_xml , policyID ) ; } if ( created . optString ( "status" ) . equalsIgnoreCase ( "ok" ) ) { status = 201 ; response . put ( "code" , status ) ; } message = created . optString ( "message" ) ; } catch ( Exception e ) { logger . error ( "Exception" , e ) ; e . printStackTrace ( ) ; response = new JSONObject ( ) ; try { response . put ( "faultCode" , Status . SERVER_ERROR_INTERNAL ) ; response . put ( "message" , "Internal Server Error" ) ; } catch ( JSONException e1 ) { logger . error ( "JSONException" , e1 ) ; e1 . printStackTrace ( ) ; } getResponse ( ) . setStatus ( new Status ( 500 ) ) ; return new JsonRepresentation ( response ) ; } getResponse ( ) . setStatus ( new Status ( status ) ) ; logger . info ( message ) ; return new JsonRepresentation ( response ) ; }
Stores an element in the Database.
Stores an element in the Database .
//			dbPolicyConnection.dbManager.disconnect();