private byte[] createErrorObject(int codeNumber, String errorMessageSummary, String[] errorMessageDetails) { if (errorMessageSummary.Length == 0 || errorMessageSummary == null) { throw new Exception("Invalid error message specified."); } JSONObject errorJSON = new JSONObject(); errorJSON.AddLong("code", codeNumber); errorJSON.AddString("message", errorMessageSummary); if (errorMessageDetails == null) { errorJSON.AddString("details", "No error details specified."); } else { String errorMessages = ""; for (int i = 0; i < errorMessageDetails.Length; i++) { errorMessages = errorMessages + errorMessageDetails[i] + "\n"; } errorJSON.AddString("details", errorMessages); } JSONObject error = new JSONObject(); errorJSON.AddJSONObject("error", errorJSON); return(Encoding.UTF8.GetBytes(errorJSON.ToJSONString(null))); }
/** * Returns basic Utility Network properties on the root resource */ private byte[] RootResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; if (unDataset == null) { throw new RestErrorException("No Utility Network found"); } JSONObject result = new JSONObject(); try { result.AddString("description", "UN Tracing is a REST SOE with 2 operations called \"getMVTransformerAssetIds\" and \"getCustomers\"."); result.AddString( "usage", "The \"getMVTransformerAssetIds\" operation returns a list of medium voltage transformer asset IDs.\n" + "These asset IDs can be then used as input in the \"getCustomers\" operation.\n" + "The \"getCustomers\" operation runs a network trace starting at a specified medium voltage transformer\n" + " and returns the list of service points and associated customer information."); result.AddString("note", "Make sure that the network topology is enabled. If it is the case, then \"hasValidNetworkTopology\" property should be 'true'."); result.AddString("networkName", unLayerInfo.Name); // Get UN properties IPropertySet unProps = GetUNProperties(); object[] nameArray = new object[1]; object[] valueArray = new object[1]; unProps.GetAllProperties(out nameArray[0], out valueArray[0]); object[] names = (object[])nameArray[0]; object[] values = (object[])valueArray[0]; JSONObject joUNProps = new JSONObject(); for (int i = 0; i < unProps.Count; i++) { joUNProps.Add((String)names[i], values[i]); } result.AddJSONObject("networkProperties", joUNProps); } catch (Exception e) { logger.LogMessage(ServerLogger.msgType.debug, soe_name + " root resource", 500, "Exception " + e.GetType().Name + " " + e.Message + " " + e.StackTrace); JSONObject error = soeUtil.CreateErrorJSON(500, "Unable to complete operation", e.HResult, e.Message); return(Encoding.UTF8.GetBytes(error.ToJSONString(null))); } return(Encoding.UTF8.GetBytes(result.ToJSONString(null))); }
private byte[] SrvcPropResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = "{\"Content-Type\" : \"application/json\"}"; IPropertySet pPropSet = SOIBase.QueryConfigurationProperties(this.soHelper.ServerObject.ConfigurationName, this.soHelper.ServerObject.TypeName); JSONObject jo = new JSONObject(); jo.AddString("MaxRecordCount", pPropSet.GetProperty("MaxRecordCount").ToString()); jo.AddString("MaxImageHeight", pPropSet.GetProperty("MaxImageHeight").ToString()); jo.AddString("MaxImageWidth", pPropSet.GetProperty("MaxImageWidth").ToString()); jo.AddString("PhysicalOutputDirectory", pPropSet.GetProperty("outputDir").ToString()); jo.AddString("PhysicalCacheDirectory", pPropSet.GetProperty("cacheDir").ToString()); JSONObject result = new JSONObject(); result.AddJSONObject("serviceproperties", jo); return(Encoding.UTF8.GetBytes(result.ToJSONString(null))); }
public JSONObject CreateErrorJSON(int code, string message, int extendedCode = 0, string detailsContent = "") { JSONObject response = new JSONObject(); JSONObject error = new JSONObject(); error.Add("code", code); error.Add("message", message); if (extendedCode != 0) { error.Add("extendedCode", extendedCode); } JSONArray details = new JSONArray(); details.AddString(detailsContent); error.AddJSONArray("details", details); response.AddJSONObject("error", error); return(response); }
/** * Returns a list of customers serviced by the specified transformer */ private byte[] GetCustomersOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; if (unDataset == null) { throw new RestErrorException("No Utility Network found"); } JSONObject result = new JSONObject(); try { string xfrAssetID = ""; operationInput.TryGetString("transformerAssetId", out xfrAssetID); if (string.IsNullOrEmpty(xfrAssetID)) { long?assetNumber = null; operationInput.TryGetAsLong("transformerAssetId", out assetNumber); if (assetNumber == null) { throw new ArgumentNullException("transformerAssetId"); } else { xfrAssetID = assetNumber.ToString(); } } // Get the transformer feature global ID from the asset ID string xfrWhereClause = "ASSETGROUP=" + MV_XFR_ASSETGROUP.ToString() + " AND ASSETID=" + xfrAssetID; IStringArray xfrOutFields = new StrArrayClass(); xfrOutFields.Add("GLOBALID"); xfrOutFields.Add("ASSETTYPE"); xfrOutFields.Add("SHAPE"); IPropertySetArray xfrQryResults = QueryDevices(xfrWhereClause, xfrOutFields); if (xfrQryResults == null || xfrQryResults.Count == 0) { throw new RestErrorException("No medium voltage transformer found for asset id: " + xfrAssetID); } string xfrGlobalID = (string)xfrQryResults.Element[0].GetProperty("GLOBALID"); short xfrAssetType = (short)xfrQryResults.Element[0].GetProperty("ASSETTYPE"); // Execute a downstream trace to find all customer low voltage service points IStringArray lvsGlobalIDs = FindLVServicePoints(xfrGlobalID); // Prepare response JSON JSONObject xfrInfo = new JSONObject(); xfrInfo.AddString("ASSETID", xfrAssetID); xfrInfo.AddString("GLOBALID", xfrGlobalID); xfrInfo.AddLong("ASSETGROUP", MV_XFR_ASSETGROUP); xfrInfo.AddLong("ASSETTYPE", xfrAssetType); IJSONObject xfrPointJson = new JSONObject(); IGeometry xfrPoint = (IGeometry)xfrQryResults.Element[0].GetProperty("SHAPE"); IJSONConverterGeometry geoSerializer = new JSONConverterGeometryClass(); geoSerializer.QueryJSONGeometry(xfrPoint, false, xfrPointJson); xfrInfo.AddJSONObject("geometry", xfrPointJson); result.AddJSONObject("transformer", xfrInfo); IJSONArray servicePoints = new JSONArrayClass(); // Get additional service point info if any returned from trace if (lvsGlobalIDs != null && lvsGlobalIDs.Count > 0) { // Query devices to get more details about service points string lvsGlobalIDList = ""; for (int i = 0; i < lvsGlobalIDs.Count; i++) { lvsGlobalIDList += (i == 0) ? "" : ","; lvsGlobalIDList += "'" + lvsGlobalIDs.Element[i] + "'"; } string lvsWhereClause = "GLOBALID IN (" + lvsGlobalIDList + ")"; IStringArray lvsOutFields = new StrArrayClass(); lvsOutFields.Add("GLOBALID"); lvsOutFields.Add("ASSETTYPE"); lvsOutFields.Add("ASSETGROUP"); lvsOutFields.Add("ASSETID"); lvsOutFields.Add("SHAPE"); IPropertySetArray lvsQryResults = QueryDevices(lvsWhereClause, lvsOutFields); // Get customer details if (lvsQryResults != null && lvsQryResults.Count > 0) { for (int i = 0; i < lvsQryResults.Count; i++) { IPropertySet lvs = lvsQryResults.Element[i]; JSONObject servicePoint = new JSONObject(); servicePoint.AddString("GLOBALID", (string)lvs.GetProperty("GLOBALID")); servicePoint.AddString("ASSETID", (string)lvs.GetProperty("ASSETID")); servicePoint.AddLong("ASSETGROUP", (int)lvs.GetProperty("ASSETGROUP")); servicePoint.AddLong("ASSETTYPE", (short)lvs.GetProperty("ASSETTYPE")); IGeometry lvsPoint = (IGeometry)lvs.GetProperty("SHAPE"); IJSONObject lvsPointJson = new JSONObject(); geoSerializer.QueryJSONGeometry(lvsPoint, false, lvsPointJson); servicePoint.AddJSONObject("geometry", lvsPointJson); JSONObject customerInfo = GetCustomerInfoJSON(); servicePoint.AddJSONObject("customerInfo", customerInfo); servicePoints.AddJSONObject(servicePoint); } } } result.AddJSONArray("servicePoints", servicePoints); } catch (Exception e) { logger.LogMessage(ServerLogger.msgType.debug, soe_name + " getCustomers", 500, "Exception " + e.GetType().Name + " " + e.Message + " " + e.StackTrace); JSONObject error = soeUtil.CreateErrorJSON(500, "Unable to complete operation", e.HResult, e.Message); return(Encoding.UTF8.GetBytes(error.ToJSONString(null))); } return(Encoding.UTF8.GetBytes(result.ToJSONString(null))); }
private byte[] createErrorObject(int codeNumber, String errorMessageSummary, String[] errorMessageDetails) { if (errorMessageSummary.Length == 0 || errorMessageSummary == null) { throw new Exception("Invalid error message specified."); } JSONObject errorJSON = new JSONObject(); errorJSON.AddLong("code", codeNumber); errorJSON.AddString("message", errorMessageSummary); if (errorMessageDetails == null) { errorJSON.AddString("details", "No error details specified."); } else { String errorMessages = ""; for (int i = 0; i < errorMessageDetails.Length; i++) { errorMessages = errorMessages + errorMessageDetails[i] + "\n"; } errorJSON.AddString("details", errorMessages); } JSONObject error = new JSONObject(); error.AddJSONObject("error", errorJSON); return Encoding.UTF8.GetBytes(error.ToJSONString(null)); }
private byte[] ProcessEditAreas(IServerObject serverObject, string versionName) { try { // Open utility network IDataset unDataset = _soiUtil.GetUNDataset(serverObject, versionName); IWorkspace workspace = (IWorkspace)unDataset.Workspace; // Get all the dirty areas in the given validation area IBaseNetwork baseNetwork = (IBaseNetwork)unDataset; ITable dirtyAreaTable = baseNetwork.DirtyAreaTable; string shapeFieldName = ((IFeatureClass)dirtyAreaTable).ShapeFieldName; int areaFieldIndex = dirtyAreaTable.FindField(shapeFieldName); int creatorFieldIndex = dirtyAreaTable.FindField("CREATOR"); // Get UN schema version IDatasetComponent dsComponent = (IDatasetComponent)unDataset; IDEBaseNetwork deBaseNetwork = (IDEBaseNetwork)dsComponent.DataElement; int unVersion = deBaseNetwork.SchemaGeneration; // Get inserts made to dirty areas table in the current version // For UN > V4, Errors are discarded (ERROCODE>0) to only retain true dirty areas // Note that changes made in the last edit session must be saved for this to work // as it is not possible to get the modifications until they have been saved. IVersionedTable versionedTable = (IVersionedTable)dirtyAreaTable; QueryFilter qryFilter = null; if (unVersion >= 4) { qryFilter = new QueryFilter(); qryFilter.WhereClause = _errorCodeFName + "=0"; } IDifferenceCursor diffCursor = versionedTable.Differences(dirtyAreaTable, esriDifferenceType.esriDifferenceTypeInsert, qryFilter); // Loop through added rows to construct the modified zone extent int editCount = 0; int OID; IRow diffRow; IEnvelope editZone = null; string creator = ""; diffCursor.Next(out OID, out diffRow); // Return an error if no dirty areas found as it may be because the last edits were not saved if (diffRow == null) { JSONObject responseJSON = new JSONObject(); responseJSON.AddBoolean("success", false); JSONObject errorJSON = new JSONObject(); errorJSON.AddLong("extendedCode", (int)fdoError.FDO_E_DIRTY_AREA_BUILD_EXTENT_DO_NOT_INTERSECT); errorJSON.AddString("message", "A dirty area is not present within the validate network topology input extent. A validate network topology process did not occur."); JSONArray detailsJSON = new JSONArray(); detailsJSON.AddString("Make sure to save edits before validating."); errorJSON.AddJSONArray("details", detailsJSON); responseJSON.AddJSONObject("error", errorJSON); return(Encoding.UTF8.GetBytes(responseJSON.ToJSONString(null))); } while (diffRow != null) { editCount += 1; creator = diffRow.Value[creatorFieldIndex].ToString(); IGeometry rowShape = (IGeometry)diffRow.Value[areaFieldIndex]; IEnvelope rowArea = rowShape.Envelope; if (editZone != null) { editZone.Union(rowArea); } else { editZone = rowArea.Envelope; } diffCursor.Next(out OID, out diffRow); } diffCursor = null; versionedTable = null; workspace = null; // Add new or modify existing edit zone if (editZone != null) { AddEditArea(serverObject, creator, versionName, editCount, editZone); } } catch (Exception e) { _serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".AddEditArea()", 200, "Error while adding edit are: " + e.ToString()); } return(null); }