/// <summary> /// establish files /// </summary> /// <returns></returns> public override async Task EstablishFilesAsync() { // set up transaction data folder await _coreDriver.RootFolder.CreateFolderAsync(TRANSACTION_DATA_FOLDER , CreationCollisionOption.OpenIfExists); _coreDriver .logDebugInfo( this.GetType(), 125, "Create transaction data root folder.", MessageType.INFO); foreach (MonthIdentity monthId in _coreDriver.MonthIds) { // create transaction file for each available month ledger MonthLedger ledger = new MonthLedger(monthId); _list.Add(monthId, ledger); XDocument xdoc = ledger.toXML(); StorageFile file = await generateFilePath(monthId, true); try { await FileIO.WriteTextAsync(file, xdoc.ToString()); } catch (FileNotFoundException e) { _coreDriver.logDebugInfo(this.GetType(), 530, e.Message, MessageType.ERRO); throw new SystemException(e); } } }
/// <summary> /// get entity /// </summary> /// <param name="docId"></param> /// <returns></returns> public HeadEntity GetEntity(DocumentIdentity docId) { MonthLedger ledger = this.GetLedger(docId._monthIdentity); if (ledger == null) { return(null); } return(ledger.GetEntity(docId)); }
/// <summary> /// store month ledger to file system /// </summary> /// <param name="monthId"></param> /// <exception cref="SystemException"></exception> public async Task StoreAsync(MonthIdentity monthId) { // store master data _coreDriver.logDebugInfo(this.GetType(), 293, String.Format("Start storing %s to disk", monthId), MessageType.INFO); _coreDriver.logDebugInfo(this.GetType(), 297, "Store master data at first.", MessageType.INFO); await this._masterDataMgmt.StoreAsync(); MonthLedger collection = this.GetLedger(monthId); StorageFile file = await this.generateFilePath(monthId, true); _coreDriver.logDebugInfo(this.GetType(), 297, "Generate file path: " + file.Path, MessageType.INFO); if (file == null) { StorageFolder transactionFolder = await _coreDriver.RootFolder .GetFolderAsync(TRANSACTION_DATA_FOLDER); if (transactionFolder == null) { throw new SystemException(null); // bug } String fileName = String.Format("{0}.xml", monthId.ToString()); file = await transactionFolder.CreateFileAsync(fileName , CreationCollisionOption.OpenIfExists); } XDocument xdoc = collection.toXML(); _coreDriver .logDebugInfo(this.GetType(), 297, "Parsed document collections to XML document", MessageType.INFO); try { await FileIO.WriteTextAsync(file, xdoc.ToString()); } catch (FileNotFoundException e) { _coreDriver.logDebugInfo(this.GetType(), 316, e.Message, MessageType.ERRO); throw new SystemException(e); } _coreDriver.logDebugInfo(this.GetType(), 335, "Save document collection successfully.", MessageType.INFO); }
/// <summary> /// Get Ledger /// </summary> /// <param name="monthId"></param> /// <returns></returns> public MonthLedger GetLedger(MonthIdentity monthId) { // get current calendar month MonthIdentity curMonthId = _coreDriver.CurCalendarMonthId; // check whether ledger beyond the range if (_coreDriver.StartMonthId.CompareTo(monthId) > 0 || curMonthId.CompareTo(monthId) < 0) { return(null); } // create new month ledger MonthLedger monthLedger; if (!_list.TryGetValue(monthId, out monthLedger)) { monthLedger = new MonthLedger(monthId); _list.Add(monthId, monthLedger); } return(monthLedger); }
/// <summary> /// load month identity /// </summary> /// <param name="monthId"></param> /// <returns></returns> /// <exception cref="TransactionDataFileFormatException"></exception> public async Task <MonthLedger> Load(MonthIdentity monthId) { // construct month ledger MonthLedger monthledger = new MonthLedger(monthId); _list.Add(monthId, monthledger); _coreDriver.logDebugInfo( this.GetType(), 102, String.Format("Loading transaction data {0} ...", monthId.ToString()), MessageType.INFO); // get file path StorageFile file = await generateFilePath(monthId, false); if (file == null) {// empty return(monthledger); } _coreDriver.logDebugInfo(this.GetType(), 109, String.Format("Transaction data file: {0} .", file.Path), MessageType.INFO); try { string text = await FileIO.ReadTextAsync(file); XDocument xdoc = XDocument.Parse(text); XElement rootElem = xdoc.Element(TransDataUtils.XML_ROOT); // no root element if (rootElem == null) { throw new TransactionDataFileFormatException(file.Path); } // ------------------------------------------------------------------- // parse all the documents foreach (XElement elem in rootElem.Elements(TransDataUtils.XML_DOCUMENT)) { HeadEntity head = HeadEntity.Parse(_coreDriver, _masterDataMgmt, elem); _coreDriver .logDebugInfo( this.GetType(), 172, String.Format( "Document {0} add to list during loading.", head.DocIdentity .ToString()), MessageType.INFO); monthledger.Add(head); // raise load document _coreDriver.ListenerMgmt.LoadDoc(this, head); // raise reverse document if (head.IsReversed) { _coreDriver.ListenerMgmt.ReverseDoc(head); } } // ----------------------------------------------------------------- return(monthledger); } catch (TransactionDataFileFormatException e) { _coreDriver.logDebugInfo(this.GetType(), 170, e.Message, MessageType.ERRO); throw new TransactionDataFileFormatException(file.Path); } }
/// <summary> /// save document /// </summary> /// <param name="head"></param> /// <param name="needStroe"></param> /// <exception cref="SystemException"></exception> /// <exception cref="SaveClosedLedgerException"></exception> internal async Task saveDocumentAsync(HeadEntity head, bool needStroe) { _coreDriver.logDebugInfo(this.GetType(), 231, "Call transaction to save the document", MessageType.INFO); if (head.IsSaved) { _coreDriver.logDebugInfo(this.GetType(), 235, "Document is saved, just to store the update on disk.", MessageType.INFO); } else { _coreDriver.logDebugInfo(this.GetType(), 239, "Document is never saved", MessageType.INFO); MonthIdentity monthId = head.MonthID; MonthLedger ledger = this.GetLedger(monthId); if (ledger == null) { _coreDriver.logDebugInfo(this.GetType(), 239, "Error in document month identity", MessageType.ERRO); throw new SaveClosedLedgerException(); } // set document number DocumentNumber num; List <HeadEntity> entities = ledger.Entities; if (entities.Count == 0) { try { num = new DocumentNumber("1000000001".ToCharArray()); } catch (IdentityTooLong e) { _coreDriver.logDebugInfo(this.GetType(), 239, e.ToString(), MessageType.ERRO); throw new SystemException(e); } catch (IdentityNoData e) { _coreDriver.logDebugInfo(this.GetType(), 239, e.ToString(), MessageType.ERRO); throw new SystemException(e); } catch (IdentityInvalidChar e) { _coreDriver.logDebugInfo(this.GetType(), 239, e.ToString(), MessageType.ERRO); throw new SystemException(e); } } else { HeadEntity last = entities[entities.Count - 1]; num = last.DocNumber.Next(); } _coreDriver.logDebugInfo(this.GetType(), 239, "Generate document number " + num.ToString(), MessageType.INFO); head._docNumber = num; ledger.Add(head); } if (needStroe) { await StoreAsync(head.MonthID); _coreDriver.logDebugInfo(this.GetType(), 465, "Memory has been stored to disk", MessageType.INFO); } else { _coreDriver.logDebugInfo(this.GetType(), 465, "Memory has NOT been stored to disk", MessageType.INFO); } _coreDriver.logDebugInfo(this.GetType(), 278, "Call transaction to save the document successfully", MessageType.INFO); }
/// <summary> /// load month identity /// </summary> /// <param name="monthId"></param> /// <returns></returns> /// <exception cref="TransactionDataFileFormatException"></exception> public async Task<MonthLedger> Load(MonthIdentity monthId) { // construct month ledger MonthLedger monthledger = new MonthLedger(monthId); _list.Add(monthId, monthledger); _coreDriver.logDebugInfo( this.GetType(), 102, String.Format("Loading transaction data {0} ...", monthId.ToString()), MessageType.INFO); // get file path StorageFile file = await generateFilePath(monthId, false); if (file == null) {// empty return monthledger; } _coreDriver.logDebugInfo(this.GetType(), 109, String.Format("Transaction data file: {0} .", file.Path), MessageType.INFO); try { string text = await FileIO.ReadTextAsync(file); XDocument xdoc = XDocument.Parse(text); XElement rootElem = xdoc.Element(TransDataUtils.XML_ROOT); // no root element if (rootElem == null) { throw new TransactionDataFileFormatException(file.Path); } // ------------------------------------------------------------------- // parse all the documents foreach (XElement elem in rootElem.Elements(TransDataUtils.XML_DOCUMENT)) { HeadEntity head = HeadEntity.Parse(_coreDriver, _masterDataMgmt, elem); _coreDriver .logDebugInfo( this.GetType(), 172, String.Format( "Document {0} add to list during loading.", head.DocIdentity .ToString()), MessageType.INFO); monthledger.Add(head); // raise load document _coreDriver.ListenerMgmt.LoadDoc(this, head); // raise reverse document if (head.IsReversed) _coreDriver.ListenerMgmt.ReverseDoc(head); } // ----------------------------------------------------------------- return monthledger; } catch (TransactionDataFileFormatException e) { _coreDriver.logDebugInfo(this.GetType(), 170, e.Message, MessageType.ERRO); throw new TransactionDataFileFormatException(file.Path); } }
/// <summary> /// Get Ledger /// </summary> /// <param name="monthId"></param> /// <returns></returns> public MonthLedger GetLedger(MonthIdentity monthId) { // get current calendar month MonthIdentity curMonthId = _coreDriver.CurCalendarMonthId; // check whether ledger beyond the range if (_coreDriver.StartMonthId.CompareTo(monthId) > 0 || curMonthId.CompareTo(monthId) < 0) { return null; } // create new month ledger MonthLedger monthLedger; if (!_list.TryGetValue(monthId, out monthLedger)) { monthLedger = new MonthLedger(monthId); _list.Add(monthId, monthLedger); } return monthLedger; }