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