Policies_Storage_Service/src/com/fincons/policiesstorage/restlet/DBPolicyConnection.java
/**
	 * Saves a folder node inside the Database using an OrientDB query
	 * 
	 * @param father
	 *            The ID of the father node
	 * @param nameFodler
	 *            The name of the folder to create
	 * @param username
	 *            The user that creates the folder
	 * @return JSON Object with the information of the new folder created, the
	 *         status of the query execution and a message.
	 */
Saves a folder node inside the Database using an OrientDB query

@param father
The ID of the father node
@param nameFodler
The name of the folder to create
@param username
The user that creates the folder
@return JSON Object with the information of the new folder created, the
status of the query execution and a message.
Saves a folder node inside the Database using an OrientDB query @param father The ID of the father node @param nameFodler The name of the folder to create @param username The user that creates the folder @return JSON Object with the information of the new folder created the status of the query execution and a message .
public JSONObject saveFolder(String father, String nameFodler, String username) {
		logger.info("Start creation folder " + nameFodler);
		JSONObject result = null;
		try {
			result = new JSONObject();
			if (isValid(father, nameFodler)) {
				String queryAddFolder = Constants.CREATE_DIRECTORY;
				Date date = new Date();
				Timestamp timestamp = (new Timestamp(date.getTime()));
				HashMap<String, Object> paramsFolder = new HashMap<String, Object>();
				paramsFolder.put("name", nameFodler);
				paramsFolder.put("timestamp", timestamp);
				paramsFolder.put("created_by", username);
				Integer con_number = dbManager.connect(pathDB, usernameDB, passwordDB);
				JSONObject creationResult = new JSONObject();
				try {
					creationResult = (JSONObject) dbManager.executeQuery(con_number, queryAddFolder, paramsFolder);
				} catch (Exception e1) {
					dbManager.rollback(con_number);
					logger.error("Error while creating Directory Node with name " + nameFodler, e1);
					creationResult.put("status", "ERROR");
					creationResult.put("message", e1.getMessage());
				}
				dbManager.disconnect(con_number);
				String newNodeID = creationResult.optString(Constants.ID_NODE);
				String statusQuery = creationResult.optString("status");
				if (statusQuery.equalsIgnoreCase("ok")) {
					logger.info("Folder " + nameFodler + " 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.DIRECTORY_CLASS);
					logger.info("Start create connection between the father directory and the new 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);
					}
					dbManager.disconnect(con_number2);
					if (edgeResult.optString("status").equalsIgnoreCase("ok")) {
						result.put("status", "OK");
						result.put("message", "Folder successfully created");
					} else {
						result.put("status", "ERROR");
						result.put("message", "Folder creation failed");
					}
				}
			} else {
				result.put("status", "ERROR");
				result.put("message", "Folder with name " + nameFodler + " already exist");
			}
		} catch (JSONException e) {
			logger.error("JSON Exception " + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
public JSONObject saveFolder ( String father , String nameFodler , String username ) { logger . info ( "Start creation folder " + nameFodler ) ; JSONObject result = null ; try { result = new JSONObject ( ) ; if ( isValid ( father , nameFodler ) ) { String queryAddFolder = Constants . CREATE_DIRECTORY ; Date date = new Date ( ) ; Timestamp timestamp = ( new Timestamp ( date . getTime ( ) ) ) ; HashMap < String , Object > paramsFolder = new HashMap < String , Object > ( ) ; paramsFolder . put ( "name" , nameFodler ) ; paramsFolder . put ( "timestamp" , timestamp ) ; paramsFolder . put ( "created_by" , username ) ; Integer con_number = dbManager . connect ( pathDB , usernameDB , passwordDB ) ; JSONObject creationResult = new JSONObject ( ) ; try { creationResult = ( JSONObject ) dbManager . executeQuery ( con_number , queryAddFolder , paramsFolder ) ; } catch ( Exception e1 ) { dbManager . rollback ( con_number ) ; logger . error ( "Error while creating Directory Node with name " + nameFodler , e1 ) ; creationResult . put ( "status" , "ERROR" ) ; creationResult . put ( "message" , e1 . getMessage ( ) ) ; } dbManager . disconnect ( con_number ) ; String newNodeID = creationResult . optString ( Constants . ID_NODE ) ; String statusQuery = creationResult . optString ( "status" ) ; if ( statusQuery . equalsIgnoreCase ( "ok" ) ) { logger . info ( "Folder " + nameFodler + " 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 . DIRECTORY_CLASS ) ; logger . info ( "Start create connection between the father directory and the new 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 ) ; } dbManager . disconnect ( con_number2 ) ; if ( edgeResult . optString ( "status" ) . equalsIgnoreCase ( "ok" ) ) { result . put ( "status" , "OK" ) ; result . put ( "message" , "Folder successfully created" ) ; } else { result . put ( "status" , "ERROR" ) ; result . put ( "message" , "Folder creation failed" ) ; } } } else { result . put ( "status" , "ERROR" ) ; result . put ( "message" , "Folder with name " + nameFodler + " already exist" ) ; } } catch ( JSONException e ) { logger . error ( "JSON Exception " + e . getMessage ( ) ) ; e . printStackTrace ( ) ; } return result ; }
Saves a folder node inside the Database using an OrientDB query
Saves a folder node inside the Database using an OrientDB query