public String insertProfile(String inJson) {
		String retval = new String();
		boolean retbool;
		int retcode;
		
		try {
			JSONObject inJo  = new JSONObject(inJson);
			JSONObject inPr  = inJo.getJSONObject("profiles");
			JSONArray inConf = inJo.getJSONArray("configuration");
			JSONArray inHost = inJo.getJSONArray("hosts");
			
			Connection conn = dbConn.getConnection();
			
			String key = inPr.getString("key");
			
			if( key == null || key.length() < 2) { 
				JSONObject kjo = new JSONObject(this.generateKey());
				key = kjo.getJSONObject("data").getString("key");
			}
			else {
				this.deleteProfilesByKey(key);
			}
			
			StringBuffer sb = new StringBuffer();
			sb.append("insert into app.profiles ");
			sb.append("(id,key,title,description,reverse_cost,rid,public,");
			sb.append("enabled,pgr_sp,pgr_dd) values ");
			sb.append("(?,?,?,?,?,?,?,?,?,?)");
			
			int recId = 1;
			
			Statement stmt  = conn.createStatement();
			ResultSet rs    = stmt.executeQuery(
					"select nextval('app.profiles_id_seq') as id");
			
			while( rs.next() )
				recId = rs.getInt("id");
			
			PreparedStatement pstmt = conn.prepareStatement(sb.toString());
			pstmt.setInt    (1, recId);
			pstmt.setString (2, key);
			pstmt.setString (3, inPr.getString("title"));
			pstmt.setString (4, inPr.getString("description"));
			pstmt.setBoolean(5, inPr.getBoolean("reverse_cost"));
			pstmt.setInt    (6, inPr.getInt("rid"));
			pstmt.setBoolean(7, inPr.getBoolean("public"));
			pstmt.setBoolean(8, inPr.getBoolean("enabled"));
			pstmt.setBoolean(9, inPr.getBoolean("pgr_sp"));
			pstmt.setBoolean(10,inPr.getBoolean("pgr_dd"));
			pstmt.execute();
			
			PreparedStatement pstmt2 = conn.prepareStatement(
					"insert into app.hosts(id,host) values (?,?)");
			
			for(int j=0;j<inHost.size();j++) {
				pstmt2.setInt(1, recId);
				pstmt2.setString(2, inHost.getString(j));
				pstmt2.execute();
			}
			
			PreparedStatement pstmt3 = conn.prepareStatement(
					"insert into app.configuration "+
					"(pid,cid,speed,enabled,priority) "+
					"values (?,?,?,?,?)" );
			
			for(int k=0;k<inConf.size();k++) {
				JSONObject conf = inConf.getJSONObject(k);
				pstmt3.setInt(1, recId);
				pstmt3.setInt(2, conf.getInt("cid"));
				pstmt3.setInt(3, conf.getInt("speed"));
				pstmt3.setBoolean(4, conf.getBoolean("enabled"));
				pstmt3.setDouble( 5, conf.getDouble("priority"));
				pstmt3.execute();
			}
			
			PreparedStatement pstmt4 = conn.prepareStatement(
					"select create_profile_view(?,?)" );
			pstmt4.setInt   (1, recId);
			pstmt4.setString(2, RouteProperties.getTable());
			pstmt4.execute();
			
			retval = "Sucessfully completed";
			retcode= 201;
			retbool= true;
			
			rs.close();
			stmt.close();
			pstmt.close();
			pstmt2.close();
			pstmt3.close();
			pstmt4.close();
			conn.close();
		}
		catch( JSONException je ) {
			retval = "JSON Exception Error";
			retcode= 404;
			retbool= false;
			je.printStackTrace();
		}
		catch( Exception e ) {
			retval = "Database Exception Error";
			retcode= 404;
			retbool= false;
			e.printStackTrace();
		}
		
		JSONObject robj = new JSONObject();
		JSONObject rstat= new JSONObject();
		try {
			rstat.put("code"   , retcode);
			rstat.put("success", retbool );
			rstat.put("message", retval);
			
			robj.put("status", rstat);
		}
		catch(Exception exception) {;}
		
		return robj.toString();
	}