/// <summary>
        /// Get Inspections from service and Save to Database.
        /// </summary>
        /// <returns>The service.</returns>
        /// <param name="Token">Token.</param>
        public List <Inspection> InspectionService(string Token)
        {
            List <Inspection> inspections = new List <Inspection> ();

            try
            {
                // Get New assigned Inspections to the User
                ServiceResonse = new Model.ServiceModel.InspectionsRes();
                IServices service = new Services();
                ServiceResonse = service.FetchInspections(new Model.ServiceModel.InspectionsReq()
                {
                    token = Token
                });

                List <string> projectIDs = new List <string>();

                if (ServiceResonse != null && ServiceResonse.inspections != null && (ServiceResonse.result.code == 0 || ServiceResonse.result.code == 1))
                {
                    var inspectionLst = ServiceResonse.inspections.Where(id => id.inspectionTypeID == BALConstant.FINAL_INSPECTIONID);
                    if (inspectionLst != null && inspectionLst.Count() > 0)
                    {
                        List <string> Ids = new List <string>();
                        Ids = PunchListDO.getPunchProjectIds(conn, BALConstant.FINAL_INSPECTIONID);

                        foreach (var ins in inspectionLst)
                        {
                            if (Ids != null && Ids.Count > 0)
                            {
                                foreach (var id in Ids)
                                {
                                    if (ins.appID != id)
                                    {
                                        if (!projectIDs.Contains(ins.appID))
                                        {
                                            projectIDs.Add(ins.appID);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                projectIDs.Add(ins.appID);
                            }
                        }

                        if (projectIDs != null && projectIDs.Count > 0)
                        {
                            PunchLstResponse = service.FetchPuchList(new Model.ServiceModel.PunchListReq()
                            {
                                token = Token, appIDs = projectIDs
                            });
                        }
                        else
                        {
                            PunchLstResponse = null;
                        }
                    }
                    else
                    {
                        PunchLstResponse = null;
                    }
                }

                var ExistingInspection = GetInspectionTransactions();
                foreach (var insTrans in ExistingInspection)
                {
                    if (ServiceResonse.result != null && (ServiceResonse.result.code == 0 || ServiceResonse.result.code == 1))
                    {
                        var existingInspection = ServiceResonse.inspections.Find(si => si.inspectionTypeID == insTrans.inspectionID && si.appID == insTrans.projectID && si.pathway == insTrans.Pathway);
                        if (existingInspection == null)
                        {
                            if (insTrans.IsFinalise <= 0)
                            {
                                DeleteInspectionTransactionNotSync(insTrans);
                            }
                        }
                    }
                }
                int count = 0;

                foreach (var servInspect in ServiceResonse.inspections)
                {
                    if (PunchLstResponse != null && PunchLstResponse.result != null && (PunchLstResponse.result.code == 0 || PunchLstResponse.result.code == 1))
                    {
                        if (PunchLstResponse.projectPunchLists != null && PunchLstResponse.projectPunchLists.Count > 0)
                        {
                            foreach (var punchItems in PunchLstResponse.projectPunchLists)
                            {
                                string projectId = punchItems.appID;
                                if (servInspect.inspectionTypeID == BALConstant.FINAL_INSPECTIONID && projectId == servInspect.appID)
                                {
                                    if (punchItems.punchListItems != null && punchItems.punchListItems.Count > 0)
                                    {
                                        PunchListDO.DeletePunchList(conn, BALConstant.FINAL_INSPECTIONID, projectId);
                                        foreach (var punchcomments in punchItems.punchListItems)
                                        {
                                            List <Punch> punchList = new List <Punch>();

                                            foreach (var comment in punchcomments.comments)
                                            {
                                                punchList.Add(new Punch()
                                                {
                                                    punchDescription = comment,
                                                    ProjectID        = projectId,
                                                    InspectionID     = BALConstant.FINAL_INSPECTIONID
                                                });
                                            }
                                            PunchListDO.InsertPunchLists(conn, punchList);
                                            var PunchListID = PunchListDO.getPunchList(conn, BALConstant.FINAL_INSPECTIONID, projectId);
                                            foreach (var punchItem in PunchListID)
                                            {
                                                if (punchItem.PunchID != -1)
                                                {
                                                    OptionPunchDO.DeleteOptionPunchID(conn, punchItem.PunchID);
                                                    OptionPunchDO.InsertOptionPunch(conn, BALConstant.PUNCH_OPTIONID, punchItem.PunchID);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    var existingIns = GetInspectionTransactions().Find(i => i.inspectionID == servInspect.inspectionTypeID && i.projectID == servInspect.appID && i.Pathway == servInspect.pathway);
                    ///Punch Lists review in final Inspections of 25,50,75 and 90% created Punch Lists
                    if (existingIns == null)
                    {
                        var inspection = inspectionService.GetInspections().Where(i => i.inspectionID == servInspect.inspectionTypeID).FirstOrDefault();
                        if (!string.IsNullOrEmpty(servInspect.phoneNo))
                        {
                            servInspect.phoneNo = Regex.Replace(servInspect.phoneNo, @"(\d{3})(\d{3})(\d{4})", "$1-$2-$3");
                        }
                        string homeOwnerName = "";
                        if (!string.IsNullOrEmpty(servInspect.houseOwnerName))
                        {
                            var Name = servInspect.houseOwnerName.Split(',');
                            for (int i = Name.Length; i > 0; i--)
                            {
                                homeOwnerName += Name[i - 1];
                                homeOwnerName += " ";
                            }
                        }
                        List <Document> docList = new List <Document>();
                        if (servInspect.inspectionDocuments != null && servInspect.inspectionDocuments.Count > 0)
                        {
                            List <Model.ServiceModel.InspectionDocument> insDocumnents = new List <Model.ServiceModel.InspectionDocument>();
                            insDocumnents = servInspect.inspectionDocuments;

                            foreach (var insDoc in insDocumnents)
                            {
                                Model.Document doc = new Document();
                                doc.documentDisplayName    = insDoc.documentDisplayName;
                                doc.inspectionDocumentType = insDoc.inspectionDocumentType;
                                doc.documentID             = insDoc.documentID;
                                doc.inspectionID           = servInspect.inspectionTypeID;
                                doc.projectID = servInspect.appID;

                                using (DocViewService docServ = new DocViewService(conn))
                                {
                                    var existingDoc = docServ.GetDocumentItems(servInspect.inspectionTypeID, servInspect.appID).Where(d => d.documentID == doc.documentID);
                                    if (existingDoc != null && existingDoc.Count() > 0)
                                    {
                                        foreach (var docs in existingDoc)
                                        {
                                            docServ.DeleteDocumentItems(docs);
                                        }
                                    }
                                    docServ.SaveDocumentItems(doc);
                                }
                                docList.Add(doc);
                            }
                        }

                        inspections.Add(new Inspection()
                        {
                            inspectionID       = servInspect.inspectionTypeID,
                            inspectionDateTime = servInspect.inspectionDate,
                            ProjectName        = servInspect.projectName,
                            projectID          = servInspect.appID,
                            Pathway            = servInspect.pathway,
                            InspectionAddress1 = servInspect.activityAdress.addrline1,
                            InspectionAddress2 = servInspect.activityAdress.addrline2,
                            City                   = servInspect.activityAdress.city,
                            HouseOwnerName         = homeOwnerName.Trim(),
                            PhoneNo                = servInspect.phoneNo,
                            Pincode                = servInspect.activityAdress.postalCode,
                            InspectionType         = inspection != null ? inspection.InspectionType : "",
                            IsFinalise             = 0,
                            InspectionAttemptCount = (servInspect.info != null) ? servInspect.info.inspectionAttempt.ToString() : "",
                            ContractorName         = (servInspect.info != null) ? servInspect.info.contractorName : "",
                            HouseOwnerID           = (servInspect.info != null) ? servInspect.info.homeOwnerID.ToString() : "",
                            inspectionDocuments    = docList
                        });

                        SaveInspectionTransaction(inspections.LastOrDefault());
                        count++;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception Occured in InspectionService method due to " + ex.Message);
            }
            return(inspections);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Syncs the data.
        /// </summary>
        public void syncData()
        {
            lock (syncLock)
            {
                try
                {
                    if (!IsSyncProgress)
                    {
                        IsSyncProgress = true;

                        if (syncProgress != null)
                        {
                            syncProgress(true);
                        }
                        //Check if there are finished inspections
                        InspectionTransactionService transSer = new InspectionTransactionService(conn);
                        OptionTransactionService     OTS      = new OptionTransactionService(conn);
                        //int count = 0;
                        var                     finishedInspectionsQry = transSer.GetInspectionTransactions().Where(i => i.IsFinalise > 0);    // && (i.pass.ToLower () == "pass" || i.pass.ToLower () == "fail"));
                        UserService             userService            = new UserService(conn);
                        User                    user            = userService.GetUser();
                        IServices               service         = new Services();
                        InspectionDetailService ids             = new InspectionDetailService(conn);
                        OptionImageService      optImageservice = new OptionImageService(conn);
                        PunchService            pushservice     = new PunchService(conn);
                        DocViewService          docService      = new DocViewService(conn);
                        NotificationRepository  notificationRep = new NotificationRepository(conn);

                        if (finishedInspectionsQry.Any())
                        {
                            List <Inspection> finishedInspections = finishedInspectionsQry.Where(i => i.IsFinalise > 0).ToList();
                            if (finishedInspections.Count > 0)
                            {
                                int reportCount    = 0;
                                int punchListCount = 0;

                                Debug.WriteLine("sync thread started");

                                foreach (var inspection in finishedInspections)
                                {
                                    if (inspection == null)
                                    {
                                        Debug.WriteLine(string.Format("inspection is null"));
                                    }

                                    if (!inspection.isInspectionSynced)
                                    {
                                        if (notifiy != null)
                                        {
                                            notifiy(new notificationEventArgs
                                            {
                                                current = new Notifications
                                                {
                                                    notificationTypeID = inspection.projectID,
                                                    notificationType   = "Inspection",
                                                },
                                                isSyncCompleted = false,
                                            });
                                        }

                                        Model.ServiceModel.InspectionResults req = new Model.ServiceModel.InspectionResults();
                                        req.token       = user.Token;
                                        req.inspections = new List <Model.ServiceModel.InspectionResult>();
                                        Model.Inspection inspectionSer = ids.GetInspectionDetail(inspection, true);


                                        if (inspectionSer != null)
                                        {
                                            var insp = inspectionSer.getServiceModel();



                                            req.inspections.Add(insp);

                                            Model.ServiceModel.InspectionResultsAck inspectinPushRes = service.PushInspections(req);


                                            if (inspectinPushRes.result != null && inspectinPushRes.result.code == 0)
                                            {
                                                int inspectionTransactionID = 0;
                                                InspectionTransactionService insTransService = new InspectionTransactionService(conn);
                                                inspectionTransactionID = insTransService.GetInspectionTransactionID(inspection.projectID, inspection.inspectionID);

                                                //Loaction Image Delete after sync
                                                DAL.LocationImageDo.DeleteImage(conn, inspectionTransactionID);

                                                //Inspection Document Delete after sync
                                                var documents = docService.GetDocumentsForSync(inspection.inspectionID, inspection.projectID);
                                                if (documents != null && documents.Count > 0)
                                                {
                                                    if (DocumentDeleted != null)
                                                    {
                                                        DocumentDeleted(this, new DocumentDeletionEventArgs()
                                                        {
                                                            documentList = documents
                                                        });
                                                    }
                                                    docService.DeleteDocumentItemsForSync(inspection.inspectionID, inspection.projectID);
                                                }

                                                /// Punch List Delete for only final Inspection
                                                if (inspection.inspectionID == BALConstant.FINAL_INSPECTIONID)
                                                {
                                                    List <Punch> punchList = pushservice.getPunchList(inspection.inspectionID, inspection.projectID);
                                                    foreach (var punch in punchList)
                                                    {
                                                        PunchListImageDO.DeletePunchImageList(conn, punch.PunchID);
                                                        OptionPunchDO.DeleteOptionPunchID(conn, punch.PunchID);
                                                    }
                                                    PunchListDO.DeletePunchList(conn, BALConstant.FINAL_INSPECTIONID, inspection.projectID);
                                                }

                                                using (LevelTransactionService levelTransactionService = new LevelTransactionService(conn))
                                                {
                                                    var levelTransactions = levelTransactionService.GetLevelTransactions();

                                                    if (levelTransactions != null)
                                                    {
                                                        foreach (var levelTrans in levelTransactions)
                                                        {
                                                            levelTransactionService.DeleteLevelTransactions(levelTrans);
                                                        }
                                                    }
                                                }


                                                using (SpaceTransactionService spaceTransactionService = new SpaceTransactionService(conn))
                                                {
                                                    var spaceTransactions = spaceTransactionService.GetSpaceTransactions();

                                                    if (spaceTransactions != null)
                                                    {
                                                        foreach (var spaceTrans in spaceTransactions
                                                                 )
                                                        {
                                                            spaceTransactionService.DeleteSpaceTransactions(spaceTrans);
                                                        }
                                                    }
                                                }


                                                //Get Option transactions for inspection(Not including punch list)
                                                List <OptionTransaction> optiontransactionLst = null;
                                                optiontransactionLst = OTS.GetOptionTransactionsForInspection(inspectionTransactionID);

                                                //Delete Item Transaction
                                                if (optiontransactionLst != null && optiontransactionLst.Count > 0)
                                                {
                                                    foreach (var optiontransaction in optiontransactionLst)
                                                    {
                                                        //Option Image Deletion
                                                        optImageservice.DeleteOptionImagesForSync(conn, optiontransaction.ID);

                                                        //Guided picture deletion
                                                        if (optiontransaction.OptionId == BALConstant.GUIDEDPICTURE_OPTIONID)
                                                        {
                                                            var chkTransIDs = CheckListTransactionDO.GetCheckListTransaction(conn, optiontransaction.ID).Select(s => s.ID);
                                                            if (chkTransIDs != null && chkTransIDs.Count() > 0)
                                                            {
                                                                foreach (var chkId in chkTransIDs)
                                                                {
                                                                    GuildedPhotoDO.DeleteGuidedImageList(conn, chkId);
                                                                }
                                                            }
                                                        }
                                                        // Checklist transaction Deletion
                                                        CheckListTransactionDO.DeletecheckList(conn, optiontransaction.ID);

                                                        //Option Transaction Row deletion
                                                        OTS.DeleteOptionTransactions(optiontransaction);
                                                    }
                                                }

                                                inspection.isInspectionSynced = true;
                                                inspection.InspectionStarted  = 0;

                                                transSer.UpdateInspectionTransaction(inspection);
                                                //update notification table with successfully uploaded insÏpection
                                                notificationRep.Save("Inspection", inspection.projectID, "Inspection Results for App ID  : " + inspection.projectID + "  successfully synced");
                                            }
                                            else
                                            {
                                                //update notification table with retry count for inspection
                                                notificationRep.Save("Inspection", inspection.projectID, "Inspection Results for App ID : " + inspection.projectID + " not synced");
                                            }
                                        }
                                    }

                                    if (inspection.isInspectionSynced)
                                    {                                     ///If inspection sync is success
                                        ReportService        repservice = new ReportService(conn);
                                        IEnumerable <Report> reports    = repservice.GetReports().Where(r => r.InspectionTransID == inspection.ID);
                                        if (reports != null)
                                        {
                                            foreach (var report in reports)
                                            {
                                                if (notifiy != null)
                                                {
                                                    notifiy(new notificationEventArgs
                                                    {
                                                        current = new Notifications
                                                        {
                                                            notificationTypeID = report.ReportID + "-" + inspection.projectID,
                                                            notificationType   = "Report",
                                                        },
                                                        isSyncCompleted = false,
                                                    });
                                                }

                                                Model.ServiceModel.InspectionReportAck inspectionReoprtRes = service.PushReport(new Model.ServiceModel.InspectionReport()
                                                {
                                                    inspectionTypeID = inspection.inspectionID,
                                                    appID            = inspection.projectID,
                                                    report           = report.ReportDesc,
                                                    reportName       = report.ReportType,
                                                    token            = user.Token,
                                                });
                                                if (inspectionReoprtRes.result != null && inspectionReoprtRes.result.code == 0)
                                                {
                                                    notificationRep.Save(report.ReportType.ToUpper(), inspection.projectID, report.ReportType.ToUpper() + " Report for App ID : " + inspection.projectID + " successfully synced");
                                                    repservice.DeleteReport(report);
                                                }
                                                else
                                                {
                                                    notificationRep.Save(report.ReportType.ToUpper(), inspection.projectID, report.ReportType.ToUpper() + " Report for App ID  : " + inspection.projectID + " not synced");
                                                }
                                            }
                                        }


                                        List <Model.ServiceModel.PunchListItem> PunchItemList = new List <Model.ServiceModel.PunchListItem>();
                                        if (inspection.inspectionID != BALConstant.FINAL_INSPECTIONID)
                                        {
                                            if (notifiy != null)
                                            {
                                                notifiy(new notificationEventArgs
                                                {
                                                    current = new Notifications
                                                    {
                                                        notificationTypeID = inspection.projectID,
                                                        notificationType   = "Punchlist",
                                                    },
                                                    isSyncCompleted = false,
                                                });
                                            }

                                            List <Punch> punchList = pushservice.getPunchList(inspection.inspectionID, inspection.projectID);
                                            if (punchList != null && punchList.Count > 0)
                                            {
                                                foreach (var punch in punchList)
                                                {
                                                    List <PunchListImageDO> ImageTransLst = PunchListImageDO.getPunchImageList(conn, punch.PunchID);
                                                    List <byte[]>           images        = new List <byte[]>();

                                                    if (ImageTransLst != null)
                                                    {
                                                        foreach (var ImageTrans in ImageTransLst)
                                                        {
                                                            images.Add(ImageTrans.PunchListImage);
                                                        }
                                                    }

                                                    PunchItemList.Add(new Model.ServiceModel.PunchListItem()
                                                    {
                                                        photos   = images,
                                                        comment  = punch.punchDescription,
                                                        sequence = -1
                                                    });
                                                }

                                                Model.ServiceModel.PunchListAck inspectionPunchListRes = service.PushPunchList(new Model.ServiceModel.PunchListResult()
                                                {
                                                    inspectionTypeID = inspection.inspectionID,
                                                    appID            = inspection.projectID,
                                                    punchList        = PunchItemList,
                                                    token            = user.Token
                                                });
                                                if (inspectionPunchListRes.result != null && inspectionPunchListRes.result.code == 0)
                                                {
                                                    //update notification table with successfully uploaded punch list
                                                    notificationRep.Save("Punchlist", inspection.projectID, "Punch List for App ID : " + inspection.projectID + " successfully synced");
                                                    foreach (Punch item in punchList)
                                                    {
                                                        pushservice.DeletePunchItem(item);
                                                        PunchListImageDO.DeletePunchImageList(conn, item.PunchID);
                                                    }
                                                    OptionTransactionDO.DeleteInspectionOptions(conn, inspection.ID);
                                                }
                                                else
                                                {
                                                    //update notification table with retry count for punch list
                                                    notificationRep.Save("Punchlist", inspection.projectID, "Punch List for App ID : " + inspection.projectID + " not synced");
                                                }
                                            }
                                        }
                                    }

                                    if (inspection.isInspectionSynced)
                                    {
                                        using (ReportService repservice = new ReportService(conn))
                                        {
                                            IEnumerable <Report> reports = repservice.GetReports().Where(r => r.InspectionTransID == inspection.ID);
                                            if (reports != null && reports.Count() > 0)
                                            {
                                                reportCount = reports.Count();
                                            }
                                        }
                                        if (inspection.inspectionID != BALConstant.FINAL_INSPECTIONID)
                                        {
                                            List <Punch> punchList = pushservice.getPunchList(inspection.inspectionID, inspection.projectID);
                                            if (punchList != null && punchList.Count > 0)
                                            {
                                                punchListCount = punchList.Count;
                                            }
                                        }
                                        if (reportCount == 0 && punchListCount == 0)
                                        {
                                            transSer.DeleteInspectionTransaction(inspection);
                                            continue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception Occured in Syncdata method due to " + ex.Message);
                }
                finally
                {
                    if (notifiy != null)
                    {
                        notifiy(new notificationEventArgs
                        {
                            current         = new Notifications(),
                            isSyncCompleted = true,
                        });
                    }
                    if (syncProgress != null)
                    {
                        syncProgress(false);
                    }
                    IsSyncProgress = false;
                    UIApplication.SharedApplication.InvokeOnMainThread(delegate
                    {
                        UIApplication.SharedApplication.IdleTimerDisabled = false;
                        Debug.WriteLine("sync thread finished");
                    });
                }
            }
        }