1  //!-- UTF8
     2  /*
     3  Oregano Multiuser Server - Version 1.2.0 - January 4th, 2005
     4   
     5  	Web:  www.oregano-server.org
     6  	Mail: info@oregano-server.org
     7   
     8  	Copyright 2003 - 2004 - 2004 Jens Halm / Cologne, Germany
     9   
    10   This library is free software; you can redistribute it and/or
    11   modify it under the terms of the GNU Lesser General Public
    12   License as published by the Free Software Foundation; either
    13   version 2.1 of the License, or (at your option) any later version.
    14   
    15   This library is distributed in the hope that it will be useful,
    16   but WITHOUT ANY WARRANTY; without even the implied warranty of
    17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    18   Lesser General Public License for more details.
    19   
    20   You should have received a copy of the GNU Lesser General Public
    21   License along with this library; if not, write to the Free Software
    22   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23   */
    24  
    25  /*
    26  -------------------------------------------
    27  	Classe Property
    28  
    29  	@description :
    30  	Classe de base des propriétés persistantes.
    31  
    32  
    33  	@author Jens Halm copyright http://www.spicefactory.org/
    34  	@author erixtekila copyleft http://www.v-i-a.net  
    35  -------------------------------------------
    36  	version history :
    37  	1.2.0 : 17/01/05
    38  			- Portage en actionscript 2 pour le
    39  			compile time type checking
    40  -------------------------------------------
    41  */
    42  
    43  import org.omus.data.PropertySet;
    44  import org.omus.data.DataTransformer;
    45  import org.omus.util._Class;
    46  
    47  /**
    48   *	Classe de base des propriétés persistantes.
    49   *
    50   *	@author Jens Halm copyright http://www.spicefactory.org/
    51   *	@author erixtekila copyleft http://www.v-i-a.net 
    52   *	@version 1.2.0
    53   */
    54  class org.omus.data.Property
    55  {
    56  	//--------------------
    57  	// PROPRIETES
    58  	//--------------------
    59  	/**
    60  	 *	Nom.
    61  	 */
    62  	private var name:String;
    63  	
    64  	/**
    65  	 *	Type.
    66  	 */
    67  	private var type:String;
    68  	
    69  	/**
    70  	 *	Classe.
    71  	 */
    72  	private var clazz:Object;
    73  	
    74  	/**
    75  	 *	Cache.
    76  	 */
    77  	private var cache:Array;
    78  	
    79  	/**
    80  	 *	Un flag pour indiquer le changement.
    81  	 */
    82  	private var modified:Boolean;
    83  	
    84  	/**
    85  	 *	Valeur de la propriété.
    86  	 */
    87  	private var value:Object;
    88  	
    89  	/**
    90  	 *	Une référence au PropetSet. 
    91  	 */
    92  	private var propSet:PropertySet;
    93  	
    94  	/**
    95  	 *	TODO
    96  	 */
    97  	private var backup:String;
    98  	
    99  	/**
   100  	 *	Constante
   101  	 */
   102  	public static var OFF:Number = 0;
   103  	
   104  	/**
   105  	 *	Constante
   106  	 */
   107  	public static var SERVER_CACHE:Number = 1;
   108  	
   109  	/**
   110  	 *	Constante
   111  	 */
   112  	public static var SYNCHRONIZE_CLIENT:Number = 2;
   113  	
   114  	/**
   115  	 *	Constante
   116  	 */
   117  	public static var SYNCHRONIZE_GROUP:Number = 3;
   118  	
   119  	//--------------------
   120  	// CONSTRUCTEUR
   121  	//--------------------
   122  	/**
   123  	 *	TODO
   124  	 *
   125  	 *	@param nm			Nom de la propriété.
   126  	 *	@param tp			Type attaché à la propriété.
   127  	 *	@param cl			Classe.
   128  	 *	@param ch			Liste du cache.
   129  	 *	@param vl			Valeur de la propriété.	
   130  	 */
   131  	public function Property (nm:String, tp:String, cl:Object, ch:Array, vl:Object)
   132  	{
   133  		// Propriétés
   134  		name = nm;
   135  		type = tp;
   136  		clazz = cl;
   137  		cache = ch;
   138  		value = (typeof(vl) == "undefined") ? null : vl ;
   139  		modified = false;
   140  		propSet = null;
   141  		backup = null;
   142  		
   143  		// trace(this+ " installé.");
   144  	}
   145  	
   146  	//--------------------
   147  	// METHODES PUBLIQUES
   148  	//--------------------
   149  	/**
   150  	 *	Utilisé dans un contexte littéral
   151  	 *	@return	Une chaine définissant l'objet
   152  	 */
   153  	public function toString ():String
   154  	{
   155  		return format(0);
   156  	}
   157  	
   158  	/**
   159  	 *	Renvoie le set de propriétés.
   160  	 *
   161  	 *	@return		Une référence au groupe de propriétés.
   162  	 */
   163  	public function getPropertySet ():PropertySet
   164  	{
   165  		return propSet;
   166  	}
   167  	
   168  	/**
   169  	 *	Renvoie le nom.
   170  	 *
   171  	 *	@return		Un nom de propriété persistante.
   172  	 */
   173  	public function getName ():String
   174  	{
   175  		return name;
   176  	}
   177  
   178  	/**
   179  	 *	Renvoie la valeur de la propriété persistante.
   180  	 *
   181  	 *	@return		La valeur de la prorpiété.
   182  	 */
   183  	public function getValue ():Object
   184  	{
   185  		return value;
   186  	}
   187  
   188  	/**
   189  	 *	Renvoie le type de la propriété.
   190  	 *
   191  	 *	@return		Un type connu.
   192  	 */
   193  	public function getType ():Object
   194  	{
   195  		return type;
   196  	}
   197  	
   198  	/**
   199  	 *	Permet de savoir si la propriété a été modifiée.
   200  	 *
   201  	 *	@return		true si elle a été modifiée.
   202  	 */
   203  	public function isModified ():Boolean
   204  	{
   205  		return modified;
   206  	}
   207  	
   208  	/**
   209  	 *	Permet de savoir si la valeur de la propriété est chargée correctement.
   210  	 *
   211  	 *	@return		La valeur sinon null.
   212  	 */
   213  	public function isLoaded ():Object
   214  	{
   215  		return (value != null);
   216  	}
   217  	
   218  	/**
   219  	 *	Modifie la valeur d'une propriété.
   220  	 *	
   221  	 *	@param newVal		Une nouvelle valeur.
   222  	 */
   223  	public function setValue (newVal:Object):Void
   224  	{
   225  		//! TODO : instanceof
   226  		if (! _Class.isInstance (newVal, clazz))
   227  		{
   228  			// Logs internes
   229  			_global.oregano.iLog.error("clj-016", "property name = " + name + " - value = " + newVal);
   230  			return;
   231  		}
   232  		modified = true;
   233  		// TODO : Accès Singleton
   234  		var transformer = DataTransformer.getInstance();
   235  		// Change la valeur à travers PropertySet
   236  		propSet.valueChanged(name, transformer["marshal_" + type](newVal));
   237  	}
   238  	
   239  	/**
   240  	 *	Appelé par un objet Table non synchronisé.
   241  	 *	called by table objects that are not synchronized
   242  	 *
   243  	 *	@see org.omus.data.Table
   244  	 */
   245  	public function setModified ():Void
   246  	{
   247  		modified = true;
   248  		// TODO : Accès Singleton
   249  		var transformer = DataTransformer.getInstance();
   250  		// Change la valeur à travers PropertySet
   251  		//!! TODO : Syntaxe !! propSet.valueChanged(name, _global.org.omus.transformer["marshal_" + type](newVal));
   252  		propSet.valueChanged(name, transformer["marshal_" + type](value));
   253  	}
   254  	
   255  	/**
   256  	 *	Modifie une valeur de propriété avant synchronisation.
   257  	 *
   258  	 *	@param newVal		Nouvelle valeur.
   259  	 */
   260  	public function loadTemp (newVal:Object):Void
   261  	{
   262  		value = newVal;
   263  	}
   264  	
   265  	/**
   266  	 *	Met à jour la propriétés après synchronisation.
   267  	 *
   268  	 *	@param newVal		Nouvelle valeur.
   269  	 *	@param clientReq	true si la demande provient du poste client.
   270  	 */
   271  	public function update (newVal:Object, clientReq:Boolean):Void
   272  	{
   273  		value = newVal;
   274  		// TODO : private member. if (! propSet.marsh.contains(name)) modified = false;
   275  		if (! (propSet.getMarshalledProperties()).contains(name)) modified = false;
   276  	}
   277  	
   278  	/**
   279  	 *	Vide le contenu de la propriété.
   280  	 */
   281  	public function unload ():Void
   282  	{
   283  		value = null;
   284  	}
   285  	
   286  	/**
   287  	 *	Annule la modification d'un propriété.
   288  	 *	Utile lors d'un échec de synchronisation.
   289  	 */
   290  	public function reset ():Void
   291  	{
   292  		if (! (propSet.getMarshalledProperties()).contains(name)) modified = false;
   293  	}
   294  	
   295  	/**
   296  	 *	TODO
   297  	 *	called by PropertySet#addProperty
   298  	 *
   299  	 *	@param ps		Référence au jeu de propriétés.
   300  	 *	@see org.omus.data.PropertySet#addProperty
   301  	 */
   302  	public function setPropertySet (ps:PropertySet):Void
   303  	{
   304  		propSet = ps;
   305  	}
   306  	
   307  	//--------------------
   308  	// METHODES PRIVEES
   309  	//--------------------
   310  	/**
   311  	 *	Formate une chaîne de représenation de l'objet.
   312  	 *
   313  	 *	@param indent		Numbre d'espace d'indentation.
   314  	 *	@return				Une chaîne formatée.
   315  	 */
   316  	private function format (indent:Number):String
   317  	{
   318  		var s= "org.omus.data.Property:";
   319  		var arr = ["name", "type", "modified"];
   320  		var long = arr.length;
   321  		for (var i = 0; i < long ; i++) 
   322  		{
   323  			s += "\n";
   324  			for (var idx = 0; idx < indent; idx++) s += "  ";
   325  			s += arr[i] + " = " + this[arr[i]];
   326  		}
   327  		s += "\n";
   328  		for (var idx = 0; idx < indent; idx++) s += "  ";
   329  		s += "value = ";
   330  		if (value == null)
   331  		{
   332  			s += "[not loaded]";
   333  		} else
   334  		{
   335  			s += org.omus.util.Log.format(value, indent + 1);
   336  		}
   337  		return s;
   338  	}
   339  	
   340  	//--------------------
   341  	// METHODES STATIQUES
   342  	//--------------------
   343  	/**
   344  	 *	Utilisé dans un contexte littéral
   345  	 *	@return	Une chaine définissant l'objet
   346  	 */
   347  	public static function toLog():String
   348  	{
   349  		return "[Object Property]";
   350  	}
   351  }
   352