⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 document.cs

📁 DocFlow展示了使用.NET开发平台结合Microsoft SQL Server数据库和Microsoft Indexing Service索引服务同样也能创建功能强大的文档管理门户。 Do
💻 CS
📖 第 1 页 / 共 2 页
字号:
					cmd.Parameters.Clear();
					cmd.Parameters.Add(new OleDbParameter("id", ref_id));
					cmd.ExecuteNonQuery();
				}
			}

			cmd.Dispose();
		}

		public IEnumerable LoadDocRefs(string tableName, string refFieldName)
		{
			ArrayList res = new ArrayList();
			System.Data.OleDb.OleDbConnection con = Db.Connection;
			OleDbCommand cmd = con.CreateCommand();
			cmd.CommandText = "select  " + refFieldName + " from " + tableName + " where DocId=" + m_Id.ToString();
			OleDbDataReader reader = cmd.ExecuteReader();
			while(reader.Read())
				res.Add(Convert.ToInt32( reader[0]));
			reader.Close();
			cmd.Dispose();
			return (IEnumerable)res;
		}


		public void Validate()
		{

			System.Data.OleDb.OleDbConnection con = Db.Connection;
			OleDbCommand cmd = con.CreateCommand();

			if(IncomingNumber != null && IncomingNumber.Length != 0)
			{
				cmd.CommandText =   @"select count(1) from documents where IncomingNumber=?  and id <> ?";
				cmd.Parameters.Clear();
				cmd.Parameters.Add(new OleDbParameter("IncomingNumber", IncomingNumber));
				cmd.Parameters.Add(new OleDbParameter("id", m_Id));
				bool exists = (bool)((int) cmd.ExecuteScalar() != 0);
				if(exists)
				{
					cmd.Dispose();
					throw new BipGenericException(BipResources.GetString("StrDocIncomingNumberIsNotUnique"));
				}
			}

			if(OutgoingNumber != null && OutgoingNumber.Length != 0)
			{
				cmd.CommandText =   @"select count(1) from documents where OutgoingNumber=?  and id <> ?";
				cmd.Parameters.Clear();
				cmd.Parameters.Add(new OleDbParameter("OutgoingNumber", OutgoingNumber));
				cmd.Parameters.Add(new OleDbParameter("id", m_Id));
				bool exists = (bool)((int) cmd.ExecuteScalar() != 0);
				if(exists)
				{
					cmd.Dispose();
					throw new BipGenericException(BipResources.GetString("StrDocOutgoingNumberIsNotUnique"));
				}
			}
			cmd.Dispose();
		}


		public override void New()
		{
			m_OwnerUserId = UserIdentity.Current.UserId;
			m_DateReceived = DateTime.Now;
			m_FileTypeId = 1;
		}

		public override int  Create()
		{ 
			if(! CanEdit)
				throw new BipAccessDeniedException();

			OleDbTransaction trans;
			DbCreate(out trans);
			if(trans == null)
				throw new BipFatalException();
			try
			{
				FileCreate();
				trans.Commit();
			}
			catch(Exception ex)
			{
				trans.Rollback();
				throw ex;
			}

			return m_Id;
		}
		public override void Load(int id)
		{

			System.Data.OleDb.OleDbConnection con = Db.Connection;
			OleDbCommand cmd = con.CreateCommand();
			cmd.CommandText = "select IsPublic, OwnerUserId from documents where id = " + id.ToString();
			OleDbDataReader reader = cmd.ExecuteReader();
			if(!reader.Read())
			{
				reader.Close();
				throw new BipObjectNotFoundException();
			}
			int ownerUserId = DbConvert.ToInt32( reader["OwnerUserId"] );
			bool isPublic = DbConvert.ToBoolean( reader["IsPublic"] );
			reader.Close();

			UserIdentity user = UserIdentity.Current;
			if(!(
				isPublic ||
				user.UserRole == UserRoles.Administrator ||
				user.UserRole == UserRoles.SystemOperator ||
				user.UserRole == UserRoles.Operator && ownerUserId == user.UserId 
				))
			{
				cmd.CommandText = @"select top 1 1 from 
									UserGroups inner join DocGroups on
									UserGroups.GroupId = DocGroups.GroupId
									where DocGroups.DocId = " + id.ToString() +
									" and UserGroups.UserId = " +  user.UserId.ToString();
				reader = cmd.ExecuteReader();
				if(!reader.Read())
				{
					reader.Close();
					throw new BipAccessDeniedException();
				}
				reader.Close();
			}
			
			cmd.CommandText =   @"
				select documents.*, 
				IsRead=case 
				when exists (select top 1 1 from UserReadDocs where DocId = Documents.Id and UserId=" + user.UserId.ToString() + @") then 1 else 0 end,
				IsFavorite=case 
				when exists (select top 1 1 from UserFavoriteDocs where DocId = Documents.Id and UserId=" + user.UserId.ToString() + @") then 1 else 0 end
				from documents 
				where documents.Id= " + id.ToString();

			reader = cmd.ExecuteReader();
			if(!reader.Read())
				throw new BipObjectNotFoundException();
			
			m_Id = id;
			m_CreationTime = DbConvert.ToDateTime(reader["CreationTime"]);
			m_FileTypeId = DbConvert.ToInt32(reader["FileType"]);
			m_SavedStorageFileName = DbConvert.ToString(reader["StorageFileName"]);
			m_FileName = DbConvert.ToString(reader["FileName"]);
			m_DateReceived = DbConvert.ToDateTime(reader["DateReceived"]);
			m_DocumentDate = DbConvert.ToDateTime(reader["DocumentDate"]);
			m_IncomingNumber = DbConvert.ToString(reader["IncomingNumber"]);
			m_OutgoingNumber = DbConvert.ToString(reader["OutgoingNumber"]);
			m_Subject = DbConvert.ToString(reader["Subject"]);
			m_Header = DbConvert.ToString(reader["Header"]);
			m_ArchiveFileNames = DbConvert.ToString(reader["ArchiveFileNames"]);
			m_DocTypeId = DbConvert.ToInt32(reader["DocTypeId"]);
			m_DocSourceId = DbConvert.ToInt32(reader["DocSourceId"]);
			m_DocCategoryId = DbConvert.ToInt32(reader["DocCategoryId"]);
			m_ParentId = DbConvert.ToInt32(reader["ParentId"]);
			m_PreviousVersionId = DbConvert.ToInt32(reader["PreviousVersionId"]);
			m_OwnerUserId = DbConvert.ToInt32(reader["OwnerUserId"]);
			m_IsPublic = DbConvert.ToBoolean(reader["IsPublic"]);
		
			
			m_IsRead = DbConvert.ToBoolean(reader["IsRead"]);
			m_IsFavorite = DbConvert.ToBoolean(reader["IsFavorite"]);
			reader.Close();
			cmd.Dispose();

			if(!CanRead(m_ParentId))
				m_ParentId = 0;
			if(!CanRead(m_PreviousVersionId))
				m_PreviousVersionId = 0;

			m_RefDocuments = CanRead(LoadDocRefs( "DocRefRelated", "RelatedDocId" ));
			m_Groups = LoadDocRefs( "DocGroups", "GroupId" );
			
			m_SavedFileTypeId = m_FileTypeId;
			
			if(!m_IsRead)
				MarkAsRead();
		}
		public override void Update()
		{
			if(! CanEdit)
				throw new BipAccessDeniedException();

			DbUpdate(); 
			FileUpdate();
		}
		public override void Delete()
		{
			if(! CanEdit)
				throw new BipAccessDeniedException();

			DbDelete();
			FileDelete();
		}

		static public bool CanRead(int id)
		{
			UserIdentity user = UserIdentity.Current;
			if(user.UserRole == UserRoles.Administrator ||
				user.UserRole == UserRoles.SystemOperator)
				return true;

			Database db = new Database();
			System.Data.OleDb.OleDbConnection con = db.Connection;
			OleDbCommand cmd = con.CreateCommand();
			cmd.CommandText = 
				@"select count(1) from documents
					where id = " + id.ToString() + 
					@" and( IsPublic=1 or OwnerUserId= " + user.UserId.ToString() + 
					@" or exists 
					(select top 1 1 from 
					UserGroups inner join DocGroups on
					UserGroups.GroupId = DocGroups.GroupId
					where DocGroups.DocId = Documents.id 
					and UserGroups.UserId = " + user.UserId.ToString() + " ))";

			bool canRead = (bool)(((int)cmd.ExecuteScalar()) > 0 );
			cmd.Dispose();
			db.Dispose();
			return canRead;
		}

		static public IEnumerable CanRead(IEnumerable ids)
		{
			if(ids == null)
				return null;
			IEnumerator enum_ids = ids.GetEnumerator();
			enum_ids.Reset();
			if(!enum_ids.MoveNext())
				return ids;

			
			UserIdentity user = UserIdentity.Current;
			if(user.UserRole == UserRoles.Administrator ||
				user.UserRole == UserRoles.SystemOperator)
				return ids;

			ArrayList res = new ArrayList();
			Database db = new Database();
			System.Data.OleDb.OleDbConnection con = db.Connection;
			OleDbCommand cmd = con.CreateCommand();
			cmd.CommandText = @"
				select id from documents
				where id in ( " + EnumUtils.ConvertToString(ids)  + 
				@" ) and 
				(IsPublic=1 or OwnerUserId= " + user.UserId.ToString() + 
				@" or exists 
				(select top 1 1 from 
				UserGroups inner join DocGroups on
				UserGroups.GroupId = DocGroups.GroupId
				where DocGroups.DocId = Documents.id 
				and UserGroups.UserId = " + user.UserId.ToString() + " ))";

			OleDbDataReader reader = cmd.ExecuteReader();
			while(reader.Read())
			{
				res.Add(Convert.ToInt32(reader["id"]));
			}
			reader.Close();
			cmd.Dispose();
			db.Dispose();

			return res;
		}

	
		public void ConfigureFileType(int newFileType)
		{
			if(m_FileTypeId == newFileType)
				return;
			m_FileTypeId = newFileType;
		}

		protected void SaveFileTypeInfo()
		{
			m_SavedFileTypeId = m_FileTypeId;
			m_SavedStorageFileName= StorageFileName;
		}
		protected byte[] LoadFile(string filePath)
		{
			if(!File.Exists(filePath))
				throw new  BipObjectNotFoundException();

			FileStream stm = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
			byte []fileContents = new byte[stm.Length];
			stm.Read(fileContents, 0, (int)stm.Length);
			stm.Close();
			return fileContents;
		}

		public byte[] DownloadFile(bool original)
		{
			// original param is not used because no HTML convertion is perfomed 
			if(m_Id < 1)
			{
				return LoadFile(m_UploadedFile.Path);
			}

			return LoadFile(FileStorage.OriginalDir + "\\" + m_Id.ToString());
		}

		public void UploadFile(Stream stm, string fileName)
		{
			if(m_UploadedFile == null)
				m_UploadedFile = new TempFile();
			string filePath = m_UploadedFile.Path;
			byte []buffer =new Byte[stm.Length];
			stm.Read(buffer, 0 , (int)stm.Length);
			stm.Close();
			File.Delete(filePath);
			Stream dstFile = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
			dstFile.Write(buffer, 0, (int)buffer.Length);
			dstFile.Close();
			ConfigureFileType( Bip.Components.DocFileType.ExamineFileType(Path.GetExtension(fileName)));
			FileName = fileName;
		}

		public void FileCreate()
		{
			if(m_Id <1)
				throw new BipFatalException();

			File.Copy(m_UploadedFile.Path, 	
						FileStorage.OriginalDir + "\\" + m_Id.ToString(),
						true);

			m_SavedFileTypeId = 0;
			m_SavedStorageFileName = "";
			FileUpdate();
		}

		public void FileUpdate()
		{
			if(m_SavedFileTypeId == m_FileTypeId)
				return;
			
			if(m_SavedStorageFileName != null && m_SavedStorageFileName.Length > 0)
				File.Delete(FileStorage.IndexingServiceDir + "\\" + m_SavedStorageFileName);
			string filePath = FileStorage.IndexingServiceDir + "\\" + StorageFileName;
			File.Copy(FileStorage.OriginalDir + "\\" + m_Id.ToString(), filePath, true); 
			
			SaveFileTypeInfo();
			
		}
		public void FileDelete()
		{
            if(m_Id <1)
				return;
			try
			{
				File.Delete(FileStorage.IndexingServiceDir + "\\" + m_SavedStorageFileName);
			}
			catch(Exception){}

			try
			{
				File.Delete(FileStorage.OriginalDir + "\\" + m_Id.ToString());
			}
			catch(Exception){}
		}
		
		public string GetFileUrl(bool original)
		{
			string url = "~/Documents/DocFileDownload.aspx?TC=1";
            if(original)
				url += "&Org=1";
			return url;
		}

		public void MarkAsRead()
		{
			if(m_Id < 1)
				return;

			OleDbCommand cmd = Db.Connection.CreateCommand();
			cmd.CommandText = "insert into userReadDocs (DocId, UserId) values ( " + m_Id.ToString() +
				"," + UserIdentity.Current.UserId.ToString() +" )";
			try
			{
				cmd.ExecuteNonQuery();
			}
			catch(Exception){}
		}

		public static DataTable FindEnum(IEnumerable ids)
		{
			if(ids == null)
				return null;

			IEnumerator enum_ids = ids.GetEnumerator();
			enum_ids.Reset();
			if(!enum_ids.MoveNext())
				return null;

			
			string securityConstraint = null;

			UserIdentity user = UserIdentity.Current;
			if(user.UserRole != UserRoles.Administrator &&
				user.UserRole != UserRoles.SystemOperator)
			securityConstraint = 
				" (IsPublic=1 or OwnerUserId= " + user.UserId.ToString() + 
				@" or exists 
				(select top 1 1 from 
				UserGroups inner join DocGroups on
				UserGroups.GroupId = DocGroups.GroupId
				where DocGroups.DocId = Documents.id 
				and UserGroups.UserId = " + user.UserId.ToString() + " )) ";

			string selectDocs = @"
				select * from documents
				where id in ( " + EnumUtils.ConvertToString(ids)  + " ) ";
			if(securityConstraint != null)
				selectDocs += " and " + securityConstraint;
		
			DataTable res = new DataTable();
			Database db = new Database();
			OleDbDataAdapter adapter = new OleDbDataAdapter(selectDocs,db.Connection);
			adapter.Fill(res);
			db.Dispose();

			return res;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -