class Command(BaseCommand):
    help = """Reindex the configured eXist-db collection."""

    def handle(self, *args, **options):
        # check for required settings
        if not hasattr(settings, 'EXISTDB_ROOT_COLLECTION') or not settings.EXISTDB_ROOT_COLLECTION:
            raise CommandError("EXISTDB_ROOT_COLLECTION setting is missing")
            return

        collection = settings.EXISTDB_ROOT_COLLECTION      

        # Explicitly request no timeout (even if one is configured in
        # django settings), since reindexing could take a while.
        self.db = ExistDB(timeout=None)
        if not self.db.hasCollection(collection):
            raise CommandError("Collection %s does not exist" % collection)

        try:
            print "Reindexing collection %s" % collection
            print "-- If you have a large collection, this may take a while."
            start_time = time.time()
            success = self.db.reindexCollection(collection)
            end_time = time.time()
            if success:
                print "Successfully reindexed collection %s" % collection
                print "Reindexing took %.2f seconds" % (end_time - start_time)
            else:
                print "Failed to reindexed collection %s" % collection
                print "-- Check that the configured exist user is in the exist DBA group."
        except Exception, err:
            raise CommandError(err)
    def handle(self, *args, **options):
        if not len(args) or args[0] == 'help':
            print self.help
            return

        cmd = args[0]
        if cmd not in ['load', 'show', 'show-info', 'remove']:
            print "Command '%s' not recognized" % cmd
            print self.help
            return

        # check for required settings (used in all modes)
        if not hasattr(settings, 'EXISTDB_ROOT_COLLECTION') or not settings.EXISTDB_ROOT_COLLECTION:
            raise CommandError("EXISTDB_ROOT_COLLECTION setting is missing")
            return
        if not hasattr(settings, 'EXISTDB_INDEX_CONFIGFILE') or not settings.EXISTDB_INDEX_CONFIGFILE:
            raise CommandError("EXISTDB_INDEX_CONFIGFILE setting is missing")
            return

        collection = settings.EXISTDB_ROOT_COLLECTION
        index = settings.EXISTDB_INDEX_CONFIGFILE

        try:
            self.db = ExistDB()

            # check there is already an index config
            hasindex = self.db.hasCollectionIndex(collection)

            # for all commands but load, nothing to do if config collection does not exist
            if not hasindex and cmd != 'load':
                print "Collection %s has no index configuration" % collection
                return

            if cmd == 'load':
                # load collection index to eXist

                # no easy way to check if index is different, but give some info to user to help indicate
                if hasindex:
                    index_desc = self.db.describeDocument(self.db._collectionIndexPath(collection))
                    print "Collection already has an index configuration; last modified %s\n" % index_desc['modified']
                else:
                    print "This appears to be a new index configuration\n"

                message =  "eXist index configuration \n collection:\t%s\n index file:\t%s" % (collection, index)

                success = self.db.loadCollectionIndex(collection, open(index))
                if success:
                    print "Succesfully updated %s" % message
                    print """
If your collection already contains data and the index configuration
is new or has changed, you should reindex the collection.
            """
                else:
                    raise CommandError("Failed to update %s" % message)

            elif cmd == 'show':
                # show the contents of the the collection index config file in exist
                print self.db.getDoc(self.db._collectionIndexPath(collection))

            elif cmd == 'show-info':
                # show information about the collection index config file in exist
                index_desc = self.db.describeDocument(self.db._collectionIndexPath(collection))
                for field, val in index_desc.items():
                    print "%s:\t%s" % (field, val)

            elif cmd == 'remove':
                # remove any collection index in eXist
                if self.db.removeCollectionIndex(collection):
                    print "Removed collection index configuration for %s" % collection
                else:
                    raise CommandError("Failed to remove collection index configuration for %s" % collection)

        except Exception, err:
            # better error messages would be nice...
            raise CommandError(err)