src/org/sonews/storage/Group.java
author cli
Tue, 07 Jun 2011 11:55:22 +0200
changeset 44 5d7d1adf387f
parent 42 7f84f4de2893
child 47 e118b4d60029
permissions -rwxr-xr-x
Work on hsqldb support
     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 // TODO: This class should not be public!
    31 public class Group extends Channel {
    32 
    33 	private long id = 0;
    34 	private int flags = -1;
    35 	private String name = null;
    36 
    37 	/**
    38 	 * @return List of all groups this server handles.
    39 	 */
    40 	public static List<Channel> getAll() {
    41 		try {
    42 			return StorageManager.current().getGroups();
    43 		} catch (StorageBackendException ex) {
    44 			Log.get().severe(ex.getMessage());
    45 			return null;
    46 		}
    47 	}
    48 
    49 	/**
    50 	 * @param name
    51 	 * @param id
    52 	 */
    53 	public Group(final String name, final long id, final int flags) {
    54 		this.id = id;
    55 		this.flags = flags;
    56 		this.name = name;
    57 	}
    58 
    59 	@Override
    60 	public boolean equals(Object obj) {
    61 		if (obj instanceof Group) {
    62 			return ((Group) obj).id == this.id;
    63 		} else {
    64 			return false;
    65 		}
    66 	}
    67 
    68 	public Article getArticle(long idx)
    69 			throws StorageBackendException {
    70 		return StorageManager.current().getArticle(idx, this.id);
    71 	}
    72 
    73 	public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
    74 			throws StorageBackendException {
    75 		return StorageManager.current().getArticleHeads(this, first, last);
    76 	}
    77 
    78 	public List<Long> getArticleNumbers()
    79 			throws StorageBackendException {
    80 		return StorageManager.current().getArticleNumbers(id);
    81 	}
    82 
    83 	public long getFirstArticleNumber()
    84 			throws StorageBackendException {
    85 		return StorageManager.current().getFirstArticleNumber(this);
    86 	}
    87 
    88 	public int getFlags() {
    89 		return this.flags;
    90 	}
    91 
    92 	public long getIndexOf(Article art)
    93 			throws StorageBackendException {
    94 		return StorageManager.current().getArticleIndex(art, this);
    95 	}
    96 
    97 	/**
    98 	 * Returns the group id.
    99 	 */
   100 	public long getInternalID() {
   101 		assert id > 0;
   102 
   103 		return id;
   104 	}
   105 
   106 	public boolean isDeleted() {
   107 		return (this.flags & DELETED) != 0;
   108 	}
   109 
   110 	public boolean isMailingList() {
   111 		return (this.flags & MAILINGLIST) != 0;
   112 	}
   113 
   114 	public boolean isWriteable() {
   115 		return true;
   116 	}
   117 
   118 	public long getLastArticleNumber()
   119 			throws StorageBackendException {
   120 		return StorageManager.current().getLastArticleNumber(this);
   121 	}
   122 
   123 	public String getName() {
   124 		return name;
   125 	}
   126 
   127 	/**
   128 	 * Performs this.flags |= flag to set a specified flag and updates the data
   129 	 * in the JDBCDatabase.
   130 	 * @param flag
   131 	 */
   132 	public void setFlag(final int flag) {
   133 		this.flags |= flag;
   134 	}
   135 
   136 	public void setName(final String name) {
   137 		this.name = name;
   138 	}
   139 
   140 	/**
   141 	 * @return Number of posted articles in this group.
   142 	 * @throws java.sql.SQLException
   143 	 */
   144 	public long getPostingsCount()
   145 			throws StorageBackendException {
   146 		return StorageManager.current().getPostingsCount(this.name);
   147 	}
   148 
   149 	/**
   150 	 * Updates flags and name in the backend.
   151 	 */
   152 	public void update()
   153 			throws StorageBackendException {
   154 		StorageManager.current().update(this);
   155 	}
   156 }