Ejemplo n.º 1
0
        public DocumentBrowserViewModel(Type modelType, Object entityModel)
        {
            this.ModelType = modelType;
            this.EntityModel = entityModel ;
            this.DataService = ServiceLocator.Current.GetInstance<IDataService>();
            this.Codes = new ObservableCollection<DocumentCodeViewModel>();
            this.EntityTableInfo = this.DataService.GetEntityTableInfo(this.ModelType);
            PropertyInfo propModelId = modelType.GetProperty("Id");
            Int64 modelId = (Int64)propModelId.GetValue(entityModel);
            String entityTableName = EntityTableInfo.TableName.ToLower();

            String camelSchemaName = EntityTableInfo.SchemaInfo.SchemaName.Substring(0, 1).ToUpper() + EntityTableInfo.SchemaInfo.SchemaName.Substring(1).ToLower();
            DbSet setCodeDoc = this.DataService.GetDbSet(camelSchemaName + "CodeDoc");

            DbSet<PrfParam> parameters = this.DataService.GetDbSet<PrfParam>();

            PrfParam paramPathDoc = (from p in parameters where p.PrfSchema.Name.Equals(camelSchemaName.ToUpper()) && p.Code.Equals("DIRECTORY_DOC") select p).FirstOrDefault();

            PropertyInfo propCode = setCodeDoc.ElementType.GetProperty("Code");
            PropertyInfo propPath = setCodeDoc.ElementType.GetProperty("Path");
            PropertyInfo propLibelle = setCodeDoc.ElementType.GetProperty("Libelle");
            PropertyInfo propId = setCodeDoc.ElementType.GetProperty("Id");
            foreach (Object item in setCodeDoc)
            {
                DocumentCodeViewModel vm = new DocumentCodeViewModel(paramPathDoc.Valeur);
                vm.Code = propCode.GetValue(item).ToString();
                vm.Libelle = propLibelle.GetValue(item).ToString();
                vm.Path = propPath.GetValue(item).ToString();
                vm.Id = (Int64)propId.GetValue(item);
                if (!Directory.Exists(vm.AbsolutePath))
                {Directory.CreateDirectory(vm.AbsolutePath);}
                this.Codes.Add(vm);

            }

            DbSet setCls = this.DataService.GetDbSet(camelSchemaName + "Cls");
            ParameterExpression expressionBase = System.Linq.Expressions.Expression.Parameter(setCls.ElementType, "item");
            List<Expression> expressions = new List<Expression>();

            expressions.Add(Expression.Equal(Expression.Property(expressionBase, "TableName"), Expression.Constant(EntityTableInfo.TableName)));
            if (this.EntityModel == null)
            {
                expressions.Add(Expression.Equal(Expression.Property(expressionBase, "RowId"), Expression.Constant(null)));
            }
            else
            {
                expressions.Add(Expression.Equal(Expression.Property(expressionBase, "RowId"), Expression.Constant(modelId,typeof(Nullable<Int64>))));
            }
            IQueryable queryable = setCls.AsQueryable();
            Expression ex = Expression.And(expressions[0], expressions[1]);
            System.Linq.Expressions.Expression  whereCallExpression = System.Linq.Expressions.Expression.Call(
                   typeof(Queryable),
                   "Where",
                   new Type[] { queryable.ElementType },
                   queryable.Expression,
                   System.Linq.Expressions.Expression.Lambda(ex, expressionBase));
            queryable = queryable.Provider.CreateQuery(whereCallExpression);
            bool hasCls = false;
            PropertyInfo propClsId = setCls.ElementType.GetProperty("Id");
            foreach (Object item in queryable)
            {
                this.ClsId = (Int64)propClsId.GetValue(item);
                hasCls = true;
                break;
            }
            if (!hasCls)
            {
                Object cls = Activator.CreateInstance(setCls.ElementType);
                PropertyInfo propClsTableName = setCls.ElementType.GetProperty("TableName");
                PropertyInfo propRowId = setCls.ElementType.GetProperty("RowId");
                propClsTableName.SetValue(cls, EntityTableInfo.TableName);
                if (entityModel != null)
                { propRowId.SetValue(cls, modelId); }
                else
                { propRowId.SetValue(cls, null); }
                setCls.Add(cls);
                this.DataService.DataContext.SaveChanges();
                this.ClsId = (Int64)propClsId.GetValue(cls);
            }
            Console.WriteLine("Identifiant vclasseur " + this.ClsId);

            foreach (DocumentCodeViewModel codeVm in this.Codes)
            { this.LoadDocument(codeVm); }

            if (this.Codes.Count > 0)
            { this.SelectedCode = this.Codes.First(); }
            this.DropCommand = new DelegateCommand<System.Windows.DragEventArgs>(DropExecute);
            this.RemoveDocumentCommand = new DelegateCommand<DocumentViewModel>(RemoveDocumentExecute);
            this.SetDefaultDocumentCommand = new DelegateCommand<DocumentViewModel>(SetDefaultDocumentExecute);
            this.OpenDocumentCommand = new DelegateCommand<Object>(OpenDocumentExecute);
        }
Ejemplo n.º 2
0
        private void LoadDocument(DocumentCodeViewModel codeVm)
        {
            codeVm.Documents.Clear();
            String camelSchemaName = EntityTableInfo.SchemaInfo.SchemaName.Substring(0, 1).ToUpper() + EntityTableInfo.SchemaInfo.SchemaName.Substring(1).ToLower();
            DbSet setDocCls = this.DataService.GetDbSet(camelSchemaName + "DocCls");
            ParameterExpression expressionBase = System.Linq.Expressions.Expression.Parameter(setDocCls.ElementType, "item");

            Expression exp = Expression.And(
            Expression.Equal(Expression.Property(expressionBase, "InfClsId"), Expression.Constant(this.ClsId)),
            Expression.Equal(Expression.Property(Expression.Property(expressionBase, "InfDoc"), "InfCodeDocId"), Expression.Constant(codeVm.Id))
            );

            IQueryable queryable = setDocCls.AsQueryable();

            System.Linq.Expressions.Expression whereCallExpression = System.Linq.Expressions.Expression.Call(
                   typeof(Queryable),
                   "Where",
                   new Type[] { queryable.ElementType },
                   queryable.Expression,
                   System.Linq.Expressions.Expression.Lambda(exp, expressionBase));
            queryable = queryable.Provider.CreateQuery(whereCallExpression);
            PropertyInfo propDefaut = setDocCls.ElementType.GetProperty("Defaut");

            PropertyInfo propDoc = setDocCls.ElementType.GetProperty("InfDoc");
            PropertyInfo propDocId = propDoc.PropertyType.GetProperty("Id");
            PropertyInfo propDocFileName = propDoc.PropertyType.GetProperty("Filename");
            PropertyInfo propDocLibelle = propDoc.PropertyType.GetProperty("Libelle");
            foreach (Object item in queryable)
            {
                Object objDoc = propDoc.GetValue(item);
                DocumentViewModel documentViewModel = new DocumentViewModel();

                Boolean isDefault =(Boolean ) propDefaut.GetValue(item);
                String fileName = propDocFileName.GetValue (objDoc).ToString ();
                string libelle = propDocLibelle.GetValue (objDoc ).ToString ();
                String path = Path.Combine(codeVm.AbsolutePath, fileName);
                documentViewModel.Code = codeVm;
                documentViewModel.IsDefault = isDefault;
                documentViewModel.AbsoluteFileName = path;
                documentViewModel.Id = (Int64)propDocId.GetValue(objDoc);
                documentViewModel.Libelle = libelle;
                codeVm.Documents.Add(documentViewModel);
            }
        }