/// <summary> /// Creates the entity object. /// </summary> /// <param name="metaClassName">Name of the meta class.</param> /// <param name="primaryKeyId">The primary key id.</param> /// <returns></returns> protected override EntityObject CreateEntityObject(string metaClassName, PrimaryKeyId?primaryKeyId) { if (metaClassName == DocumentContentVersionEntity.GetAssignedMetaClassName()) { DocumentContentVersionEntity retVal = new DocumentContentVersionEntity(); retVal.PrimaryKeyId = primaryKeyId; return(retVal); } return(base.CreateEntityObject(metaClassName, primaryKeyId)); }
protected override void PreDeleteInsideTransaction(BusinessContext context) { base.PreDeleteInsideTransaction(context); #region Delete DocumentVersion EntityObject[] versions = BusinessManager.List(DocumentContentVersionEntity.GetAssignedMetaClassName(), new FilterElement[] { FilterElement.EqualElement("OwnerDocumentId", context.GetTargetPrimaryKeyId().Value) }); foreach (EntityObject version in versions) { BusinessManager.Delete(version); } #endregion #region Process RelatedDocumentAction RelatedDocumentAction relDocumentAction = context.Request.Parameters.GetValue <RelatedDocumentAction>(DocumentRequestParameters.Delete_RelatedDocumentAction, RelatedDocumentAction.None); switch (relDocumentAction) { // Detach all assigned contacts case RelatedDocumentAction.Detach: EntityObject[] detachedItems = BusinessManager.List(DocumentEntity.GetAssignedMetaClassName(), new FilterElement[] { FilterElement.EqualElement("MasterDocumentId", context.GetTargetPrimaryKeyId()) }); foreach (DocumentEntity document in detachedItems) { document.MasterDocumentId = null; BusinessManager.Update(document); } break; // Delete all assigned actions case RelatedDocumentAction.Delete: EntityObject[] deletedItems = BusinessManager.List(DocumentEntity.GetAssignedMetaClassName(), new FilterElement[] { FilterElement.EqualElement("MasterDocumentId", context.GetTargetPrimaryKeyId()) }); foreach (DocumentEntity document in deletedItems) { DeleteRequest request = new DeleteRequest(document); request.Parameters.Add(DocumentRequestParameters.Delete_RelatedDocumentAction, RelatedDocumentAction.Delete); BusinessManager.Execute(request); } break; } #endregion // TODO: Process Link To Current Document }
protected override void PostCreateInsideTransaction(BusinessContext context) { base.PostCreateInsideTransaction(context); PrimaryKeyId documentPrimaryKeyId = ((CreateResponse)context.Response).PrimaryKeyId; // Check TemplateId PrimaryKeyId templatePrimaryKeyId = context.Request.Parameters.GetValue <PrimaryKeyId>(DocumentRequestParameters.Create_DocumentTemplatedId, PrimaryKeyId.Empty); if (templatePrimaryKeyId != PrimaryKeyId.Empty) { // Load Template DocumentTemplateEntity template = (DocumentTemplateEntity)BusinessManager.Load(DocumentTemplateEntity.GetAssignedMetaClassName(), templatePrimaryKeyId); if (template.File != null) { // Initialize Content //DocumentContentEntity content = (DocumentContentEntity)BusinessManager.InitializeEntity(DocumentContentEntity.GetAssignedMetaClassName()); //content.Name = template.File.Name; //content.OwnerDocumentId = documentPrimaryKeyId; //PrimaryKeyId contentPrimaryKeyId = BusinessManager.Create(content); // Initialize Version DocumentContentVersionEntity version = (DocumentContentVersionEntity)BusinessManager.InitializeEntity(DocumentContentVersionEntity.GetAssignedMetaClassName()); version.Name = template.File.Name; version.OwnerDocumentId = documentPrimaryKeyId; // version.DocumentContentId is not updateble //version.DocumentContentId = contentPrimaryKeyId; // Upload File using (System.IO.Stream inputStream = template.File.OpenRead()) { // Modify Template string extension = System.IO.Path.GetExtension(template.File.Name); IDocumentTemplateProcessor processor = FindTemplateProcessor(extension); System.IO.Stream outputStream = null; if (processor != null) { Dictionary <string, EntityObject> keys = (Dictionary <string, EntityObject>)context.Items["MC_DocumentTemplateProcessorKeys"]; context.Request.Target.PrimaryKeyId = ((CreateResponse)context.Response).PrimaryKeyId; keys.Add("Document", context.Request.Target); keys.Add("DocumentContentVersion", version); keys.Add("DocumentTemplateEntity", template); outputStream = processor.Convert(extension, inputStream, keys); } else { outputStream = inputStream; } FileInfo fileInfo = new FileInfo(template.File.Name, outputStream); version.File = fileInfo; PrimaryKeyId versionPrimaryKeyId = BusinessManager.Create(version); } } } else { // Initialize Content //DocumentContentEntity content = (DocumentContentEntity)BusinessManager.InitializeEntity(DocumentContentEntity.GetAssignedMetaClassName()); //content.Name = string.Empty; //content.OwnerDocumentId = documentPrimaryKeyId; //PrimaryKeyId contentPrimaryKeyId = BusinessManager.Create(content); } }
protected override void PreCreateInsideTransaction(BusinessContext context) { // Call base method base.PreCreateInsideTransaction(context); DocumentContentVersionEntity newVersion = (DocumentContentVersionEntity)context.Request.Target; #region Check Source Version Id PrimaryKeyId?sourceVersionId = (PrimaryKeyId?)context.Request.Parameters.GetValue <PrimaryKeyId?>(DocumentContentVersionRequestParameters.Create_SourceVersionId); if (sourceVersionId.HasValue) { // Load Version DocumentContentVersionEntity srcVersion = (DocumentContentVersionEntity)BusinessManager.Load(DocumentContentVersionEntity.GetAssignedMetaClassName(), sourceVersionId.Value); newVersion.OwnerDocumentId = srcVersion.OwnerDocumentId; //newVersion.DocumentContentId = srcVersion.DocumentContentId; // Copy File if (srcVersion.File != null) { using (System.IO.Stream srcStream = srcVersion.File.OpenRead()) { FileInfo fileInfo = new FileInfo(srcVersion.File.Name, srcStream); newVersion.File = fileInfo; } } } #endregion }
/// <summary> /// Customs the method. /// </summary> /// <param name="context">The context.</param> protected virtual void UpdateState(BusinessContext context) { // Load Meta Object MetaObject metaObject = MetaObjectActivator.CreateInstance(DocumentContentVersionEntity.GetAssignedMetaClassName(), context.GetTargetPrimaryKeyId().Value); // Extract old and new states DocumentContentVersionState newState = (DocumentContentVersionState)(int)context.Request.Target["State"]; DocumentContentVersionState oldState = (DocumentContentVersionState)(int)metaObject["State"]; if (oldState == newState) { return; } BusinessContext.Current.Items["MC_Document_SourceMetaObject"] = metaObject; BusinessContext.Current.Items["MC_Document_NewState"] = newState; BusinessContext.Current.Items["MC_Document_OldState"] = oldState; // Pre-Process switch (oldState) { case DocumentContentVersionState.Active: { // Reset Content Reference to Active Document here DocumentEntity document = (DocumentEntity)BusinessManager.Load(DocumentEntity.GetAssignedMetaClassName(), (PrimaryKeyId)metaObject["OwnerDocumentId"]); document.ActiveVersionId = null; BusinessManager.Update(document); } break; case DocumentContentVersionState.Draft: // Nothing break; case DocumentContentVersionState.Obsolete: // Nothing break; } // Process switch (newState) { case DocumentContentVersionState.Active: { DocumentEntity document = (DocumentEntity)BusinessManager.Load(DocumentEntity.GetAssignedMetaClassName(), (PrimaryKeyId)metaObject["OwnerDocumentId"]); // Reset Current Reference to Active Document here if (document.ActiveVersionId.HasValue) { DocumentContentVersionEntity oldVersion = new DocumentContentVersionEntity(document.ActiveVersionId.Value); oldVersion.State = (int)DocumentContentVersionState.Draft; UpdateStateRequest request = new UpdateStateRequest(oldVersion); BusinessManager.Execute(request); } // Update Current Reference to Active Document here document.ActiveVersionId = context.GetTargetPrimaryKeyId(); BusinessManager.Update(document); } break; case DocumentContentVersionState.Draft: break; case DocumentContentVersionState.Obsolete: break; } // Update Data Storage And Save metaObject["State"] = (int)newState; metaObject.Save(); context.SetResponse(new Response()); }