Click here to Skip to main content
15,895,606 members
Articles / Web Development / ASP.NET

Using Silverlight in Enterprise: RAD of User Friendly Database Access

Rate me:
Please Sign up or sign in to vote.
4.81/5 (19 votes)
31 Jul 2009CPOL8 min read 58.2K   7K   80  
This article introduces FulcrumWeb RAD Framework - A Silverlight UI Engine to build user friendly database driven applications
/********************************************************************
 *  FulcrumWeb RAD Framework - Fulcrum of your business             *
 *  Copyright (c) 2002-2009 FulcrumWeb, ALL RIGHTS RESERVED         *
 *                                                                  *
 *  THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      *
 *  FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        *
 *  COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       *
 *  AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  *
 *  AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE    *
 *  AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS.           *
 ********************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Framework.Db;
using Framework.Entity;
using Framework.Entity.Filter;
using Framework.Metadata;
using Framework.Utils;

namespace Framework.Remote
{
  public partial class CxAppServer
  {
    /// <summary>
    /// Returns list of entities.
    /// </summary>
    /// <param name="marker">Server operation identifier.</param>
    /// <param name="prms">Query parameters.</param>
    /// <returns>Initialized CxModel</returns>
    public CxModel GetEntityList(Guid marker, CxQueryParams prms)
    {
      try
      {
        CxEntityUsageMetadata entityUsage = m_Holder.EntityUsages[prms.EntityUsageId];

        CxModel model = new CxModel(marker);
        using (CxDbConnection connection = CxDbConnections.CreateEntityConnection())
        {

          IList<string> whereParamsMames =
              CxDbParamParser.GetList(entityUsage.WhereClause, true);

          // Obtaining the parent entity.
          CxBaseEntity parent = null;
          if (prms.ParentEntityUsageId != null)
          {
            CxEntityUsageMetadata parentEntityUsage = m_Holder.EntityUsages[prms.ParentEntityUsageId];
            IList<string> parentPkNames = parentEntityUsage.PrimaryKeyIds;
            IxValueProvider parentVlProvider =
              CxQueryParams.CreateValueProvider(prms.ParentPks);
            parent = CxBaseEntity.CreateAndReadFromDb
              (parentEntityUsage,
               connection,
               parentVlProvider);
          }

          IxValueProvider paramsProvider =
              CxValueProviderCollection.Create(
                parent,
                CxQueryParams.CreateValueProvider(prms.WhereValues),
                m_Holder.ApplicationValueProvider);

          foreach (CxFilterItem filterItem in prms.FilterItems)
          {
            filterItem.Operation = (NxFilterOperation)Enum.Parse(typeof(NxFilterOperation), filterItem.OperationAsString);
            CxFilterOperator filterOperator
                = CxFilterOperator.Create(entityUsage, filterItem);
            if (filterOperator != null)
            {
              filterOperator.InitializeValueProvider(paramsProvider);
            }

          }
          string filterCondition = GetFilterCondition(entityUsage, prms.FilterItems);

          // Indicates whether we should perform an additional query to the 
          // database to obtain the overall amount of records.
          bool doPerformQueryForCount = entityUsage.IsPagingEnabled && (prms.StartRecordIndex >= 1 || prms.RecordsAmount != -1);

          int totalEntityAmount = -1;
          if (doPerformQueryForCount)
          {
            totalEntityAmount = CxBaseEntity.ReadEntityAmount(
              connection, entityUsage, filterCondition, paramsProvider);
          }

          // Composing the list of sort descriptors from the input params.
          CxSortDescriptorList sortings = new CxSortDescriptorList();
          if (prms.SortDescriptions != null)
          {
            foreach (CxSortDescription sorting in prms.SortDescriptions)
            {
              sortings.Add(new CxSortDescriptor(sorting.AttributeId,
                sorting.Direction == NxListSortDirection.Ascending ? ListSortDirection.Ascending : ListSortDirection.Descending));
            }
          }

          // Query to the backend.
          CxBaseEntity[] entities = CxBaseEntity.ReadEntities(
            connection, entityUsage, filterCondition, paramsProvider,
            prms.StartRecordIndex, prms.RecordsAmount, sortings);

          if (!doPerformQueryForCount)
          {
            totalEntityAmount = entities.Length;
          }

          Dictionary<string, CxClientRowSource> unfilteredRowSources = new Dictionary<string, CxClientRowSource>();
          List<CxClientRowSource> filteredRowSources = new List<CxClientRowSource>();

          model = new CxModel
          {
            Marker = marker,
            EntityUsageId = entityUsage.Id,
            UnfilteredRowSources = unfilteredRowSources,
            FilteredRowSources = filteredRowSources,
            TotalDataRecordAmount = totalEntityAmount,
          };
          if (prms.SortDescriptions != null)
          {
            model.SortDescriptions = (new List<CxSortDescription>(prms.SortDescriptions)).ToArray();
          }
          model.SetData(entityUsage, entities, connection);
          UpdateRecentItems(connection, model);

        }

        InitApplicationValues(model.ApplicationValues);
        return model;
      }
      catch (Exception ex)
      {
        CxExceptionDetails exceptionDetails = new CxExceptionDetails(ex);
        CxModel model = new CxModel { Error = exceptionDetails };
        return model;
      }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions