package com.dbxml.db.enterprise.sync;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: Group.java,v 1.2 2004/02/12 00:17:57 bradford Exp $
 */
import com.dbxml.xml.XMLSerializable;
import com.dbxml.xml.dom.DOMUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.util.Set;
import java.util.TreeSet;

/**
 * Group represents a dbXML Content grouping
 */

public final class Group implements XMLSerializable {
   private Set collections = Collections.synchronizedSet(new TreeSet());
   private List notes = Collections.synchronizedList(new ArrayList());

   private String name = "";
   private int status = Constants.STATUS_ACTIVE;
   private double version = 1.0;

   public Group() {
   }

   public Group(String name) {
      this.name = name;
   }

   public Element streamToXML(Document doc) throws DOMException {
      Element groupElem = doc.createElement(Constants.GROUP);

      groupElem.setAttribute(Constants.NAME, name);
      groupElem.setAttribute(Constants.STATUS, Constants.getStatusString(status));
      groupElem.setAttribute(Constants.VERSION, Double.toString(version));

      Element colsElem = DOMUtils.addElement(groupElem, Constants.COLLECTIONS);
      Element notesElem = DOMUtils.addElement(groupElem, Constants.NOTES);

      DOMUtils.setToElements(colsElem, Constants.COLLECTION, collections);
      DOMUtils.listToElements(notesElem, Constants.NOTE, notes);

      return groupElem;
   }

   public void streamFromXML(Element element) throws DOMException {
      name = element.getAttribute(Constants.NAME);
      status = Constants.getStatusValue(element.getAttribute(Constants.STATUS));
      version = Double.parseDouble(element.getAttribute(Constants.VERSION));

      DOMUtils.elementsToSet(element, Constants.COLLECTION, collections);
      DOMUtils.elementsToList(element, Constants.NOTE, notes);
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public int getStatus() {
      return status;
   }

   public void setStatus(int status) {
      this.status = status;
   }

   public double getVersion() {
      return version;
   }

   public void setVersion(double version) {
      this.version = version;
   }

   public Set getCollections() {
      return collections;
   }

   public List getNotes() {
      return notes;
   }
}
