chris@3: /*
chris@3: * SONEWS News Server
chris@3: * see AUTHORS for the list of contributors
chris@3: *
chris@3: * This program is free software: you can redistribute it and/or modify
chris@3: * it under the terms of the GNU General Public License as published by
chris@3: * the Free Software Foundation, either version 3 of the License, or
chris@3: * (at your option) any later version.
chris@3: *
chris@3: * This program is distributed in the hope that it will be useful,
chris@3: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@3: * GNU General Public License for more details.
chris@3: *
chris@3: * You should have received a copy of the GNU General Public License
chris@3: * along with this program. If not, see .
chris@3: */
chris@3:
chris@3: package org.sonews.storage;
chris@3:
chris@3: import java.sql.SQLException;
chris@3: import java.util.List;
chris@3: import org.sonews.util.Log;
chris@3: import org.sonews.util.Pair;
chris@3:
chris@3: /**
chris@3: * Represents a logical Group within this newsserver.
chris@3: * @author Christian Lins
chris@3: * @since sonews/0.5.0
chris@3: */
chris@3: // TODO: This class should not be public!
chris@3: public class Group extends Channel
chris@3: {
chris@3:
cli@37: private long id = 0;
cli@37: private int flags = -1;
cli@37: private String name = null;
chris@3:
cli@37: /**
cli@37: * @return List of all groups this server handles.
cli@37: */
cli@37: public static List getAll()
cli@37: {
cli@37: try {
cli@37: return StorageManager.current().getGroups();
cli@37: } catch (StorageBackendException ex) {
cli@37: Log.get().severe(ex.getMessage());
cli@37: return null;
cli@37: }
cli@37: }
chris@3:
cli@37: /**
cli@37: * @param name
cli@37: * @param id
cli@37: */
cli@37: public Group(final String name, final long id, final int flags)
cli@37: {
cli@37: this.id = id;
cli@37: this.flags = flags;
cli@37: this.name = name;
cli@37: }
chris@3:
cli@37: @Override
cli@37: public boolean equals(Object obj)
cli@37: {
cli@37: if (obj instanceof Group) {
cli@37: return ((Group) obj).id == this.id;
cli@37: } else {
cli@37: return false;
cli@37: }
cli@37: }
chris@3:
cli@37: public Article getArticle(long idx)
cli@37: throws StorageBackendException
cli@37: {
cli@37: return StorageManager.current().getArticle(idx, this.id);
cli@37: }
chris@3:
cli@37: public List> getArticleHeads(final long first, final long last)
cli@37: throws StorageBackendException
cli@37: {
cli@37: return StorageManager.current().getArticleHeads(this, first, last);
cli@37: }
chris@3:
cli@37: public List getArticleNumbers()
cli@37: throws StorageBackendException
cli@37: {
cli@37: return StorageManager.current().getArticleNumbers(id);
cli@37: }
chris@3:
cli@37: public long getFirstArticleNumber()
cli@37: throws StorageBackendException
cli@37: {
cli@37: return StorageManager.current().getFirstArticleNumber(this);
cli@37: }
chris@3:
cli@37: public int getFlags()
cli@37: {
cli@37: return this.flags;
cli@37: }
chris@3:
cli@37: public long getIndexOf(Article art)
cli@37: throws StorageBackendException
cli@37: {
cli@37: return StorageManager.current().getArticleIndex(art, this);
cli@37: }
chris@3:
cli@37: /**
cli@37: * Returns the group id.
cli@37: */
cli@37: public long getInternalID()
cli@37: {
cli@37: assert id > 0;
chris@3:
cli@37: return id;
cli@37: }
chris@3:
cli@37: public boolean isDeleted()
cli@37: {
cli@37: return (this.flags & DELETED) != 0;
cli@37: }
chris@3:
cli@37: public boolean isMailingList()
cli@37: {
cli@37: return (this.flags & MAILINGLIST) != 0;
cli@37: }
chris@3:
cli@37: public boolean isWriteable()
cli@37: {
cli@37: return true;
cli@37: }
chris@3:
cli@37: public long getLastArticleNumber()
cli@37: throws StorageBackendException
cli@37: {
cli@37: return StorageManager.current().getLastArticleNumber(this);
cli@37: }
chris@3:
cli@37: public String getName()
cli@37: {
cli@37: return name;
cli@37: }
chris@3:
cli@37: /**
cli@37: * Performs this.flags |= flag to set a specified flag and updates the data
cli@37: * in the JDBCDatabase.
cli@37: * @param flag
cli@37: */
cli@37: public void setFlag(final int flag)
cli@37: {
cli@37: this.flags |= flag;
cli@37: }
chris@3:
cli@37: public void setName(final String name)
cli@37: {
cli@37: this.name = name;
cli@37: }
cli@37:
cli@37: /**
cli@37: * @return Number of posted articles in this group.
cli@37: * @throws java.sql.SQLException
cli@37: */
cli@37: public long getPostingsCount()
cli@37: throws StorageBackendException
cli@37: {
cli@37: return StorageManager.current().getPostingsCount(this.name);
cli@37: }
cli@37:
cli@37: /**
cli@37: * Updates flags and name in the backend.
cli@37: */
cli@37: public void update()
cli@37: throws StorageBackendException
cli@37: {
cli@37: StorageManager.current().update(this);
cli@37: }
chris@3: }