public void CsvWriterServiceTest_Write() { var csvModel = new CsvModel(); var service = GetService(); using (var sw = new StringWriter()) { service.Write(new [] { csvModel }, sw); Debug.Write(sw.ToString()); } }
public int Import(CsvModel csvModel) { var vault = this.VaultService.Vault.Value; if (this.ObjectTypeLookup == null) this.ObjectTypeLookup = GetObjTypeLookup(vault); var objectTypeLookup = this.ObjectTypeLookup; if (this.ObjectClassLookup == null) this.ObjectClassLookup = GetObjectClassLookup(vault); var objectClassLookup = this.ObjectClassLookup; if (this.PropertyDefLookupById == null) this.PropertyDefLookupById = GetPropertyDefLookupById(vault); var propertyDefLookupById = this.PropertyDefLookupById; if (this.PropertyDefLookupByName == null) this.PropertyDefLookupByName = GetPropertyDefLookupByName(vault); var propertyDefLookupByName = this.PropertyDefLookupByName; int objectClassId = objectClassLookup[csvModel.Class].ID; int objectTypeId = objectTypeLookup[csvModel.ObjectType].ID; Debug.WriteLine("objectTypeId: {0}, objectClassId: {1}", objectTypeId, objectClassId); ObjType objType = objectTypeLookup[csvModel.ObjectType]; /* create the property Values */ var propertyValues = GetPropertyValues(csvModel, vault, objectClassId, objType, objectTypeLookup, propertyDefLookupByName, propertyDefLookupById); /* attach the files */ var sourceObjectFiles = GetSourceObjectFiles(csvModel); /* create the object */ var objectVersionAndProperties = GetObjectVersionAndProperties(csvModel, vault, propertyValues, sourceObjectFiles, objectTypeId); //var objectVersionAndProperties = GetObjectVersionAndPropertiesAndCheckin(csvModel, vault, propertyValues, sourceObjectFiles, objectTypeId); /* check in the newly created object */ vault.ObjectOperations.CheckIn(objectVersionAndProperties.ObjVer); /* return the id */ return objectVersionAndProperties.ObjVer.ObjID.ID; }
private SourceObjectFiles GetSourceObjectFiles(CsvModel csvModel) { SourceObjectFiles sourceObjectFiles = null; var sourceFiles = csvModel.SourceFiles.Select(f => SourceFileModel.FromFile(f)).ToArray(); Debug.WriteLine("sourceFiles.Length: " + sourceFiles.Length); if (sourceFiles.Length > 0) { sourceObjectFiles = new MFilesAPI.SourceObjectFiles(); /* add a source file */ for (int i = 0; i < sourceFiles.Length; i++) { var sourceObjectFile = new MFilesAPI.SourceObjectFile(); sourceObjectFile.SourceFilePath = sourceFiles[i].FilePath; sourceObjectFile.Title = sourceFiles[i].Title; sourceObjectFile.Extension = sourceFiles[i].Extension; sourceObjectFiles.Add(i, sourceObjectFile); } } return sourceObjectFiles; }
//private int GetObjectTypeId(CsvModel csvModel, Vault vault) //{ // ObjTypes objectTypes = vault.ObjectTypeOperations.GetObjectTypes(); // int objectTypeId = -1; // foreach (ObjType objectType in objectTypes) // { // if (objectType.NameSingular == csvModel.ObjectType) // { // objectTypeId = objectType.ID; // break; // } // Debug.WriteLine(objectType.NameSingular); // } // return objectTypeId; //} //private int GetClassId(CsvModel csvModel, Vault vault) //{ // var allObjectClasses = vault.ClassOperations.GetAllObjectClasses(); // int classId = -1; // foreach (ObjectClass objectClass in allObjectClasses) // { // if (csvModel.Class == objectClass.Name) // { // classId = objectClass.ID; // break; // } // //Debug.WriteLine(string.Format("Id={0}, Name={1}", objectClass.ID, objectClass.Name)); // } // return classId; //} //private ObjectVersionAndProperties GetObjectVersionAndPropertiesAndCheckin(CsvModel csvModel, Vault vault, PropertyValues propertyValues, SourceObjectFiles sourceObjectFiles, int objectTypeId) //{ // if (objectTypeId == -1) // throw new ArgumentException(string.Format("could not find objectType with name '{0}'", csvModel.ObjectType)); // Debug.WriteLine("objectTypeId: " + objectTypeId); // return vault.ObjectOperations.CreateNewObjectEx(objectTypeId, propertyValues, SourceFiles, false, false) // return vault.ObjectOperations.CreateNewObject(objectTypeId, propertyValues, sourceObjectFiles); //} private ObjectVersionAndProperties GetObjectVersionAndProperties(CsvModel csvModel, Vault vault, PropertyValues propertyValues, SourceObjectFiles sourceObjectFiles, int objectTypeId) { if (objectTypeId == -1) throw new ArgumentException(string.Format("could not find objectType with name '{0}'", csvModel.ObjectType)); Debug.WriteLine("objectTypeId: " + objectTypeId); //return vault.ObjectOperations.CreateNewObjectEx(objectTypeId, propertyValues, sourceObjectFiles, false, false); return vault.ObjectOperations.CreateNewObject(objectTypeId, propertyValues, sourceObjectFiles); }
private PropertyValues GetPropertyValues(CsvModel csvModel, Vault vault, int classId, ObjType objType, Dictionary<string, ObjType> objectTypeLookup, Dictionary<string, PropertyDef> propertyDefLookupByName, Dictionary<int, PropertyDef> propertyDefLookupById) { var propertyValues = new MFilesAPI.PropertyValues(); int index = 0; /* add the special class property */ propertyValues.Add(index++, GetPropertyValue((int)MFilesAPI.MFBuiltInPropertyDef.MFBuiltInPropertyDefClass, MFDataType.MFDatatypeLookup, classId)); ObjectClass objectClass = vault.ClassOperations.GetObjectClass(classId); /* if the object type has an owner */ if (objType.HasOwnerType) { /* add a special property for the owner */ var ownerObjType = vault.ObjectTypeOperations.GetObjectType(objType.OwnerType); var propertyDef = propertyDefLookupByName[ownerObjType.NameSingular]; string value; if (csvModel.Values.TryGetValue(propertyDef.Name, out value)) { object objValue = ConvertPropertyValue(propertyDef, value, vault); var propertyValue = GetPropertyValue(propertyDef.ID, propertyDef.DataType, objValue); propertyValues.Add(index++, propertyValue); } } foreach (AssociatedPropertyDef associatedPropertyDef in objectClass.AssociatedPropertyDefs) { /* get the property definition id */ PropertyDef propertyDef = PropertyDefLookupById[associatedPropertyDef.PropertyDef]; string value; if (csvModel.Values.TryGetValue(propertyDef.Name, out value)) { /* try to convert the string to the ObjectData Type */ object objValue = ConvertPropertyValue(propertyDef, value, vault); /* create the property value */ PropertyValue propertyValue = GetPropertyValue(associatedPropertyDef.PropertyDef, propertyDef.DataType, objValue); Debug.WriteLine("property - Id: {0}, Name: {1}, DataType: {2}, Value: {3}", associatedPropertyDef.PropertyDef, propertyDef.Name, propertyDef.DataType, objValue); /* add the property value */ propertyValues.Add(index++, propertyValue); } } return propertyValues; }
public IEnumerable<CsvModel> GetCsvModels(TextReader tr, string delimiter) { List<CsvModel> models = new List<CsvModel>(); using (var csvParser = new CsvParser(tr, new CsvHelper.Configuration.CsvConfiguration() { Delimiter = delimiter } )) { var headerRow = csvParser.Read(); int classIndex = headerRow.GetIndexOf(CsvHeaders.Class); int objectTypeIndex = headerRow.GetIndexOf(CsvHeaders.ObjectType); int sourceIndex = headerRow.GetIndexOf(CsvHeaders.Source); if (classIndex == -1) throw new ArgumentException("missing column 'Class'!"); if (objectTypeIndex == -1) throw new ArgumentException("missing column 'Object Type'!"); string[] row = null; while ((row = csvParser.Read()) != null) { var csvModel = new CsvModel(); csvModel.ObjectType = row[objectTypeIndex]; csvModel.Class = row[classIndex]; for (int i = 0; i < row.Length; i++) { if (i == sourceIndex) { var sourceFiles = row[i].Split(new char[] { Constants.SourceFileDelimiter }, StringSplitOptions.RemoveEmptyEntries); foreach (var sourceFile in sourceFiles) csvModel.SourceFiles.Add(sourceFile); } else if (i != classIndex && i != objectTypeIndex && headerRow[i] != CsvHeaders.Source) csvModel.Values.Add(headerRow[i], row[i]); } models.Add(csvModel); } } return models; }