示例#1
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        public override DataDictionary GetDictionary()
        {
            Proxy proxy = null;
              Session session = null;

              if (_dictionary != null)
            return _dictionary;

              if (System.IO.File.Exists(_dictionaryPath))
              {
            _dictionary = Utility.Read<DataDictionary>(_dictionaryPath);
            return _dictionary;
              }

              try
              {
            Connect(ref proxy, ref session);

            EqlClient eqlClient = new EqlClient(session);
            List<ClassObject> classObjects = GetClassObjects(eqlClient);

            _dictionary = new DataDictionary();
            foreach (ClassObject classObject in classObjects)
            {
              DataObject objDef = CreateObjectDefinition(proxy, classObject);

              if (objDef != null)
              {
            _dictionary.dataObjects.Add(objDef);
              }
            }

            Utility.Write<DataDictionary>(_dictionary, _dictionaryPath);
            return _dictionary;
              }
              catch (Exception e)
              {
            throw e;
              }
              finally
              {
            Disconnect(ref proxy, ref session);
              }
        }
示例#2
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        public override Response Post(IList<IDataObject> dataObjects)
        {
            Response response = new Response();
              Proxy proxy = null;
              Session session = null;

              try
              {
            if (dataObjects.Count <= 0)
            {
              response.Level = StatusLevel.Error;
              response.Messages.Add("No data objects to update.");
              return response;
            }

            string objType = ((GenericDataObject)dataObjects[0]).ObjectType;
            DataObject objDef = GetObjectDefinition(objType);
            Configuration config = GetConfiguration(objDef);

            Connect(ref proxy, ref session);

            foreach (IDataObject dataObject in dataObjects)
            {
              KeyProperty keyProp = objDef.keyProperties.FirstOrDefault();
              string keyValue = Convert.ToString(dataObject.GetPropertyValue(keyProp.keyPropertyName));

              string revision = string.Empty;
              Map revisionMap = config.Mappings.ToList<Map>().Find(x => x.Destination == (int)Destination.Revision);
              if (revisionMap != null)
              {
            string propertyName = Utilities.ToPropertyName(revisionMap.Column);
            revision = Convert.ToString(dataObject.GetPropertyValue(propertyName));
              }

              EqlClient eql = new EqlClient(session);
              int objectId = eql.GetObjectId(keyValue, revision, config.Template.ObjectType);
              org.iringtools.adapter.datalayer.eb.Template template = config.Template;

              if (objectId == 0)  // does not exist, create
              {
            string templateName = GetTemplateName(template, objDef, dataObject);
            int templateId = eql.GetTemplateId(templateName);

            if (templateId == 0)
            {
              Status status = new Status()
              {
                Identifier = keyValue,
                Level = StatusLevel.Error,
                Messages = new Messages() { string.Format("Template [{0}] does not exist.", templateName) }
              };

              response.StatusList.Add(status);
              response.Level = StatusLevel.Error;

              continue;
            }

            objectId = session.Writer.CreateFromTemplate(templateId, "", "");
              }

              string objectType = Enum.GetName(typeof(ObjectType), template.ObjectType);
              ebProcessor processor = new ebProcessor(session, config.Mappings.ToList<Map>(), _rules);

              if (objectType == ObjectType.Tag.ToString())
              {
            response.Append(processor.ProcessTag(objDef, dataObject, objectId, keyValue));
              }
              else if (objectType == ObjectType.Document.ToString())
              {
            response.Append(processor.ProcessDocument(objDef, dataObject, objectId, keyValue));

            //
            // post content like IW data layer
            //
            if (dataObject.GetType() == typeof(GenericContentObject))
            {
              response.Append(PostContents(new List<IContentObject>{(GenericContentObject) dataObject}, proxy, session));
            }
              }
              else
              {
            Status status = new Status()
            {
              Identifier = keyValue,
              Level = StatusLevel.Error,
              Messages = new Messages() { string.Format("Object type [{0}] not supported.", template.ObjectType) }
            };

            response.StatusList.Add(status);
            response.Level = StatusLevel.Error;
              }
            }
              }
              catch (Exception e)
              {
            _logger.Error("Error posting data objects: " + e);

            response.Level = StatusLevel.Error;
            response.Messages.Add("Error posting data objects: " + e);
              }
              finally
              {
            Disconnect(ref proxy, ref session);
              }

              return response;
        }
示例#3
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        public IList<IDataObject> Get(string objectType, IList<string> identifiers, bool retainSession)
        {
            IList<IDataObject> dataObjects = new List<IDataObject>();
              Proxy proxy = null;
              Session session = null;

              try
              {
            Connect(ref proxy, ref session);

            DataObject objDef = GetObjectDefinition(objectType);

            if (objDef != null)
            {
              string classObject = objDef.objectNamespace;
              string key = objDef.keyProperties.FirstOrDefault().keyPropertyName;
              string keyValues = "('" + string.Join("','", identifiers) + "')";

              if (classObject.ToLower() == "document" || classObject.ToLower() == "tag")
              {
            string eql = "START WITH {0} SELECT {1} WHERE {2} IN {3}";
            StringBuilder builder = new StringBuilder();

            foreach (DataProperty dataProp in objDef.dataProperties)
            {
              string item = Utilities.ToQueryItem(dataProp);

              if (!string.IsNullOrEmpty(item))
              {
                if (builder.Length > 0)
                  builder.Append(",");

                builder.Append(item);
              }
            }

            eql = string.Format(eql, classObject, builder.ToString(), key, keyValues);

            EqlClient eqlClient = new EqlClient(session);
            DataTable result = eqlClient.Search(session, eql, new object[0], 0, -1);

            dataObjects = ToDataObjects(result, objDef);

            //
            // return content when requesting single item like IW data layer
            //
            if (dataObjects.Count == 1 && identifiers.Count == 1)
            {
              IList<int> docIds = GetDocumentIds(dataObjects);
              IList<IContentObject> contentObjects = GetContents(objectType, docIds, proxy, session);

              //
              // make data object a content object
              //
              if (contentObjects.Count > 0)
              {
                IDataObject dataObject = dataObjects.FirstOrDefault();
                IContentObject contentObject = new GenericContentObject { ObjectType = objectType };

                contentObject.content = contentObjects[0].content;
                contentObject.contentType = contentObjects[0].contentType;
                contentObject.identifier = contentObjects[0].identifier;

                foreach (DataProperty prop in objDef.dataProperties)
                {
                  object value = dataObject.GetPropertyValue(prop.propertyName);
                  contentObject.SetPropertyValue(prop.propertyName, value);
                }

                return new List<IDataObject> { contentObject };
              }
            }
              }
              else
              {
            throw new Exception("Class object [" + classObject + "] not supported.");
              }
            }
            else
            {
              throw new Exception("Object type " + objectType + " not found.");
            }
              }
              finally
              {
            if (!retainSession) Disconnect(ref proxy, ref session);
              }

              return dataObjects;
        }
示例#4
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        public override long GetCount(string objectType, DataFilter filter)
        {
            Proxy proxy = null;
              Session session = null;

              try
              {
            DataObject objDef = GetObjectDefinition(objectType);

            if (objDef != null)
            {
              Connect(ref proxy, ref session);

              Configuration config = GetConfiguration(objDef);
              int objType = (int)config.Template.ObjectType;
              string classIds = objDef.tableName.Replace("_", ",");
              string eql = string.Empty;

              if (objType == (int)ObjectType.Tag)
              {
            eql = string.Format("START WITH Tag WHERE Class.Id IN ({0})", classIds);
              }
              else if (objType == (int)ObjectType.Document)
              {
            eql = string.Format("START WITH Document WHERE Class.Id IN ({0})", classIds);
              }
              else
              {
            throw new Exception(string.Format("Object type [{0}] not supported.", objectType));
              }

              if (filter != null)
              {
            string whereClause = Utilities.ToSqlWhereClause(filter, objDef);
            if (!string.IsNullOrEmpty(whereClause))
            {
              eql += whereClause.Replace(" WHERE ", " AND ");
            }
              }

              EqlClient eqlClient = new EqlClient(session);
              DataTable dt = eqlClient.RunQuery(eql);
              return Convert.ToInt64(dt.Rows.Count);
            }
            else
            {
              throw new Exception(string.Format("Object type [{0}] not found.", objectType));
            }
              }
              catch (Exception e)
              {
            _logger.Error(string.Format("Error getting object count for [{0}]: {1}", objectType, e.Message));
            throw e;
              }
              finally
              {
            Disconnect(ref proxy, ref session);
              }
        }
示例#5
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        public override Response Delete(string objectType, IList<string> identifiers)
        {
            Response response = new Response() { Level = StatusLevel.Success };
              Proxy proxy = null;
              Session session = null;

              try
              {
            DataObject objDef = GetObjectDefinition(objectType);

            if (objDef != null)
            {
              try
              {
            Connect(ref proxy, ref session);

            EqlClient eqlClient = new EqlClient(session);
            Configuration config = GetConfiguration(objDef);
            int objType = (int)config.Template.ObjectType;

            foreach (string identifier in identifiers)
            {
              Status status = new Status()
              {
                Identifier = identifier,
                Level = StatusLevel.Success
              };

              int objId = eqlClient.GetObjectId(identifier, string.Empty, objType);

              if (objId != 0)
              {
                if (objType == (int)ObjectType.Tag)
                {
                  Tag tag = new Tag(session, objId);
                  tag.Delete();
                  status.Messages.Add(string.Format("Tag [{0}] deleted succesfully.", identifier));
                }
                else if (objType == (int)ObjectType.Document)
                {
                  Document doc = new Document(session, objId);
                  doc.Delete();
                  status.Messages.Add(string.Format("Document [{0}] deleted succesfully.", identifier));
                }
                else
                {
                  status.Level = StatusLevel.Error;
                  status.Messages.Add(string.Format("Object type [{0}] not supported.", objType));
                  response.Level = StatusLevel.Error;
                }
              }
              else
              {
                status.Level = StatusLevel.Error;
                status.Messages.Add(string.Format("Object [{0}] not found.", identifier));
                response.Level = StatusLevel.Error;
              }

              response.Append(status);
            }
              }
              finally
              {
            Disconnect(ref proxy, ref session);
              }
            }
            else
            {
              response.Level = StatusLevel.Error;
              response.Messages.Add(string.Format("Object type [{0}] does not exist.", objectType));
            }
              }
              catch (Exception e)
              {
            _logger.Error("Error deleting data object: " + e);

            response.Level = StatusLevel.Error;
            response.Messages.Add(e.Message);
              }

              return response;
        }
示例#6
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        public override IList<IDataObject> Get(string objectType, DataFilter filter, int pageSize, int startIndex)
        {
            IList<IDataObject> dataObjects = new List<IDataObject>();
              Proxy proxy = null;
              Session session = null;

              try
              {
            Connect(ref proxy, ref session);

            DataObject objDef = GetObjectDefinition(objectType);

            if (objDef != null)
            {
              string classObject = objDef.objectNamespace;
              string classIds = objDef.tableName.Replace("_", ",");

              if (classObject.ToLower() == "document" || classObject.ToLower() == "tag")
              {
            string eql = "START WITH {0} SELECT {1} WHERE Class.Id IN ({2})";
            StringBuilder builder = new StringBuilder();

            foreach (DataProperty dataProp in objDef.dataProperties)
            {
              string item = Utilities.ToQueryItem(dataProp);

              if (!string.IsNullOrEmpty(item))
              {
                if (builder.Length > 0)
                  builder.Append(",");

                builder.Append(item);
              }
            }

            eql = string.Format(eql, classObject, builder.ToString(), classIds);

            if (filter != null)
            {
              string whereClause = Utilities.ToSqlWhereClause(filter, objDef);
              if (!string.IsNullOrEmpty(whereClause))
              {
                eql += whereClause.Replace(" WHERE ", " AND ");
              }
            }

            EqlClient eqlClient = new EqlClient(session);
            DataTable result = eqlClient.Search(session, eql, new object[0], startIndex, pageSize);

            dataObjects = ToDataObjects(result, objDef);
              }
              else
              {
            throw new Exception("Class object [" + classObject + "] not supported.");
              }
            }
            else
            {
              throw new Exception("Object type " + objectType + " not found.");
            }
              }
              finally
              {
            Disconnect(ref proxy, ref session);
              }

              return dataObjects;
        }
示例#7
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        protected Response PostContents(IList<IContentObject> contentObjects, Proxy proxy, Session session)
        {
            Response response = new Response() { Level = StatusLevel.Success };

              foreach (GenericContentObject contentObject in contentObjects)
              {
            try
            {
              //
              // get doc Id
              //
              IList<IDataObject> dataObjects = Get(contentObject.ObjectType, new List<string> { contentObject.identifier }, true);
              int docId = GetDocumentId(dataObjects.FirstOrDefault());

              if (docId <= 0)
              {
            session.ProtoProxy.AddObjectFile(session.ReaderSessionString, docId,
              (int)ObjectType.Document, contentObject.content, contentObject.identifier, 0);

            Status status = new Status()
            {
              Identifier = contentObject.identifier,
              Level = StatusLevel.Error,
              Messages = new Messages() { string.Format("Document [{0}] not found.", contentObject.identifier) }
            };

            response.StatusList.Add(status);
            response.Level = StatusLevel.Error;

            continue;
              }

              //
              // get content id
              //
              EqlClient client = new EqlClient(session);
              DataTable dt = client.RunQuery(string.Format(CONTENT_EQL, docId));
              int fileId = (int)(dt.Rows[0]["FilesId"]);

              if (fileId <= 0)  // add
              {
            session.ProtoProxy.AddObjectFile(session.ReaderSessionString, docId,
              (int)ObjectType.Document, contentObject.content, contentObject.identifier, 0);

            Status status = new Status()
            {
              Identifier = contentObject.identifier,
              Level = StatusLevel.Success,
              Messages = new Messages() { string.Format("Document [{0}] added successfully.", contentObject.identifier) }
            };

            response.StatusList.Add(status);
              }
              else  // update
              {
            //
            // check out content
            //
            eB.Data.File file = new eB.Data.File(session);
            file.Retrieve(fileId, "Header;Repositories");

            FileInfo localFile = new FileInfo(Path.GetTempPath() + file.Name);
            if (localFile.Exists)
              localFile.Delete();

            file.CheckOut(localFile.FullName);

            //
            // update content
            //
            Utility.WriteStream(contentObject.content, localFile.FullName);

            //
            // check content back in
            //
            file = new eB.Data.File(session);
            file.Retrieve(fileId, "Header;Repositories");

            if (file.IsCheckedOut)
            {
              try
              {
                file.CheckIn(localFile.FullName, eB.ContentData.File.CheckinOptions.DeleteLocalCopy, null);
                session.Writer.CheckinDoc(file.Document.Id, 0);

                Status status = new Status()
                {
                  Identifier = contentObject.identifier,
                  Level = StatusLevel.Success,
                  Messages = new Messages() { string.Format("Document [{0}] updated successfully.", contentObject.identifier) }
                };

                response.StatusList.Add(status);
              }
              catch
              {
                file.UndoCheckout();
              }
            }
              }
            }
            catch (Exception e)
            {
              _logger.Error("Error posting content: " + e.Message);

              Status status = new Status()
              {
            Identifier = contentObject.identifier,
            Level = StatusLevel.Error,
            Messages = new Messages() { e.Message }
              };

              response.StatusList.Add(status);
              response.Level = StatusLevel.Error;
            }
              }

              return response;
        }
示例#8
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        protected IList<IContentObject> GetContents(String objectType, IList<int> docIds, Proxy proxy, Session session)
        {
            IList<IContentObject> contents = new List<IContentObject>();

              try
              {
            string query = string.Format(CONTENT_EQL, string.Join(",", docIds.ToArray()));
            EqlClient client = new EqlClient(session);
            DataTable dt = client.RunQuery(query);

            foreach (DataRow row in dt.Rows)
            {
              int fileId = (int)row["FilesId"];
              string code = (string)row["Code"];

              string name = ((string)row["FilesName"]);
              string type = name.Substring(name.LastIndexOf(".") + 1);

              eB.Data.File f = new eB.Data.File(session);
              f.Retrieve(fileId, "Header; Repositories");
              MemoryStream stream = new MemoryStream();
              f.ContentData = new eB.ContentData.File(f, stream);
              f.ContentData.ReadAllBytes();
              stream.Position = 0;

              GenericContentObject content = new GenericContentObject()
              {
            ObjectType = objectType,
            identifier = code,
            content = stream,
            contentType = _contentTypes.Find(x => x.Extension == type.ToLower()).MimeType,
            //name = name
              };

              contents.Add(content);
            }
              }
              catch (Exception e)
              {
            _logger.Error("Error getting contents: " + e.Message);
            throw e;
              }

              return contents;
        }
示例#9
0
文件: ebDataLayer.cs 项目: Vidisha/eb
        protected List<ClassObject> GetClassObjects(EqlClient eqlClient)
        {
            List<ClassObject> classObjects = new List<ClassObject>();

              if (string.IsNullOrEmpty(_classObjects))
              {
            DataTable dt = eqlClient.RunQuery(CLASS_OBJECTS_EQL);

            foreach (DataRow row in dt.Rows)
            {
              int groupId = (int)row["GroupId"];
              string groupName = Enum.GetName(typeof(GroupType), groupId);
              string path = row["Path"].ToString();

              ClassObject classObject = new ClassObject()
              {
            Name = path,
            ObjectType = (ObjectType)(row["ObjectType"]),
            GroupId = groupId,
            Ids = eqlClient.GetClassIds(groupId, path)
              };

              classObjects.Add(classObject);
            }
              }
              else
              {
            string[] cosParts = _classObjects.Split(',');

            for (int i = 0; i < cosParts.Length; i++)
            {
              string[] coParts = cosParts[i].Trim().Split('.');
              string groupName = coParts[0];
              string className = coParts[1];

              ClassObject classObject = new ClassObject()
              {
            Name = className,
            ObjectType = (ObjectType)Enum.Parse(typeof(ObjectType), groupName),
            GroupId = (int)Enum.Parse(typeof(GroupType), groupName),
            Ids = eqlClient.GetClassIds((int)Enum.Parse(typeof(GroupType), groupName), className)
              };

              classObjects.Add(classObject);
            }
              }

              return classObjects;
        }
示例#10
0
        protected Status SetRelationships(Document doc)
        {
            Status status = new Status()
            {
                Level = StatusLevel.Success
            };
            EqlClient eqlClient = new EqlClient(_session);

            try
            {
                foreach (Map m in _mappings.Where(m => m.RuleRefs != null && m.RuleRefs.Length > 0).Select(m => m))
                {
                    List <Rule> rules = GetRules(m.RuleRefs.ToList <RuleRef>());

                    foreach (Rule rule in rules)
                    {
                        if (RequiredParametersPresent(rule))
                        {
                            if (SelfchecksPassed(rule))
                            {
                                int reltemplateId = eqlClient.GetTemplateId(rule.RelationshipTemplate);
                                if (reltemplateId > 0)
                                {
                                    DataTable relatedObjects = eqlClient.GetObjectIds(ParseEQL(rule));
                                    foreach (DataRow dr in relatedObjects.Rows)
                                    {
                                        int relatedObjectId = int.Parse(dr[0].ToString());
                                        if (relatedObjectId > 0)
                                        {
                                            try
                                            {
                                                bool add           = false;
                                                int  existingrelId = eqlClient.GetExistingRelationship(reltemplateId, doc.Id, relatedObjectId, ref add);
                                                if (existingrelId > 0)
                                                {
                                                    _session.Writer.DelRelationship(existingrelId);
                                                }
                                                if (add)
                                                {
                                                    _session.Writer.CreateRelFromTemplate(reltemplateId, doc.Id, (int)ObjectType.Document, relatedObjectId, (int)rule.RelatedObjectType);
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                status.Messages.Add(string.Format("Relationship update failed due to error {0}.", e.Message));
                                                status.Level = StatusLevel.Warning;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    status.Messages.Add(string.Format("Relationship template {0} is not defined.", rule.RelationshipTemplate));
                                    status.Level = StatusLevel.Warning;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                status.Messages.Add(e.Message);
                status.Level = StatusLevel.Error;
            }

            return(status);
        }
示例#11
0
文件: ebProcessor.cs 项目: Vidisha/eb
        protected Status SetRelationships(Document doc)
        {
            Status status = new Status() { Level = StatusLevel.Success };
              EqlClient eqlClient = new EqlClient(_session);

              try
              {
            foreach (Map m in _mappings.Where(m => m.RuleRefs != null && m.RuleRefs.Length > 0).Select(m => m))
            {
              List<Rule> rules = GetRules(m.RuleRefs.ToList<RuleRef>());

              foreach (Rule rule in rules)
              {
            if (RequiredParametersPresent(rule))
            {
              if (SelfchecksPassed(rule))
              {
                int reltemplateId = eqlClient.GetTemplateId(rule.RelationshipTemplate);
                if (reltemplateId > 0)
                {
                  DataTable relatedObjects = eqlClient.GetObjectIds(ParseEQL(rule));
                  foreach (DataRow dr in relatedObjects.Rows)
                  {
                    int relatedObjectId = int.Parse(dr[0].ToString());
                    if (relatedObjectId > 0)
                    {
                      try
                      {
                        bool add = false;
                        int existingrelId = eqlClient.GetExistingRelationship(reltemplateId, doc.Id, relatedObjectId, ref add);
                        if (existingrelId > 0)
                        {
                          _session.Writer.DelRelationship(existingrelId);
                        }
                        if (add)
                        {
                          _session.Writer.CreateRelFromTemplate(reltemplateId, doc.Id, (int)ObjectType.Document, relatedObjectId, (int)rule.RelatedObjectType);
                        }
                      }
                      catch (Exception e)
                      {
                        status.Messages.Add(string.Format("Relationship update failed due to error {0}.", e.Message));
                        status.Level = StatusLevel.Warning;
                      }
                    }
                  }
                }
                else
                {
                  status.Messages.Add(string.Format("Relationship template {0} is not defined.", rule.RelationshipTemplate));
                  status.Level = StatusLevel.Warning;
                }
              }
            }
              }
            }
              }
              catch (Exception e)
              {
            status.Messages.Add(e.Message);
            status.Level = StatusLevel.Error;
              }

              return status;
        }