org/sonews/storage/Group.java
changeset 12 bb6990c0dd1a
child 16 5a4a41cfc0a3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/storage/Group.java	Thu Aug 20 14:31:19 2009 +0200
     1.3 @@ -0,0 +1,202 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.storage;
    1.23 +
    1.24 +import java.sql.SQLException;
    1.25 +import java.util.List;
    1.26 +import org.sonews.util.Log;
    1.27 +import org.sonews.util.Pair;
    1.28 +
    1.29 +/**
    1.30 + * Represents a logical Group within this newsserver.
    1.31 + * @author Christian Lins
    1.32 + * @since sonews/0.5.0
    1.33 + */
    1.34 +// TODO: This class should not be public!
    1.35 +public class Group extends Channel
    1.36 +{
    1.37 +  
    1.38 +  private long   id     = 0;
    1.39 +  private int    flags  = -1;
    1.40 +  private String name   = null;
    1.41 +  
    1.42 +  /**
    1.43 +   * Returns a Group identified by its full name.
    1.44 +   * @param name
    1.45 +   * @return
    1.46 +   */
    1.47 +  public static Group getByName(final String name)
    1.48 +  {
    1.49 +    try
    1.50 +    {
    1.51 +      return StorageManager.current().getGroup(name);
    1.52 +    }
    1.53 +    catch(StorageBackendException ex)
    1.54 +    {
    1.55 +      ex.printStackTrace();
    1.56 +      return null;
    1.57 +    }
    1.58 +  }
    1.59 +
    1.60 +  /**
    1.61 +   * @return List of all groups this server handles.
    1.62 +   */
    1.63 +  public static List<Channel> getAll()
    1.64 +  {
    1.65 +    try
    1.66 +    {
    1.67 +      return StorageManager.current().getGroups();
    1.68 +    }
    1.69 +    catch(StorageBackendException ex)
    1.70 +    {
    1.71 +      Log.msg(ex.getMessage(), false);
    1.72 +      return null;
    1.73 +    }
    1.74 +  }
    1.75 +  
    1.76 +  /**
    1.77 +   * @param name
    1.78 +   * @param id
    1.79 +   */
    1.80 +  public Group(final String name, final long id, final int flags)
    1.81 +  {
    1.82 +    this.id    = id;
    1.83 +    this.flags = flags;
    1.84 +    this.name  = name;
    1.85 +  }
    1.86 +
    1.87 +  @Override
    1.88 +  public boolean equals(Object obj)
    1.89 +  {
    1.90 +    if(obj instanceof Group)
    1.91 +    {
    1.92 +      return ((Group)obj).id == this.id;
    1.93 +    }
    1.94 +    else
    1.95 +    {
    1.96 +      return false;
    1.97 +    }
    1.98 +  }
    1.99 +
   1.100 +  public Article getArticle(long idx)
   1.101 +    throws StorageBackendException
   1.102 +  {
   1.103 +    return StorageManager.current().getArticle(idx, this.id);
   1.104 +  }
   1.105 +
   1.106 +  public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
   1.107 +    throws StorageBackendException
   1.108 +  {
   1.109 +    return StorageManager.current().getArticleHeads(this, first, last);
   1.110 +  }
   1.111 +  
   1.112 +  public List<Long> getArticleNumbers()
   1.113 +    throws StorageBackendException
   1.114 +  {
   1.115 +    return StorageManager.current().getArticleNumbers(id);
   1.116 +  }
   1.117 +
   1.118 +  public long getFirstArticleNumber()
   1.119 +    throws StorageBackendException
   1.120 +  {
   1.121 +    return StorageManager.current().getFirstArticleNumber(this);
   1.122 +  }
   1.123 +
   1.124 +  public int getFlags()
   1.125 +  {
   1.126 +    return this.flags;
   1.127 +  }
   1.128 +
   1.129 +  public long getIndexOf(Article art)
   1.130 +    throws StorageBackendException
   1.131 +  {
   1.132 +    return StorageManager.current().getArticleIndex(art, this);
   1.133 +  }
   1.134 +
   1.135 +  /**
   1.136 +   * Returns the group id.
   1.137 +   */
   1.138 +  public long getInternalID()
   1.139 +  {
   1.140 +    assert id > 0;
   1.141 +
   1.142 +    return id;
   1.143 +  }
   1.144 +
   1.145 +  public boolean isDeleted()
   1.146 +  {
   1.147 +    return (this.flags & DELETED) != 0;
   1.148 +  }
   1.149 +
   1.150 +  public boolean isMailingList()
   1.151 +  {
   1.152 +    return (this.flags & MAILINGLIST) != 0;
   1.153 +  }
   1.154 +
   1.155 +  public boolean isWriteable()
   1.156 +  {
   1.157 +    return true;
   1.158 +  }
   1.159 +
   1.160 +  public long getLastArticleNumber()
   1.161 +    throws StorageBackendException
   1.162 +  {
   1.163 +    return StorageManager.current().getLastArticleNumber(this);
   1.164 +  }
   1.165 +
   1.166 +  public String getName()
   1.167 +  {
   1.168 +    return name;
   1.169 +  }
   1.170 +
   1.171 +  /**
   1.172 +   * Performs this.flags |= flag to set a specified flag and updates the data
   1.173 +   * in the JDBCDatabase.
   1.174 +   * @param flag
   1.175 +   */
   1.176 +  public void setFlag(final int flag)
   1.177 +  {
   1.178 +    this.flags |= flag;
   1.179 +  }
   1.180 +
   1.181 +  public void setName(final String name)
   1.182 +  {
   1.183 +    this.name = name;
   1.184 +  }
   1.185 +
   1.186 +  /**
   1.187 +   * @return Number of posted articles in this group.
   1.188 +   * @throws java.sql.SQLException
   1.189 +   */
   1.190 +  public long getPostingsCount()
   1.191 +    throws StorageBackendException
   1.192 +  {
   1.193 +    return StorageManager.current().getPostingsCount(this.name);
   1.194 +  }
   1.195 +
   1.196 +  /**
   1.197 +   * Updates flags and name in the backend.
   1.198 +   */
   1.199 +  public void update()
   1.200 +    throws StorageBackendException
   1.201 +  {
   1.202 +    StorageManager.current().update(this);
   1.203 +  }
   1.204 +
   1.205 +}