/// <summary>
 /// Retrieves objects of a specified type by an foreign-key column, with ordering specified by another column
 /// </summary>
 /// <typeparam name="T">The type of the objects to be retrieved.</typeparam>
 /// <param name="session">The currently active persistence session.</param>
 /// <param name="key">The name of the key property/field.</param>
 /// <param name="value">The value that the named property must hold.</param>
 /// <param name="sortKey">The property/field by which to sort</param>
 /// <param name="dir">The direction of the sorting</param>
 /// <returns>A list of all objects meeting the specified criteria.</returns>
 public static IList <T> RetrieveByForeignKey <T>(MPRSession session, string key, Int64 value, string sortKey, Direction dir)
 {
     return(session.Session.CreateCriteria(typeof(T))
            .Add(Expression.Eq(key + ".Id", value))
            .AddOrder(dir == Direction.Ascending ? Order.Asc(sortKey) : Order.Desc(sortKey))
            .List <T>());
 }
 /// <summary>
 /// Deletes objects of a specified type.
 /// </summary>
 /// <param name="session">The currently active persistence session.</param>
 /// <param name="itemsToDelete">The items to delete.</param>
 /// <typeparam name="T">The type of objects to delete.</typeparam>
 public static void Delete <T>(MPRSession session, IList <T> itemsToDelete)
 {
     foreach (T item in itemsToDelete)
     {
         session.Session.Delete(item);
     }
 }
        public static MPRSession StartSession()
        {
            MPRSession session = new MPRSession();

            session.Session     = DatabaseHelper.GetCurrentSession();
            session.Transaction = session.Session.BeginTransaction();
            return(session);
        }
 public static void EndSession(MPRSession session, bool commit)
 {
     if ((session != null) && (session.Transaction != null))
     {
         if (commit)
         {
             session.Transaction.Commit();
         }
         else
         {
             session.Transaction.Rollback();
         }
         session.Transaction = null;
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T">The type of the objects to be retrieved. Must implement DisplayObject</typeparam>
        /// <param name="orderField"></param>
        /// <param name="direction"></param>
        /// <returns></returns>
        public static IList <T> RetrieveAll <T>(MPRSession session, string orderField, SortDirection direction)
            where T : IDisplayObject, new()
        {
            // Get the query string of type T.
            // TODO: I would be really happy to find a way to do that without an instance
            T      displayObject = new T();
            string sql           = ((IDisplayObject)displayObject).QueryStringSelect;

            if (orderField != null)
            {
                sql += " order by " + orderField + (direction == SortDirection.Ascending ? " asc " : " desc ");
            }
            IList <T> items = session.Session.CreateSQLQuery(sql)
                              .SetResultTransformer(Transformers.AliasToBean(typeof(T)))
                              .List <T>();

            return(items);
        }
        public static ISet <MPTag> GetTags(MPRSession session, string tagNameList)
        {
            string[]      tagNames = tagNameList.Split(',');
            IList <MPTag> tags     = session.Session.CreateCriteria(typeof(MPTag))
                                     .Add(Expression.In("Name", tagNames))
                                     .List <MPTag>();
            Set <string> tagNamesExisting = new HashedSet <string>();

            foreach (MPTag tag in tags)
            {
                tagNamesExisting.Add(tag.Name);
            }
            ISet <MPTag> results = new HashedSet <MPTag>(tags);

            foreach (string tagName in (new HashedSet <string>(tagNames)).Minus(tagNamesExisting))
            {
                MPTag tag = new MPTag {
                    Name = tagName
                };
                session.Session.Save(tag);
                results.Add(tag);
            }
            return(results);
        }
    protected void FillCategoriesForSelectedItemType(MPRSession session)
    {
      if (typesList.SelectedValue == null)
      {
        return;
      }

      // Remove the previous categories
      categoriesList.Items.Clear();

      // Fill the categories for the selected type
      Int64 typeId;
      Int64.TryParse(typesList.SelectedValue, out typeId);
      IList<MPCategory> categories = MPRController.RetrieveByForeignKey<MPCategory>(session, "Type", typeId);

      foreach (MPCategory category in categories)
      {
        categoriesList.Items.Add(new ListItem(category.Name, category.Id.ToString()));
      }

    }
 public static IList <T> RetrieveAll <T>(MPRSession session)
     where T : IDisplayObject, new()
 {
     return(RetrieveAll <T>(session, null, SortDirection.Ascending));
 }
 /// <summary>
 /// Deletes an object of a specified type.
 /// </summary>
 /// <param name="session">The currently active persistence session.</param>
 /// <param name="itemsToDelete">The items to delete.</param>
 /// <typeparam name="T">The type of objects to delete.</typeparam>
 public static void Delete <T>(MPRSession session, T item)
 {
     session.Session.Delete(item);
 }
 public static void Save <T>(MPRSession session, T item, out Int64 newId)
 {
     newId = (Int64)session.Session.Save(item);
 }
 /// <summary>
 /// Saves an object and its persistent children.
 /// </summary>
 public static void Save <T>(MPRSession session, T item)
 {
     session.Session.SaveOrUpdate(item);
 }
 public static IList <T> RetrieveByIdList <T>(MPRSession session, List <Int64> ids)
 {
     return(session.Session.CreateCriteria(typeof(T))
            .Add(Expression.In("Id", ids))
            .List <T>());
 }
 /// <summary>
 /// Retrieves object of a specified type by its Id field
 /// </summary>
 /// <typeparam name="T">The type of the objects to be retrieved.</typeparam>
 /// <param name="session">The currently active persistence session.</param>
 /// <param name="id">The id of the object.</param>
 /// <returns>The object or null if not found</returns>
 public static T RetrieveById <T>(MPRSession session, Int64 id)
 {
     return(session.Session.Get <T>(id));
 }
 /// <summary>
 /// Retrieves objects of a specified type by an foreign-key column
 /// </summary>
 /// <typeparam name="T">The type of the objects to be retrieved.</typeparam>
 /// <param name="session">The currently active persistence session.</param>
 /// <param name="key">The name of the column to be tested.</param>
 /// <param name="value">The value that the named property must hold.</param>
 /// <returns>A list of all objects meeting the specified criteria.</returns>
 public static IList <T> RetrieveByForeignKey <T>(MPRSession session, string key, Int64 value)
 {
     return(session.Session.CreateCriteria(typeof(T))
            .Add(Expression.Eq(key + ".Id", value))
            .List <T>());
 }
 /// <summary>
 /// Retrieves objects of a specified type where a specified property equals a specified value.
 /// </summary>
 /// <typeparam name="T">The type of the objects to be retrieved.</typeparam>
 /// <param name="session">The currently active persistence session.</param>
 /// <param name="propertyName">The name of the property to be tested.</param>
 /// <param name="propertyValue">The value that the named property must hold.</param>
 /// <returns>A list of all objects meeting the specified criteria.</returns>
 public static IList <T> RetrieveEquals <T>(MPRSession session, string propertyName, object propertyValue)
 {
     return(session.Session.CreateCriteria(typeof(T))
            .Add(Expression.Eq(propertyName, propertyValue))
            .List <T>());
 }
 /// <summary>
 /// Retrieves all objects of a given type.
 /// </summary>
 /// <param name="session">The currently active persistence session.</param>
 /// <typeparam name="T">The type of the objects to be retrieved.</typeparam>
 /// <returns>A list of all objects of the specified type.</returns>
 public static IList <T> RetrieveAll <T>(MPRSession session)
 {
     // Retrieve all objects of the type passed in
     return(session.Session.CreateCriteria(typeof(T))
            .List <T>());
 }