/********************************************************************
* 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 Framework.Db;
using Framework.Utils;
namespace Framework.Metadata
{
//---------------------------------------------------------------------------
/// <summary>
/// Represents a shortened description of the attribute metadata.
/// </summary>
public class CxAttributeCustomizer : CxCustomizerBase, IxStorableInIdOrder
{
//-------------------------------------------------------------------------
private CxEntityUsageCustomizer m_ParentCustomizer;
private CxAttributeCustomizerData m_CurrentData;
private CxAttributeCustomizerData m_InitialData;
private string m_Id;
private CxAttributeMetadata m_Metadata;
//-------------------------------------------------------------------------
/// <summary>
/// The metadata object the customizer belongs to.
/// </summary>
public CxAttributeMetadata Metadata
{
get { return m_Metadata ?? ParentCustomizer.Metadata.GetAttribute(Id); }
set { m_Metadata = value; }
}
//-------------------------------------------------------------------------
/// <summary>
/// Current data snapshot, non-saved data.
/// </summary>
public CxAttributeCustomizerData CurrentData
{
get { return m_CurrentData; }
set { m_CurrentData = value; }
}
//-------------------------------------------------------------------------
/// <summary>
/// Initial data snapshot, the data which is actually applied to the application.
/// </summary>
public CxAttributeCustomizerData InitialData
{
get { return m_InitialData; }
set { m_InitialData = value; }
}
//-------------------------------------------------------------------------
/// <summary>
/// Logical parent customizer object.
/// </summary>
public CxEntityUsageCustomizer ParentCustomizer
{
get { return m_ParentCustomizer; }
set { m_ParentCustomizer = value; }
}
//-------------------------------------------------------------------------
/// <summary>
/// Identifier of the customizer.
/// </summary>
public string Id
{
get { return m_Id; }
set { m_Id = value; }
}
//-------------------------------------------------------------------------
public CxAttributeCustomizer(
CxEntityUsageCustomizer parentCustomizer, string attributeId)
{
Id = attributeId;
ParentCustomizer = parentCustomizer;
Context = ParentCustomizer.Context;
Manager = ParentCustomizer.Manager;
CurrentData = new CxAttributeCustomizerData(this);
InitialData = CurrentData.Clone();
}
//-------------------------------------------------------------------------
/// <summary>
/// Indicates whether the customizer has been modified somehow.
/// </summary>
/// <returns>true if modified, otherwise false</returns>
public bool GetIsModified()
{
return !CurrentData.Compare(InitialData);
}
//-------------------------------------------------------------------------
/// <summary>
/// Applies the customizer state to the metadata it belongs to.
/// </summary>
/// <returns>true if changes took place</returns>
public override bool ApplyToMetadata()
{
bool isChanged = base.ApplyToMetadata();
if (!CxText.Equals(Metadata.WinControlPlacement, CurrentData.WinControlPlacement) ||
Metadata.FilterAdvanced != CurrentData.IsShownOnAdvancedFilterPanel ||
Metadata.Nullable == CurrentData.IsRequired ||
!CxText.Equals(Metadata.Default, CurrentData.DefaultValue) ||
!CxText.Equals(Metadata.RowSourceId, CurrentData.RowSourceId))
{
if (!ParentCustomizer.Metadata.IsAttributeDefined(Metadata.Id))
{
Metadata = Metadata.ApplyToEntityUsage(ParentCustomizer.Metadata);
}
Metadata.WinControlPlacement = CurrentData.WinControlPlacement;
Metadata.FilterAdvanced = CurrentData.IsShownOnAdvancedFilterPanel;
Metadata.Nullable = !CurrentData.IsRequired;
Metadata.Default = CurrentData.DefaultValue;
Metadata.RowSourceId = CurrentData.RowSourceId;
isChanged |= true;
}
return isChanged;
}
//-------------------------------------------------------------------------
/// <summary>
/// Saves the changes done to the customizer into the database store.
/// </summary>
/// <param name="connection">connection to be used</param>
public void Save(CxDbConnection connection)
{
if (GetIsModified())
{
// Not quite an effective way, but quite simple.
// We store language caption in the same method.
SaveLanguageCaptions(connection);
InitialData = CurrentData.Clone();
}
}
//-------------------------------------------------------------------------
/// <summary>
/// Saves the language captions into the multilanguage subsystem.
/// </summary>
/// <param name="connection">connection to be used</param>
protected void SaveLanguageCaptions(CxDbConnection connection)
{
CxMultilanguage multilanguage = Metadata.Holder.Multilanguage;
if (multilanguage != null)
{
foreach (KeyValuePair<string, string> langCaption in CurrentData.LanguageCaptionMap)
{
string localizationObjectName = ParentCustomizer.Metadata.Id + "." + Metadata.Id;
multilanguage.SetLocalizedValue(
connection,
langCaption.Key,
CxAttributeUsageMetadata.OBJECT_TYPE_ATTRIBUTE_USAGE,
"caption",
localizationObjectName,
langCaption.Value);
}
}
}
//-------------------------------------------------------------------------
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
return !string.IsNullOrEmpty(Id) ? Id : base.ToString();
}
//-------------------------------------------------------------------------
/// <summary>
/// Resets the customizer to the metadata object default values.
/// </summary>
public override void ResetToDefault()
{
base.ResetToDefault();
CurrentData.WinControlPlacement = Metadata.GetInitialProperty("win_control_placement", false);
CurrentData.IsShownOnAdvancedFilterPanel = CxBool.Parse(Metadata.GetInitialProperty("filter_advanced", false), false);
}
//-------------------------------------------------------------------------
}
}