using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Xpo;

namespace Nuvolar.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class Products_VC : ViewController
    {
        public Products_VC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            // Perform various tasks depending on the target View.
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }

        private void aUNCheck_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _onew = Application.CreateObjectSpace() as XPObjectSpace;

            XPCollection<Products> _Products_Collection =new XPCollection<Products>(_onew.Session,CriteriaOperator.Parse("UNNumber like 'UN%'"));

            foreach (Products _Products in _Products_Collection)
            {
                    UNList _UNList = _onew.FindObject<UNList>(CriteriaOperator.Parse("ShortName=?",_Products.UNNumber));
                    ProductsUNList _ProductsUNList = _onew.FindObject<ProductsUNList>(CriteriaOperator.Parse("Products=? and UNList=?",_Products,_UNList));
                if (_ProductsUNList==null && _UNList!=null)
                {
                    _ProductsUNList = _onew.CreateObject<ProductsUNList>();
                    _ProductsUNList.Products = _Products;
                    _ProductsUNList.UNList = _UNList;
                    _onew.CommitChanges();
                }
            }

        }
    }
}
