public FieldTemplate(Field field, Template template, int orderlist, int orderedit)
 {
     _field = field;
     _template = template;
     _orderlist = orderlist;
     _orderedit = orderedit;
 }
    /*private ArrayList GetModelsListFromFormattedString(string sourceType, string sourceString)
    {
        if (sourceString == null)
        {
            return null;
        }
        ArrayList list = new ArrayList ();
        string[] Ids = sourceString.TrimEnd(Constants.SEPARATOR).Split(Constants.SEPARATOR);
        foreach (string id in Ids)
        {
            System.Type type = System.Type.GetType(sourceType);
            object model = ActiveRecordMediator.FindByPrimaryKey(type, int.Parse(id));
            list.Add(model);
        }
        return list;
    }*/
    
    /*private ArrayList GetFieldsFromFormattedString(string sourceString)
    {
    	  return GetModelsListFromFormattedString("CastlePortal.Field", sourceString);
    }*/
    
    /*private ArrayList GetAclsFromFormattedString(string sourceString)
    {
        return GetModelsListFromFormattedString("CastlePortal.Acl", sourceString);
    }*/

    private void AddFieldsTemplates(Template template, string edit, string list)
    {

        foreach (FieldTemplate ft in template.Fields)
           ft.Delete();
        template.ListingVisibleFields = new ArrayList();
        template.Fields = new ArrayList();
        if (edit != null)
        {
            string[] options = edit.TrimEnd(':').Split(':');
            string[] ol;
            if (list != null)
                ol = list.TrimEnd(':').Split(':');
            else
                ol = edit.TrimEnd(':').Split(':');
            int i = 1;
            int j = 1;
            // delete current data 
            foreach (string option in options)
            {
                if (option.Length > 0)
                {
                    Field field = Field.Find(int.Parse(option));
                    FieldTemplate fieldTemplate;
                    // if field is within main list
                    if ((Array.BinarySearch(ol, option) >= 0) && (Array.BinarySearch(ol, option) <= options.Length))
                    {
                        fieldTemplate = new FieldTemplate(field, template, i++, j++);
                        fieldTemplate.Save();
                        template.Fields.Add(fieldTemplate);
                        template.ListingVisibleFields.Add(fieldTemplate);
                    }
                    else
                    {
                        fieldTemplate = new FieldTemplate(field, template, -1, j++);
                        fieldTemplate.Save();
                        template.Fields.Add(fieldTemplate);
                    }
                }
            }
        }
    }
    public void SaveTemplate(int id, string name, string description, 
            string tlist, string tview, string tedit, string sFields, string sFieldsEdit, bool tpub)
    {
        Commons.CheckSuperUser(Session);
        Template template;
        if (id != 0)
            template = Template.Find (id);
        else
            template = new Template();

        template.Name = name;
        template.Description = description;
        template.Public = tpub;

        string filelist = Constants.DEFAULT_LIST_TEMPLATE;
        string fileview = Constants.DEFAULT_VIEW_TEMPLATE;
        string fileedit = Constants.DEFAULT_EDIT_TEMPLATE;

        if (System.IO.File.Exists(Constants.TEMPLATES_FOLDER + tlist))
        {
            filelist = tlist;
        }
        if (System.IO.File.Exists(Constants.TEMPLATES_FOLDER + tview))
        {
            fileview = tview;
        }
        if (System.IO.File.Exists(Constants.TEMPLATES_FOLDER + tedit))
        {
            fileedit = tedit;
        }

        template.TList = filelist;
        template.TView = fileview;
        template.TEdit = fileedit;
        template.Fields = new ArrayList();

        template.Save();
        AddFieldsTemplates(template, sFieldsEdit, sFields);
        template.Save();

        RedirectToAction(Constants.TEMPLATES);
    }
    private static string ReadTemplate(XmlNode node, string rootpath)
    {
        if (node.Name == "Template")
        {
            string name = NodeGetString(node, "nombre");
            string description = NodeGetString(node, "nombrelargo");
            string path = NodeGetString(node, "fichero");
            string isPublic = NodeGetString(node, "public");

            string filelist = Constants.DEFAULT_LIST_TEMPLATE;
            string fileview = Constants.DEFAULT_VIEW_TEMPLATE;
            string fileedit = Constants.DEFAULT_EDIT_TEMPLATE;
            string prepath = rootpath + Constants.TEMPLATES_FOLDER + path;

            if (System.IO.File.Exists(prepath + ".vm"))
                filelist = path + ".vm";
            if (System.IO.File.Exists(prepath + "_list.vm"))
                filelist = path + "_list.vm";
            if (System.IO.File.Exists(prepath + "_edit.vm"))
                fileedit = path + "_edit.vm";
            if (System.IO.File.Exists(prepath + "_view.vm"))
                fileview = path + "_view.vm";

            Template t = new Template(name, description, filelist, fileview, fileedit);
            if (isPublic == "true")
            {
                t.Public = true;
            }
            else
            {
                t.Public = false;
            }
            /* FIXME: Estropeado porque antes era field y ahora es fieldtemplate
            t.FieldEdit = listaedit;
            */
            t.Save();

            int i = 1; // orden para la edicion
            int j = 1; // orden para el listado
            foreach (XmlNode field in node.ChildNodes)
            if (field.Name == "Field")
            {
                Field f = (Field) NodeGetObject(field, "nombre", fields);
                string show = NodeGetString(field, "show");

                FieldTemplate ft;
                if (show == "0")
                    ft = new FieldTemplate(f, t, -1, i++);
                else
                    ft = new FieldTemplate(f, t, j++, i++);
                ft.Save();
            }

            System.Console.WriteLine ("Template: "+ name);
            templates[name] = t;
        }
        return null;
    }