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

import java.util.*;

import com.dbxml.db.common.adapters.DOMAdapter;
import com.dbxml.db.common.adapters.XMLSerializableAdapter;
import com.dbxml.db.common.xpath.XPathQueryResolver;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.FaultCodes;
import com.dbxml.db.core.SystemCollection;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.extension.SimpleExtension;
import com.dbxml.db.core.query.ResultSet;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.labrador.types.Variant;
import com.dbxml.xml.NamespaceMap;
import com.dbxml.xml.dom.DOMHelper;
import com.dbxml.xml.dom.DOMUtils;
import com.dbxml.xml.dtsm.DTSMException;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * SyncManager
 */

public final class SyncManager extends SimpleExtension {
   private static final String[] EmptyStrings = new String[0];
   private static final NamespaceMap EmptyNSMap = new NamespaceMap();
   private static final String SYNCID_XML = "syncid.xml";
   private static final String SYNC = "sync";
   private static final String ID = "id";
   private static final String GROUPS_COL = "groups";
   private static final String CONTENT_COL = "content";

   private String machineID;
   private XMLSerializableAdapter groupsCol;
   private XMLSerializableAdapter contentCol;

   public String getMachineID() {
      if ( machineID == null ) {
         Transaction tx = new Transaction();

         try {
            Collection sysCol = collection.getSystemCollection();
            Collection cfgCol = sysCol.getCollection(SystemCollection.CONFIGS);
				DOMAdapter domAdapter = new DOMAdapter(cfgCol);
				Document doc = domAdapter.getDocument(tx, SYNCID_XML);
            if ( doc != null ) {
               Element elem = doc.getDocumentElement();
               machineID = elem.getAttribute(ID);
            }
            else {
               machineID = cfgCol.createNewOID().toString();
               doc = DOMHelper.newDocument();
               Element elem = doc.createElement(SYNC);
               doc.appendChild(elem);
               elem.setAttribute(ID, machineID);
					domAdapter.setDocument(tx, SYNCID_XML, doc);
            }
         }
         catch ( Exception e ) {
            try {
               tx.cancel();
            }
            catch ( DBException ex ) {
               ex.printStackTrace(System.err);
            }

            // If this doesn't work, something is VERY wrong
            e.printStackTrace(System.err);
            machineID = collection.createNewOID().toString();
         }
			finally {
				if ( tx.getStatus() == Transaction.ACTIVE ) {
					try {
						 tx.commit();
					}
					catch ( DBException e ) {
						e.printStackTrace(System.err);
					}
				}
			}
      }
      return machineID;
   }

   public static final String[] PARAMS_addGroup = {"group"};

   public void addGroup(String group) throws DBException {
      Group g = getGroup(group, true);
      storeGroup(g);
   }

   public static final String[] PARAMS_removeGroup = {"group"};

   public void removeGroup(String group) throws DBException {
      Transaction tx = new Transaction();
      try {
         XMLSerializableAdapter col = getGroupsCollection();
         col.remove(tx, group);
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
		finally {
	   	if ( tx.getStatus() == Transaction.ACTIVE )
   	      tx.commit();
		}
   }

   public String[] listGroups() throws DBException {
      Transaction tx = new Transaction();
      try {
         XMLSerializableAdapter col = getGroupsCollection();
			Key[] keys = col.listKeys(tx);
			String[] result = new String[keys.length];
			for ( int i = 0; i < keys.length; i++ )
				result[i] = keys[i].toString();
         return result;
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
		finally {
	   	if ( tx.getStatus() == Transaction.ACTIVE )
   	      tx.commit();
		}
   }

   public static final String[] PARAMS_addCollection = {"group", "collection"};

   public void addCollection(String group, String collection) throws DBException {
      Group g = getGroup(group, true);
      Set s = g.getCollections();
      if ( !s.contains(collection) )
         s.add(collection);
      storeGroup(g);
   }

   public static final String[] PARAMS_removeCollection = {"group", "collection"};

   public void removeCollection(String group, String collection) throws DBException {
      Group g = getGroup(group, true);
      g.getCollections().remove(collection);
      storeGroup(g);
   }

   public static final String[] PARAMS_listCollections = {"group"};

   public String[] listCollections(String group) throws DBException {
      Group g = getGroup(group, true);
      return (String[])g.getCollections().toArray(EmptyStrings);
   }

   public static final String[] PARAMS_addGroupToContent = {"path", "group"};

   public void addGroupToContent(String path, String group) throws DBException {
      Content c = getContent(path, true);
      Set s = c.getGroups();
      if ( !s.contains(group) )
         s.add(group);
      storeContent(c);
   }

   public static final String[] PARAMS_removeGroupFromContent = {"path", "group"};

   public void removeGroupFromContent(String path, String group) throws DBException {
      Content c = getContent(path, true);
      Set s = c.getGroups();
      s.remove(group);
      storeContent(c);
   }

   public static final String[] PARAMS_listContentGroups = {"path" };

   public String[] listContentGroups(String path) throws DBException {
      Content c = getContent(path, true);
      return (String[])c.getGroups().toArray(EmptyStrings);
   }

   public static final String[] PARAMS_listGroupContents = {"group"};

   public String[] listGroupContents(String group) throws DBException {
      Transaction tx = new Transaction();
      try {
         XMLSerializableAdapter col = getContentCollection();
         ResultSet res = col.queryCollection(tx, XPathQueryResolver.STYLE_XPATH, "/content[@status='active']/groups/group[.='"+group+"']", EmptyNSMap);
         Set s = new TreeSet();
         while ( res.next() )
            s.add(res.getResultKey().toString());
			return (String[])s.toArray(EmptyStrings);
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
		finally {
	   	if ( tx.getStatus() == Transaction.ACTIVE )
   	      tx.commit();
		}
   }

   public static final String[] PARAMS_listGroupChanges = {"date", "groups"};

   public Document listGroupChanges(Date date, String[] groups) throws DBException {
      Transaction tx = new Transaction();

      try {
         Date start = new Date();

         Set grpSet = new HashSet();
         for ( int i = 0; groups != null && i < groups.length; i++ )
            grpSet.add(groups[i]);

         String d = new Variant(date).getString();
         StringBuffer sb = new StringBuffer(64);
         sb.append("/" + Constants.CONTENT + "[" + Constants.MODIFIED + " > '" + d + "'");
         if ( groups != null ) {
            sb.append(" and " + Constants.GROUPS + "/" + Constants.GROUP + "[");
            for ( int i = 0; i < groups.length; i++ ) {
               if ( i > 0 )
                  sb.append(" or ");
               sb.append(".='" + groups[i] + "'");
            }
            sb.append("]");
         }
         sb.append("]");

         XMLSerializableAdapter col = getContentCollection();
         ResultSet rs = col.queryCollection(tx, XPathQueryResolver.STYLE_XPATH, sb.toString(), null);
         Map grpMap = new HashMap(); // of String to List of Element

         while ( rs.next() ) {
            try {
               DocumentTable dt = rs.getResult();
               Element elem = (Element)DTSMHelper.tableToNode(dt);
               NodeList nl = elem.getElementsByTagName(Constants.GROUP);
               for ( int i = 0; i < nl.getLength(); i++ ) {
                  Element e = (Element)nl.item(i);
                  String grpName = DOMUtils.getText(e);
                  if ( groups != null && !grpSet.contains(grpName) )
                     continue;
                  List l = (List)grpMap.get(grpName);
                  if ( l == null ) {
                     l = new ArrayList();
                     grpMap.put(grpName, l);
                  }
                  l.add(elem);
               }
            }
            catch ( DTSMException e ) {
               throw new DBException(FaultCodes.GEN_GENERAL_ERROR, "Can't convert DocumentTable", e);
            }
         }

         Document doc = DOMHelper.newDocument();
         Element rootElem = doc.createElement(Constants.MANIFEST);
         rootElem.setAttribute(Constants.DATE, new Variant(start).getString());
         doc.appendChild(rootElem);

         Iterator iter = grpMap.keySet().iterator();
         while ( iter.hasNext() ) {
            String key = (String)iter.next();
            Group g = getGroup(key, true);
            Element groupElem = doc.createElement(Constants.GROUP);
            groupElem.setAttribute(Constants.NAME, key);
            groupElem.setAttribute(Constants.VERSION, Double.toString(g.getVersion()));
            groupElem.setAttribute(Constants.STATUS, Constants.getStatusString(g.getStatus()));
            rootElem.appendChild(groupElem);
            List l = (List)grpMap.get(key);
            Iterator i = l.iterator();
            while ( i.hasNext() ) {
               Node n = doc.importNode((Element)i.next(), true);
               groupElem.appendChild(n);
            }
         }

			return doc;
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
		finally {
	   	if ( tx.getStatus() == Transaction.ACTIVE )
   	      tx.commit();
		}
   }

   public static final String[] PARAMS_listAllChanges = {"date"};

   public Document listAllChanges(Date date) throws DBException {
      return listGroupChanges(date, null);
   }

   void storeGroup(Group group) throws DBException {
      Transaction tx = new Transaction();
      try {
         XMLSerializableAdapter col = getGroupsCollection();
         col.setObject(tx, group.getName(), group);
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
		finally {
	   	if ( tx.getStatus() == Transaction.ACTIVE )
   	      tx.commit();
		}
   }

   Group getGroup(String group, boolean create) throws DBException {
      Transaction tx = new Transaction();
      try {
         XMLSerializableAdapter col = getGroupsCollection();
         Group g = (Group)col.getObject(tx, group);
         if ( g == null && create ) {
            g = new Group(group);
            storeGroup(g);
         }
         return g;
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
		finally {
	   	if ( tx.getStatus() == Transaction.ACTIVE )
   	      tx.commit();
		}
   }

   void storeContent(Content content) throws DBException {
      Transaction tx = new Transaction();
      try {
         XMLSerializableAdapter col = getContentCollection();
         col.setObject(tx, content.getPath(), content);
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
		finally {
	   	if ( tx.getStatus() == Transaction.ACTIVE )
   	      tx.commit();
		}
   }

   Content getContent(String path, boolean create) throws DBException {
      Transaction tx = new Transaction();
      try {
         XMLSerializableAdapter col = getContentCollection();
         Content c = (Content)col.getObject(tx, path);
         if ( c == null && create ) {
            c = new Content(path);
            storeContent(c);
         }
         return c;
      }
      catch ( DBException e ) {
         tx.cancel();
         throw e;
      }
		finally {
	   	if ( tx.getStatus() == Transaction.ACTIVE )
   	      tx.commit();
		}
   }

   XMLSerializableAdapter getGroupsCollection() throws DBException {
      if ( groupsCol == null ) {
         Collection sysCol = getCollection().getSystemCollection();
         Collection metaCol = sysCol.getCollection(SystemCollection.METADATA);
         Collection col = metaCol.getCollection(GROUPS_COL);
         groupsCol = new XMLSerializableAdapter(col);
      }
      return groupsCol;
   }

   XMLSerializableAdapter getContentCollection() throws DBException {
      if ( contentCol == null ) {
         Collection sysCol = getCollection().getSystemCollection();
         Collection metaCol = sysCol.getCollection(SystemCollection.METADATA);
         Collection col = metaCol.getCollection(CONTENT_COL);
         contentCol = new XMLSerializableAdapter(col);
      }
      return contentCol;
   }
}
