/// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara
///
/// Permission is hereby granted, free of charge, to any person obtaining
/// a copy of this software and associated documentation files (the
/// "Software"), to deal in the Software without restriction, including
/// without limitation the rights to use, copy, modify, merge, publish,
/// distribute, sublicense, and/or sell copies of the Software, and to
/// permit persons to whom the Software is furnished to do so, subject to
/// the following conditions:
///
/// The above copyright notice and this permission notice shall be included
/// in all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
/// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
/// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///
using System;
using System.Collections.Generic;
using System.Numerics;
using System.IO;
using System.Text.Json.Serialization;
namespace CoreLibrary
{
///
/// Attached data class contains the pat of attached source file,
/// the area/points the data is linked to in the map and also list
/// of meta data that is relative to the attached data.
///
public class AttachedData
{
public List AttachmentPoints { get; set; }
public RelativeFilepath Filepath { get; set; }
public Metas Metas { get; set; }
public string DataName { get; set; }
[JsonIgnore]
public string DataType
{
get
{
string extension = Path.GetExtension(Filepath.GetPath());
return extension[1..];
}
}
[JsonIgnore]
public string FirstPoint
{
get
{
if (AttachmentPoints.Count == 0) return "";
var point = AttachmentPoints[0];
return "(" + point.X + "," + point.Y + "," + point.Z + ")";
}
}
///
/// Constructor for JSON serialization and deserialization. Note that the
/// parameter names must match the attribute names (non case sensitive)
///
/// Points the data is attached to
/// Relative filepath to the file
/// List of meta datas
/// Name of the attached data
/// Type of the data
[JsonConstructor]
public AttachedData(List attachmentpoints, RelativeFilepath filepath, Metas metas, string dataname, string datatype, string firstpoint)
{
AttachmentPoints = attachmentpoints;
Filepath = filepath;
Metas = metas;
DataName = dataname;
}
public AttachedData(string filePath, string dataName, List> coord)
{
Filepath = new RelativeFilepath(filePath);
DataName = dataName;
AttachmentPoints = new();
for (int i = 0; i < coord.Count; i++)
{
AttachmentPoints.Add(new Vector3(coord[i][0], coord[i][1], coord[i][2]));
}
Metas = new();
}
///
/// Sets filepath to be relative to given filepath.
///
/// Filepath the relation is set to.
public void SetDataRelation(string relativeTo)
{
Filepath.SetRelation(relativeTo);
}
///
/// Adds meta data to the attached data.
///
/// Metadata to be added.
public void AddMetaData(Meta meta)
{
Metas.AddData(meta);
}
///
/// Sets metadata of specified name to the first metadata that maches.
/// If no metadata name matches, creates new metadata.
///
/// Name of existing or new metadata.
/// Value with existing name was replaced.
///
public bool SetMetaData(string name, string value)
{
return Metas.SetData(name, value);
}
///
/// Checks if the AttachedData filepath exists.
///
/// Does the filepath exist
public bool CheckFilePathExists()
{
return File.Exists(Filepath.GetPath());
}
///
/// Returns the data name.
///
/// Name of the metadata
public override string ToString()
{
return DataName;
}
}
}