src/org/sonews/storage/Group.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 11 Oct 2011 00:38:41 +0200
changeset 68 6e16e3bee1ca
parent 48 b78e77619152
permissions -rwxr-xr-x
Drupal: částečně funkční – jde stáhnout zprávy s předmětem a textem.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package org.sonews.storage;
    19 
    20 import java.sql.SQLException;
    21 import java.util.List;
    22 import org.sonews.util.Log;
    23 import org.sonews.util.Pair;
    24 
    25 /**
    26  * Represents a logical Group within this newsserver.
    27  * @author Christian Lins
    28  * @since sonews/0.5.0
    29  */
    30 public class Group {
    31 
    32 	/**
    33 	 * If this flag is set the Group is no real newsgroup but a mailing list
    34 	 * mirror. In that case every posting and receiving mails must go through
    35 	 * the mailing list gateway.
    36 	 */
    37 	public static final int MAILINGLIST = 0x1;
    38 
    39 	/**
    40 	 * If this flag is set the Group is marked as readonly and the posting
    41 	 * is prohibited. This can be useful for groups that are synced only in
    42 	 * one direction.
    43 	 */
    44 	public static final int READONLY = 0x2;
    45 
    46 	/**
    47 	 * If this flag is set the Group is marked as deleted and must not occur
    48 	 * in any output. The deletion is done lazily by a low priority daemon.
    49 	 */
    50 	public static final int DELETED = 0x80;
    51 
    52 	private long id = 0;
    53 	private int flags = -1;
    54 	private String name = null;
    55 
    56 	/**
    57 	 * @return List of all groups this server handles.
    58 	 */
    59 	public static List<Group> getAll() {
    60 		try {
    61 			return StorageManager.current().getGroups();
    62 		} catch (StorageBackendException ex) {
    63 			Log.get().severe(ex.getMessage());
    64 			return null;
    65 		}
    66 	}
    67 
    68 	/**
    69 	 * Constructor.
    70 	 * @param name
    71 	 * @param id
    72 	 * @param flags
    73 	 */
    74 	public Group(final String name, final long id, final int flags) {
    75 		this.id = id;
    76 		this.flags = flags;
    77 		this.name = name;
    78 	}
    79 
    80 	@Override
    81 	public boolean equals(Object obj) {
    82 		if (obj instanceof Group) {
    83 			return ((Group) obj).id == this.id;
    84 		} else {
    85 			return false;
    86 		}
    87 	}
    88 
    89 	public Article getArticle(long idx)
    90 			throws StorageBackendException {
    91 		return StorageManager.current().getArticle(idx, this.id);
    92 	}
    93 
    94 	public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
    95 			throws StorageBackendException {
    96 		return StorageManager.current().getArticleHeads(this, first, last);
    97 	}
    98 
    99 	public List<Long> getArticleNumbers()
   100 			throws StorageBackendException {
   101 		return StorageManager.current().getArticleNumbers(id);
   102 	}
   103 
   104 	public long getFirstArticleNumber()
   105 			throws StorageBackendException {
   106 		return StorageManager.current().getFirstArticleNumber(this);
   107 	}
   108 
   109 	public int getFlags() {
   110 		return this.flags;
   111 	}
   112 
   113 	public long getIndexOf(Article art)
   114 			throws StorageBackendException {
   115 		return StorageManager.current().getArticleIndex(art, this);
   116 	}
   117 
   118 	/**
   119 	 * Returns the group id.
   120 	 */
   121 	public long getInternalID() {
   122 		assert id > 0;
   123 		return id;
   124 	}
   125 
   126 	public boolean isDeleted() {
   127 		return (this.flags & DELETED) != 0;
   128 	}
   129 
   130 	public boolean isMailingList() {
   131 		return (this.flags & MAILINGLIST) != 0;
   132 	}
   133 
   134 	public boolean isWriteable() {
   135 		return true;
   136 	}
   137 
   138 	public long getLastArticleNumber()
   139 			throws StorageBackendException {
   140 		return StorageManager.current().getLastArticleNumber(this);
   141 	}
   142 
   143 	public String getName() {
   144 		return name;
   145 	}
   146 
   147 	/**
   148 	 * Performs this.flags |= flag to set a specified flag and updates the data
   149 	 * in the JDBCDatabase.
   150 	 * @param flag
   151 	 */
   152 	public void setFlag(final int flag) {
   153 		this.flags |= flag;
   154 	}
   155 
   156 	public void unsetFlag(final int flag) {
   157 		this.flags &= ~flag;
   158 	}
   159 
   160 	public void setName(final String name) {
   161 		this.name = name;
   162 	}
   163 
   164 	/**
   165 	 * @return Number of posted articles in this group.
   166 	 * @throws java.sql.SQLException
   167 	 */
   168 	public long getPostingsCount()
   169 			throws StorageBackendException {
   170 		return StorageManager.current().getPostingsCount(this.name);
   171 	}
   172 
   173 	/**
   174 	 * Updates flags and name in the backend.
   175 	 */
   176 	public void update()
   177 			throws StorageBackendException {
   178 		StorageManager.current().update(this);
   179 	}
   180 }