//******************************************************************************************************
// MetadataService.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 08/28/2009 - Pinal C. Patel
// Generated original version of source code.
// 09/10/2009 - Pinal C. Patel
// Modified ReadMetadata() overloads to remove try-catch and check for null reference instead.
// 09/15/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/15/2009 - Pinal C. Patel
// Changed the default port for the service from 5151 to 6151.
// 03/30/2010 - Pinal C. Patel
// Added check to verify that the service is enabled before processing incoming requests.
// 10/11/2010 - Mihir Brahmbhatt
// Updated header and license agreement.
// 11/07/2010 - Pinal C. Patel
// Modified to fix breaking changes made to SelfHostingService.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.ServiceModel;
using GSF.Historian.Files;
using GSF.Parsing;
namespace GSF.Historian.DataServices
{
///
/// Represents a REST web service for historian metadata.
///
///
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MetadataService : DataService, IMetadataService
{
#region [ Constructors ]
///
/// Initializes a new instance of the class.
///
public MetadataService()
{
Endpoints = "http.rest://localhost:6151/historian";
}
#endregion
#region [ Methods ]
///
/// Writes received in format to the .
///
/// An object.
public void WriteMetadataAsXml(SerializableMetadata metadata)
{
WriteMetadata(metadata);
}
///
/// Writes received in format to the .
///
/// An object.
public void WriteMetadataAsJson(SerializableMetadata metadata)
{
WriteMetadata(metadata);
}
///
/// Reads all metadata from the and sends it in format.
///
/// An object.
public SerializableMetadata ReadAllMetadataAsXml()
{
return ReadMetadata();
}
///
/// Reads a subset of metadata from the and sends it in format.
///
/// A comma or semi-colon delimited list of IDs for which metadata is to be read.
/// An object.
public SerializableMetadata ReadSelectMetadataAsXml(string idList)
{
return ReadMetadata(idList);
}
///
/// Reads a subset of metadata from the and sends it in format.
///
/// Starting ID in the ID range for which metadata is to be read.
/// Ending ID in the ID range for which metadata is to be read.
/// An object.
public SerializableMetadata ReadRangeMetadataAsXml(string fromID, string toID)
{
return ReadMetadata(fromID, toID);
}
///
/// Reads all metadata from the and sends it in format.
///
/// An object.
public SerializableMetadata ReadAllMetadataAsJson()
{
return ReadMetadata();
}
///
/// Reads a subset of metadata from the and sends it in format.
///
/// A comma or semi-colon delimited list of IDs for which metadata is to be read.
/// An object.
public SerializableMetadata ReadSelectMetadataAsJson(string idList)
{
return ReadMetadata(idList);
}
///
/// Reads a subset of metadata from the and sends it in format.
///
/// Starting ID in the ID range for which metadata is to be read.
/// Ending ID in the ID range for which metadata is to be read.
/// An object.
public SerializableMetadata ReadRangeMetadataAsJson(string fromID, string toID)
{
return ReadMetadata(fromID, toID);
}
private void WriteMetadata(SerializableMetadata metadata)
{
try
{
// Abort if services is not enabled.
if (!Enabled)
return;
// Ensure that data archive is available.
if (Archive == null)
throw new NullReferenceException("Archive");
// Write all metadata records to the archive.
foreach (SerializableMetadataRecord record in metadata.MetadataRecords)
{
Archive.WriteMetaData(record.HistorianID, record.Deflate().BinaryImage());
}
}
catch (Exception ex)
{
// Notify about the encountered processing exception.
OnServiceProcessException(ex);
throw;
}
}
private SerializableMetadata ReadMetadata()
{
try
{
// Abort if services is not enabled.
if (!Enabled)
return null;
// Ensure that data archive is available.
if (Archive == null)
throw new NullReferenceException("Archive");
// Read all metadata records from the archive.
int id = 0;
byte[] buffer;
SerializableMetadata metadata = new SerializableMetadata();
List records = new List();
while (true)
{
buffer = Archive.ReadMetaData(++id);
if ((object)buffer == null)
// No more records.
break;
records.Add(new SerializableMetadataRecord(new MetadataRecord(id, MetadataFileLegacyMode.Enabled, buffer, 0, buffer.Length)));
}
metadata.MetadataRecords = records.ToArray();
return metadata;
}
catch (Exception ex)
{
// Notify about the encountered processing exception.
OnServiceProcessException(ex);
throw;
}
}
private SerializableMetadata ReadMetadata(string idList)
{
try
{
// Abort if services is not enabled.
if (!Enabled)
return null;
// Ensure that data archive is available.
if (Archive == null)
throw new NullReferenceException("Archive");
// Read specified metadata records from the archive.
int id;
byte[] buffer;
SerializableMetadata metadata = new SerializableMetadata();
List records = new List();
foreach (string singleID in idList.Split(',', ';'))
{
buffer = Archive.ReadMetaData(id = int.Parse(singleID));
if ((object)buffer == null)
// ID is invalid.
continue;
records.Add(new SerializableMetadataRecord(new MetadataRecord(id, MetadataFileLegacyMode.Enabled, buffer, 0, buffer.Length)));
}
metadata.MetadataRecords = records.ToArray();
return metadata;
}
catch (Exception ex)
{
// Notify about the encountered processing exception.
OnServiceProcessException(ex);
throw;
}
}
private SerializableMetadata ReadMetadata(string fromID, string toID)
{
try
{
// Abort if services is not enabled.
if (!Enabled)
return null;
// Ensure that data archive is available.
if (Archive == null)
throw new NullReferenceException("Archive");
// Read specified metadata records from the archive.
byte[] buffer;
SerializableMetadata metadata = new SerializableMetadata();
List records = new List();
for (int id = int.Parse(fromID); id <= int.Parse(toID); id++)
{
buffer = Archive.ReadMetaData(id);
if ((object)buffer == null)
// ID is invalid.
continue;
records.Add(new SerializableMetadataRecord(new MetadataRecord(id, MetadataFileLegacyMode.Enabled, buffer, 0, buffer.Length)));
}
metadata.MetadataRecords = records.ToArray();
return metadata;
}
catch (Exception ex)
{
// Notify about the encountered processing exception.
OnServiceProcessException(ex);
throw;
}
}
#endregion
}
}