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: 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: */ cli@48: public class Group { chris@3: cli@47: /** cli@47: * If this flag is set the Group is no real newsgroup but a mailing list cli@47: * mirror. In that case every posting and receiving mails must go through cli@47: * the mailing list gateway. cli@47: */ cli@47: public static final int MAILINGLIST = 0x1; cli@47: cli@47: /** cli@47: * If this flag is set the Group is marked as readonly and the posting cli@47: * is prohibited. This can be useful for groups that are synced only in cli@47: * one direction. cli@47: */ cli@47: public static final int READONLY = 0x2; cli@47: cli@47: /** cli@47: * If this flag is set the Group is marked as deleted and must not occur cli@47: * in any output. The deletion is done lazily by a low priority daemon. cli@47: */ cli@47: public static final int DELETED = 0x80; cli@47: 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@48: public static List getAll() { 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@47: * Constructor. cli@37: * @param name cli@37: * @param id cli@47: * @param flags cli@37: */ cli@42: public Group(final String name, final long id, final int flags) { cli@37: this.id = id; cli@37: this.flags = flags; cli@37: this.name = name; cli@37: } chris@3: cli@37: @Override cli@42: public boolean equals(Object obj) { 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@42: throws StorageBackendException { 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@42: throws StorageBackendException { cli@37: return StorageManager.current().getArticleHeads(this, first, last); cli@37: } chris@3: cli@37: public List getArticleNumbers() cli@42: throws StorageBackendException { cli@37: return StorageManager.current().getArticleNumbers(id); cli@37: } chris@3: cli@37: public long getFirstArticleNumber() cli@42: throws StorageBackendException { cli@37: return StorageManager.current().getFirstArticleNumber(this); cli@37: } chris@3: cli@42: public int getFlags() { cli@37: return this.flags; cli@37: } chris@3: cli@37: public long getIndexOf(Article art) cli@42: throws StorageBackendException { cli@37: return StorageManager.current().getArticleIndex(art, this); cli@37: } chris@3: cli@37: /** cli@37: * Returns the group id. cli@37: */ cli@42: public long getInternalID() { cli@37: assert id > 0; cli@37: return id; cli@37: } chris@3: cli@42: public boolean isDeleted() { cli@37: return (this.flags & DELETED) != 0; cli@37: } chris@3: cli@42: public boolean isMailingList() { cli@37: return (this.flags & MAILINGLIST) != 0; cli@37: } chris@3: cli@42: public boolean isWriteable() { cli@37: return true; cli@37: } chris@3: cli@37: public long getLastArticleNumber() cli@42: throws StorageBackendException { cli@37: return StorageManager.current().getLastArticleNumber(this); cli@37: } chris@3: cli@42: public String getName() { 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@42: public void setFlag(final int flag) { cli@37: this.flags |= flag; cli@37: } chris@3: cli@47: public void unsetFlag(final int flag) { cli@47: this.flags &= ~flag; cli@47: } cli@47: cli@42: public void setName(final String name) { 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@42: throws StorageBackendException { 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@42: throws StorageBackendException { cli@37: StorageManager.current().update(this); cli@37: } chris@3: }