/// <summary> /// wandelt bexis primardaten (rows) in bexis 2 datatuples um /// </summary> /// <param name="data"></param> /// <param name="dataStructureId"></param> /// <param name="observationIndex"></param> /// <returns></returns> public DataTuple XmlRowReader(XmlDocument data, long dataStructureId, int observationIndex) { DataStructureManager dataStructureManager = new DataStructureManager(); DataTuple dataRow = new DataTuple(); StructuredDataStructure dataStructure = dataStructureManager.StructuredDataStructureRepo.Get(dataStructureId); this.StructuredDataStructure = dataStructure; List<VariableIdentifier> variableIdentifierList = new List<VariableIdentifier>(); foreach (Variable variable in dataStructure.Variables) { VariableIdentifier vi = new VariableIdentifier(); vi.id = variable.Id; vi.name = variable.Label; vi.systemType = variable.DataAttribute.DataType.SystemType.ToString(); variableIdentifierList.Add(vi); } this.SubmitedVariableIdentifiers = variableIdentifierList; List<string> dataList = XmlRowToStringList(data, variableIdentifierList); int startRow = 0; int indexOfRow = startRow + observationIndex; //List<Error> errors = ValidateRow(dataList, indexOfRow); //if (errors.Count == 0) { dataRow = ReadRow(dataList, indexOfRow); } return dataRow; }
/// <summary> /// create a new dataset, check it out to create the first version, add a tuple to it. /// </summary> /// <returns></returns> private Int64 createDatasetVersion() { DataStructureManager dsManager = new DataStructureManager(); ResearchPlanManager rpManager = new ResearchPlanManager(); DatasetManager dm = new DatasetManager(); MetadataStructureManager mdsManager = new MetadataStructureManager(); MDS.MetadataStructure mds = mdsManager.Repo.Query().First(); Dataset ds = dm.CreateEmptyDataset(dsManager.StructuredDataStructureRepo.Get(1), rpManager.Repo.Get(1), mds); if (dm.IsDatasetCheckedOutFor(ds.Id, "Javad") || dm.CheckOutDataset(ds.Id, "Javad")) { DatasetVersion workingCopy = dm.GetDatasetWorkingCopy(ds.Id); //DataTuple changed = dm.GetDatasetVersionEffectiveTuples(workingCopy).First(); //changed.VariableValues.First().Value = (new Random()).Next().ToString(); DataTuple dt = dm.DataTupleRepo.Get(1); // its sample data List<DataTuple> tuples = new List<DataTuple>(); for (int i = 0; i < 10000; i++) { DataTuple newDt = new DataTuple(); newDt.XmlAmendments = dt.XmlAmendments; newDt.XmlVariableValues = dt.XmlVariableValues; // in normal cases, the VariableValues are set and then Dematerialize is called newDt.Materialize(); newDt.OrderNo = i; //newDt.TupleAction = TupleAction.Created;//not required //newDt.Timestamp = DateTime.UtcNow; //required? no, its set in the Edit //newDt.DatasetVersion = workingCopy;//required? no, its set in the Edit tuples.Add(newDt); } dm.EditDatasetVersion(workingCopy, tuples, null, null); dm.CheckInDataset(ds.Id, "for testing purposes", "Javad"); dm.DatasetVersionRepo.Evict(); dm.DataTupleRepo.Evict(); dm.DatasetRepo.Evict(); workingCopy.PriliminaryTuples.Clear(); workingCopy = null; } var dsId = ds.Id; ds = null; return (dsId); }
/// <summary> /// Read Row and convert each value into a variableValue /// and each row to a Datatuple /// </summary> /// <param name="row">List of values in one row</param> /// <param name="indexOfRow">Currently row index</param> /// <returns>DataTuple</returns> public DataTuple ReadRow(List<string> row, int indexOfRow) { DataTuple dt = new DataTuple(); DataStructureManager dsm = new DataStructureManager(); string value = ""; // convert row to List<VariableValue> for(int i=0;i< row.Count();i++ ) { VariableIdentifier variableIdentifier = this.SubmitedVariableIdentifiers.ElementAt(i); long variableId = 0; if (variableIdentifier.id > 0) variableId = this.SubmitedVariableIdentifiers.ElementAt(i).id; else variableId = getVariableUsage(variableIdentifier).Id; // if variable from systemtype datatime // maybee needs to convert into the default datetime culture format if (this.StructuredDataStructure.Variables.Where(p => p.Id.Equals(variableId)).FirstOrDefault().DataAttribute.DataType.SystemType.Equals("DateTime")) { value = IOUtility.ConvertDateToCulture(row[i]); } else { if (this.StructuredDataStructure.Variables.Where(p => p.Id.Equals(variableId)).FirstOrDefault().DataAttribute.DataType.SystemType.Equals("Double")|| this.StructuredDataStructure.Variables.Where(p => p.Id.Equals(variableId)).FirstOrDefault().DataAttribute.DataType.SystemType.Equals("Decimal")|| this.StructuredDataStructure.Variables.Where(p => p.Id.Equals(variableId)).FirstOrDefault().DataAttribute.DataType.SystemType.Equals("Float")) { value = row[i]; if (Info.Decimal.Equals(DecimalCharacter.comma)) { if(value.Contains(".")) value = value.Replace(".",""); if(value.Contains(",")) value = value.Replace(',', '.'); } if (Info.Decimal.Equals(DecimalCharacter.point)) { if (value.Contains(",")) value = value.Remove(','); } } else { value = row[i]; } } dt.VariableValues.Add(DatasetManager.CreateVariableValue(value,"", DateTime.Now, DateTime.Now, new ObtainingMethod(), variableId, new List<ParameterValue>())); } return dt; }
public bool DeleteDataTuple(DataTuple entity) { Contract.Requires(entity != null); Contract.Requires(entity.Id >= 0); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<DataTuple> repo = uow.GetRepository<DataTuple>(); entity = repo.Reload(entity); entity.DatasetVersion = null; repo.Delete(entity); uow.Commit(); } // if any problem was detected during the commit, an exception will be thrown! return (true); }
/// <summary> /// Using the provided values creates a data tuple, attaches it to the version and persists it in the database. /// This method does not affect the status of the dataset version. /// </summary> /// <param name="orderNo">The order of the data tuple in the list of tuples</param> /// <param name="variableValues">The values to be considered as the data tuple. They must be attached to their corresponding variables according to the dataset's data structure.</param> /// <param name="amendments">Each data tuple can have amendments and if provided, the method attaches them to the data tuple.</param> /// <param name="datasetVersion">The version of the dataset the data tuple is attached to. The version must be checked-out.</param> /// <returns>the created data tuple</returns> /// <exception cref="Exception">throws and exception if the dataset version is not checked-out.</exception> public DataTuple CreateDataTuple(int orderNo, ICollection<VariableValue> variableValues, ICollection<Amendment> amendments, DatasetVersion datasetVersion) { //Contract.Requires(!string.IsNullOrWhiteSpace(name)); Contract.Requires(datasetVersion != null); Contract.Ensures(Contract.Result<DataTuple>() != null && Contract.Result<DataTuple>().Id >= 0); if (datasetVersion.Status != DatasetVersionStatus.CheckedOut) { throw new Exception(string.Format("The dataset version {0} must be checked-out!", datasetVersion.Id)); } DataTuple e = new DataTuple() { OrderNo = orderNo, DatasetVersion = datasetVersion, VariableValues = new List<VariableValue>(variableValues), Amendments = new List<Amendment>(amendments), }; e.DatasetVersion.PriliminaryTuples.Add(e); e.Amendments.ToList().ForEach(ex => ex.Tuple = e); //e.VariableValues.ToList().ForEach(ex => ex.Tuple = e); // check to see if all variable values and their parameter values are defined in the data structure using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<DataTuple> repo = uow.GetRepository<DataTuple>(); repo.Put(e); uow.Commit(); } return (e); }
/// <summary> /// An amendment is like a variable value that is added to a data tuple. The difference is that the amendment does not need to be defined in the dataset's structure /// and also not all the data tuples need to have the same amendments. /// This method creates and amendment object, attaches it to the data tuple but does <b>NOT</b> persist it. /// </summary> /// <param name="value"><see cref="DataValue"/></param> /// <param name="note"><see cref="DataValue"/></param> /// <param name="samplingTime"><see cref="DataValue"/></param> /// <param name="resultTime"><see cref="DataValue"/></param> /// <param name="obtainingMethod"><see cref="DataValue"/></param> /// <param name="parameterId">The identifier of the parameter that the amendment will be linked to. needs more clarification</param> /// <param name="tuple">The data tuple receiving the amendment.ku</param> /// <returns></returns> public Amendment CreateAmendment(string value, string note, DateTime samplingTime, DateTime resultTime, ObtainingMethod obtainingMethod, Int64 parameterId, DataTuple tuple) { Contract.Requires(!string.IsNullOrWhiteSpace(value)); Contract.Requires(parameterId > 0); Contract.Requires(tuple != null); Contract.Ensures(Contract.Result<Amendment>() != null); Amendment e = new Amendment() { Value = value, Note = note, SamplingTime = samplingTime, ResultTime = resultTime, ObtainingMethod = obtainingMethod, ParameterId = parameterId, Tuple = tuple, }; //using (IUnitOfWork uow = this.GetUnitOfWork()) //{ // IRepository<Amendment> repo = uow.GetRepository<Amendment>(); // repo.Put(e); // uow.Commit(); //} return (e); }
/// <summary> /// Provided that the data tuple entity contains some changes, the method persists the changes into the database. /// </summary> /// <param name="entity">The data tuple containing the changes.</param> /// <returns>The same data tuple having the changes applied.</returns> public DataTuple UpdateDataTuple(DataTuple entity) { Contract.Requires(entity != null, "provided entity can not be null."); Contract.Requires(entity.Id >= 0, "provided entity must have a permanent ID."); Contract.Ensures(Contract.Result<DataTuple>() != null && Contract.Result<DataTuple>().Id >= 0, "No entity is persisted!"); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<DataTuple> repo = uow.GetRepository<DataTuple>(); repo.Put(entity); // Merge is required here!!!! uow.Commit(); } return (entity); }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="dt"></param> /// <param name="pksVs"></param> /// <returns></returns> private static bool sameDatatuple(DataTuple dt, Dictionary<long, string> pksVs) { bool IsSame = true; foreach (KeyValuePair<long, string> kvp in pksVs) { if (dt.VariableValues.Count > 0) { object value = dt.VariableValues.Where(p => p.VariableId.Equals(kvp.Key)).First().Value; if (value != null) { //value not equal different datatuples if (value.ToString() != kvp.Value) { IsSame = false; break; } } // if value is null means not equal to a not null value and // v1 = null != v2 = null else { IsSame = false; break; } } else { IsSame = false; break; } } return IsSame; }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="newDatatuple"></param> /// <param name="sourceDatatuple"></param> /// <returns></returns> private static DataTuple Merge(DataTuple newDatatuple, DataTuple sourceDatatuple) { sourceDatatuple.VariableValues = newDatatuple.VariableValues; return sourceDatatuple; }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="dataTuple"></param> /// <returns></returns> private static bool IsEmpty(DataTuple dataTuple) { foreach(VariableValue variableValue in dataTuple.VariableValues) { if (variableValue.Value!=null) return false; } return true; }
/// <summary> /// /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="dt"></param> /// <param name="pks"></param> /// <returns></returns> private static Dictionary<long, string> getPrimaryKeyValues(DataTuple dt, List<long> pks) { Dictionary<long, string> temp = new Dictionary<long,string>(); foreach (long k in pks) { object value = dt.VariableValues.Where(p => p.VariableId.Equals(k)).First().Value; if (value != null) temp.Add(k, value.ToString()); else temp.Add(k, ""); } return temp; }
/// <summary> /// convert primary keys to string /// returns null if a emtpy string is inside /// </summary> /// <remarks></remarks> /// <seealso cref=""/> /// <param name="datatuple"></param> /// <param name="primaryKeys"></param> /// <returns></returns> private static string getPrimaryKeysAsString(DataTuple datatuple, List<long> primaryKeys) { string value = ""; foreach (long t in primaryKeys) { // empty means not equals value // so if value is empty add timestamp millisec //datatuple.Materialize(); object v = datatuple.VariableValues.Where(p => p.VariableId.Equals(t)).First().Value; if (v != null) //if (!String.IsNullOrEmpty(v.ToString())) if (!String.IsNullOrEmpty((string)v)) value += ";"+v; else return ""; else return ""; } return value; }
private static bool Equal2(DataTuple newDatatuple, DataTuple sourceDatatuple) { foreach(VariableValue newVariableValue in newDatatuple.VariableValues ) { foreach(VariableValue sourceVariableValue in sourceDatatuple.VariableValues ) { if(newVariableValue.VariableId.Equals(sourceVariableValue.VariableId)) { if (!newVariableValue.Value.Equals(sourceVariableValue.Value)) return false; else break; } } } return true; }