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: Content.java,v 1.2 2004/02/12 00:17:57 bradford Exp $
 */

import com.dbxml.labrador.types.Variant;
import com.dbxml.xml.XMLSerializable;
import com.dbxml.xml.dom.DOMUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
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;

/**
 * Content represents metadata about a piece of dbXML content,
 * such as a Document or a binary image.
 */

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

   private int status = Constants.STATUS_ACTIVE;
   private String path = "";
   private String owner = "";
   private Date modified = new Date();
   private Date created = new Date();
   private long size = 0;
   private double version = 1.0;

   public Content() {
   }

   public Content(String path) {
      this.path = path;
   }

   public Element streamToXML(Document doc) throws DOMException {
      Element contentElem = doc.createElement(Constants.CONTENT);
      contentElem.setAttribute(Constants.STATUS, Constants.getStatusString(status));

      Element pathElem = DOMUtils.addElement(contentElem, Constants.PATH);
      Element ownerElem = DOMUtils.addElement(contentElem, Constants.OWNER);
      Element modifiedElem = DOMUtils.addElement(contentElem, Constants.MODIFIED);
      Element createdElem = DOMUtils.addElement(contentElem, Constants.CREATED);
      Element sizeElem = DOMUtils.addElement(contentElem, Constants.SIZE);
      Element versionElem = DOMUtils.addElement(contentElem, Constants.VERSION);
      Element groupsElem = DOMUtils.addElement(contentElem, Constants.GROUPS);
      Element tagsElem = DOMUtils.addElement(contentElem, Constants.TAGS);
      Element notesElem = DOMUtils.addElement(contentElem, Constants.NOTES);

      DOMUtils.setText(pathElem, path);
      DOMUtils.setText(ownerElem, owner);
      DOMUtils.setText(modifiedElem, new Variant(modified).toString());
      DOMUtils.setText(createdElem, new Variant(created).toString());
      DOMUtils.setText(sizeElem, Long.toString(size));
      DOMUtils.setText(versionElem, Double.toString(version));

      DOMUtils.setToElements(groupsElem, Constants.GROUP, groups);
      DOMUtils.setToElements(tagsElem, Constants.TAG, tags);
      DOMUtils.listToElements(notesElem, Constants.NOTE, notes);

      return contentElem;
   }


   public void streamFromXML(Element element) throws DOMException {
      status = Constants.getStatusValue(element.getAttribute(Constants.STATUS));

      path = DOMUtils.getElementText(element, Constants.PATH);
      owner = DOMUtils.getElementText(element, owner);
      modified = new Variant(DOMUtils.getElementText(element, Constants.MODIFIED)).getDate();
      created = new Variant(DOMUtils.getElementText(element, Constants.CREATED)).getDate();
      size = Long.parseLong(DOMUtils.getElementText(element, Constants.SIZE));
      version = Double.parseDouble(DOMUtils.getElementText(element, Constants.VERSION));

      DOMUtils.elementsToSet(element, Constants.GROUP, groups);
      DOMUtils.elementsToSet(element, Constants.TAG, tags);
      DOMUtils.elementsToList(element, Constants.NOTE, notes);
   }

   public String getPath() {
      return path;
   }

   public void setPath(String path) {
      this.path = path;
   }

   public int getStatus() {
      return status;
   }

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

   public String getOwner() {
      return owner;
   }

   public void setOwner(String owner) {
      this.owner = owner;
   }

   public Date getModified() {
      return modified;
   }

   public void setModified(Date modified) {
      this.modified = modified;
   }

   public Date getCreated() {
      return created;
   }

   public void setCreated(Date created) {
      this.created = created;
   }

   public long getSize() {
      return size;
   }

   public void setSize(long size) {
      this.size = size;
   }

   public double getVersion() {
      return version;
   }

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

   public Set getGroups() {
      return groups;
   }

   public Set getTags() {
      return tags;
   }

   public List getNotes() {
      return notes;
   }
}
