/////////////////////////////////////////////////////////// // AyaFileList.cs // Implementation of Class AyaFileList // CSLA type: Read only collection // Created on: 31-Rocktober-2008 // Object design: John // Coded: 31-Rocktober-2008 /////////////////////////////////////////////////////////// using System; using System.Data; using GZTW.Data; using CSLA.Data; using CSLA; using System.Collections.Generic; namespace GZTW.AyaNova.BLL { /// /// Used to fetch a read only list of objects /// representing files stored inside the database. /// /// Used for Wiki feature and for general file management by admin /// /// [Serializable] public class AyaFileList : ReadOnlyCollectionBase { #region Data structure /// /// Properties /// [Serializable] public struct AyaFileListInfo { internal GridNameValueCellItem mFile; /// /// File name an internal ID /// [SqlColumnNameAttribute("aFile.aName", "aFile.aID"), Display(DisplayType.Button, RootObjectType = RootObjectTypes.AyaFile)] public GridNameValueCellItem LT_O_AyaFile { get { return mFile; } } internal GridNameValueCellItem mSource; /// /// Source of the file (which AyaNova object it's associated with) /// [SqlColumnNameAttribute("grid"), Display(DisplayType.Button, RootObjectType = RootObjectTypes.AyaFile)] public GridNameValueCellItem LT_AyaFile_Label_Source { get { return mSource; } } // Object to open to view this search results original record // If this object is not a child or grandchild, then this is the same as RootObjectID internal Guid mRootObjectID; internal RootObjectTypes mRootObjectType; /// /// Files associated object ID /// [Display(DisplayType.Hidden)] public Guid RootObjectID { get { return mRootObjectID; } } /// /// Files associated object type /// [Display(DisplayType.Hidden)] public RootObjectTypes RootObjectType { get { return mRootObjectType; } } /// /// The of this file /// // [SqlColumnNameAttribute("aFile.aFileType"), Display(DisplayType.Text)] [SqlColumnNameAttribute("grid"), Display(DisplayType.Text)] public AyaFileType FileType { get { return mFileType; } } internal AyaFileType mFileType; internal string mFileSize; /// /// File size converted to largest whole display unit and unit type appended (i.e. 10MB) see /// for more. /// [SqlColumnNameAttribute("aFile.aFileSize"), Display(DisplayType.Text)] public string LT_AyaFile_Label_FileSize { get { return mFileSize; } } internal int mFileSizeBytes; /// /// File size in bytes (actual file size when uncompressed, stored in DB compressed where possible) /// [Display(DisplayType.Hidden)] public int FileSizeBytes { get { return mFileSizeBytes; } } internal string mFileSizeStored; /// /// Actual size as stored in Database in human readable format () /// [SqlColumnNameAttribute("aFile.aFObjectSize"), Display(DisplayType.Text)] public string LT_AyaFile_Label_FileSizeStored { get { return mFileSizeStored; } } internal SmartDate mCreated; /// /// DateTime the record was created /// [SqlColumnNameAttribute("aFile.aCreated"), Display(DisplayType.DateTime)] public object LT_Common_Label_Created { get { return mCreated.DBValue; } } internal SmartDate mModified; /// /// Date and time this record was last modified /// [SqlColumnNameAttribute("aFile.aModified"), Display(DisplayType.DateTime)] public object LT_Common_Label_Modified { get { return mModified.DBValue; } } internal GridNameValueCellItem mCreator; /// /// Creator of record /// [SqlColumnNameAttribute("aFile.aCreator", "aFile.aCreator"), Display(DisplayType.Button, RootObjectType = RootObjectTypes.User)] public GridNameValueCellItem LT_Common_Label_Creator { get { return mCreator; } } internal GridNameValueCellItem mModifier; /// /// User who last modified this record /// [SqlColumnNameAttribute("aFile.aModifier", "aFile.aModifier"), Display(DisplayType.Button, RootObjectType = RootObjectTypes.User)] public GridNameValueCellItem LT_Common_Label_Modifier { get { return mModifier; } } /// /// /// /// public bool Equals(AyaFileListInfo obj) { return this.mFile.Value.Equals(obj.mFile.Value); } }//end AyaFileListInfo #endregion #region Constructor /// /// /// protected AyaFileList() { // AllowSort=false; // AllowFind=true; // AllowEdit=false; // AllowNew=false; // AllowRemove=false; } #endregion #region Business properties and methods /// /// Get item by index /// /// public AyaFileListInfo this[int Item] { get { return (AyaFileListInfo) List[Item]; } } /// /// Returns AyaFileListInfo object that matches passed in File Name value /// /// public AyaFileListInfo? this[string FileName] { get { foreach (AyaFileListInfo child in List) { if (child.LT_O_AyaFile.Display == FileName) return child; } return null; } } /// /// Returns display text that matches passed in itemid value /// /// public string this[Guid ItemID] { get { foreach (AyaFileListInfo child in List) { if(child.mFile.Value==ItemID) return child.ToString(); } return "Missing: "+ItemID.ToString(); } } #endregion #region contains /// /// Check if item in collection /// /// public bool Contains(AyaFileListInfo obj) { foreach (AyaFileListInfo child in List) { if(child.Equals(obj)) return true; } return false; } /// /// Check if item in collection by file name /// /// /// public bool Contains(string FileName) { foreach (AyaFileListInfo child in List) { if (child.LT_O_AyaFile.Display==FileName) return true; } return false; } #endregion #region Reporting and shared UI editor helpers /// /// Returns the report key which is a property of /// reports used to link all reports that can be used /// with a particular data source. /// public static string ReportKey { get { return "AyaFileList"; } } /// /// Returns the Detailed report key /// which is used to determine which reports and objects /// will be used for detailed reports /// /// If empty string then indicates there is no detailed report object or reports /// public static string DetailedReportKey { get { return ""; } } /// /// Base object that this list is reporting on /// used by shared UI editor to instantiate new objects /// when user selects new in UI elements that display this list /// /// (I.E. when user clicks on new in a read only list grid, this is the object type created) /// public static RootObjectTypes BaseObjectType { get { return RootObjectTypes.AyaFile; } } /// /// Locale key so that generic list editor /// UI code knows what title to give the list in a /// grid /// public string LocaleKey { get { return "AyaFile.Label.List"; } } /// /// The Type of the struct used to store list records /// Used to fetch the custom display attributes of the fields /// contained within the record to modify the grid display accordingly /// /// public static Type ListRecordType { get { return typeof(AyaFileListInfo); } } /// /// Field that contains the ID of the objects /// that are the basis of this list. /// /// Used for compiling an ID list for reporting from user /// selections in a grid. /// public static string IDField { get { return "LT_O_AyaFile"; } } /// /// Same as IDField but for detailed reports /// public static string IDFieldDetailed { get { return IDField; } } #endregion #region Static methods /// /// Get all for given RootObjectID /// (typically the ID of a WikiPage /// /// /// List of objects public static AyaFileList GetList(Guid RootObjectID) { return (AyaFileList) DataPortal.Fetch(new Criteria("",null,-1,RootObjectID)); } /// /// Internal method used by list factory /// internal static AyaFileList Get(string Filter, int MaxRecords, List IDList) { return (AyaFileList)DataPortal.Fetch(new Criteria(Filter, IDList, MaxRecords, Guid.Empty)); } /// /// Get all (filtered by crit) /// /// Use AyaNova UI to easily build xmlCriteria and Ctrl-Alt-g keyboard command to display it for use in your code /// List of objects public static AyaFileList GetList(string xmlCriteria) { return (AyaFileList) DataPortal.Fetch(new Criteria(xmlCriteria,null,-1, Guid.Empty)); } /// /// Get list by items indicated in IDList /// /// Generic list of Guid's /// List of objects public static AyaFileList GetListFromIDList(List IDList) { //case 556 //Handle empty list if (IDList.Count == 0) return new AyaFileList(); return (AyaFileList)DataPortal.Fetch(new Criteria("", IDList, -1, Guid.Empty)); } /// /// Used internally to fetch info for a single specific file /// /// /// List of one object public static AyaFileList GetListOfOne(Guid AyaFileID) { List l = new List(); l.Add(AyaFileID); return GetListFromIDList(l); } /// /// Return an empty list /// used for initializing grid /// /// Empty List of zero objects public static AyaFileList GetEmptyList() { return new AyaFileList(); } #endregion #region DAL DATA ACCESS /// /// protected override void DataPortal_Fetch(object Criteria) { //cached user list UserPickList users = UserPickList.GetList(false); Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { string q = ""; //************************************************************ if (crit.RootObjectID != Guid.Empty) { //Standard case for wikipages dr=DBUtil.GetReaderFromSQLString("SELECT aID, aCreated,aModified,aCreator,aModifier, " + "aRootObjectID, aRootObjectType, aFileType, aName, aFileSize, aFObjectSize " + "FROM aFile WHERE aRootObjectID=@ID ", crit.RootObjectID); } else if (crit.IDList != null) { //Case 556 System.Text.StringBuilder sbIN = new System.Text.StringBuilder(); sbIN.Append(" WHERE aFile.aID in ("); foreach (Guid gItem in crit.IDList) { sbIN.Append("'"); sbIN.Append("{"); sbIN.Append(gItem.ToString().ToUpperInvariant()); sbIN.Append("}"); sbIN.Append("',"); } sbIN.Length = sbIN.Length - 1; sbIN.Append(") "); q = "SELECT aID, aCreated,aModified,aCreator,aModifier, " + "aRootObjectID, aRootObjectType, aFileType, aName, aFileSize, aFObjectSize " + "FROM aFile " + sbIN.ToString() + " " + AyaBizUtils.GetGridSortOrderColumns(crit.CriteriaXML); dr = DBUtil.GetReaderFromSQLString(q); } else { q = "SELECT ~MAXRECS~ aID, aCreated,aModified,aCreator,aModifier, " + "aRootObjectID, aRootObjectType, aFileType, aName, aFileSize, aFObjectSize " + "FROM aFile " + //Case 58; AyaBizUtils.GetGridColumnCriteria(crit.CriteriaXML, true) + " " + AyaBizUtils.GetGridSortOrderColumns(crit.CriteriaXML); if (crit.MaxRecords > 0) q = q.Replace("~MAXRECS~", "TOP " + crit.MaxRecords.ToString()); else q = q.Replace("~MAXRECS~", ""); dr = DBUtil.GetReaderFromSQLString(q); } //************************************************************ while(dr.Read()) { //******************************************* AyaFileListInfo info=new AyaFileListInfo(); info.mFileType = (AyaFileType)dr.GetInt16("aFileType"); ////Don't load image files into list when in a wikipage ////as they are handled internally, no need to show to user and confuse them //if (crit.RootObjectID != Guid.Empty)//it's a wikipage // if (info.mFileType == AyaFileType.EmbeddedWikiImage) // continue; info.mFile=new GridNameValueCellItem( dr.GetGuid("aID"), dr.GetString("aName"), RootObjectTypes.AyaFile); info.mCreated = DBUtil.ToLocal(dr.GetSmartDate("aCreated")); info.mCreator = new GridNameValueCellItem( dr.GetGuid("aCreator"), users[dr.GetGuid("aCreator")], RootObjectTypes.User); info.mModifier = new GridNameValueCellItem( dr.GetGuid("aModifier"), users[dr.GetGuid("aModifier")], RootObjectTypes.User); info.mModified = DBUtil.ToLocal(dr.GetSmartDate("aModified")); info.mFileSize = AyaBizUtils.FileSizeDisplay(System.Convert.ToDecimal(dr.GetInt32("aFileSize"))); info.mFileSizeStored = AyaBizUtils.FileSizeDisplay(System.Convert.ToDecimal(dr.GetInt32("aFObjectSize"))); info.mFileSizeBytes = dr.GetInt32("aFileSize"); info.mRootObjectID = dr.GetGuid("aRootObjectID"); info.mRootObjectType = (RootObjectTypes)dr.GetInt16("aRootObjectType"); info.mSource = new GridNameValueCellItem( info.mRootObjectID, EnumDescConverter.GetEnumDescription(info.mRootObjectType), info.mRootObjectType); InnerList.Add(info); //******************************************* } } finally { if(dr!=null) dr.Close(); } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public List IDList; public string CriteriaXML; public int MaxRecords; public Guid RootObjectID; public Criteria(string _CriteriaXML, List _IDList, int _MaxRecords,Guid _RootObjectID) { CriteriaXML = _CriteriaXML; IDList = _IDList; MaxRecords = _MaxRecords; RootObjectID=_RootObjectID; } } #endregion }//end AyaFileList }//end namespace GZTW.AyaNova.BLL