org/sonews/daemon/storage/Group.java
author chris <chris@marvin>
Fri, 26 Jun 2009 16:48:50 +0200
changeset 1 6fceb66e1ad7
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
     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.daemon.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 public class Group
    32 {
    33 
    34   /** 
    35    * If this flag is set the Group is no real newsgroup but a mailing list
    36    * mirror. In that case every posting and receiving mails must go through
    37    * the mailing list gateway.
    38    */
    39   public static final int MAILINGLIST = 0x1;
    40   
    41   /**
    42    * If this flag is set the Group is marked as readonly and the posting
    43    * is prohibited. This can be useful for groups that are synced only in
    44    * one direction.
    45    */
    46   public static final int READONLY    = 0x2;
    47 
    48   /**
    49    * If this flag is set the Group is marked as deleted and must not occur
    50    * in any output. The deletion is done lazily by a low priority daemon.
    51    */
    52   public static final int DELETED     = 0x128;
    53   
    54   private long   id     = 0;
    55   private int    flags  = -1;
    56   private String name   = null;
    57   
    58   /**
    59    * Returns a Group identified by its full name.
    60    * @param name
    61    * @return
    62    */
    63   public static Group getByName(final String name)
    64   {
    65     try
    66     {
    67       return Database.getInstance().getGroup(name);
    68     }
    69     catch(SQLException ex)
    70     {
    71       ex.printStackTrace();
    72       return null;
    73     }
    74   }
    75 
    76   /**
    77    * @return List of all groups this server handles.
    78    */
    79   public static List<Group> getAll()
    80   {
    81     try
    82     {
    83       return Database.getInstance().getGroups();
    84     }
    85     catch(SQLException ex)
    86     {
    87       Log.msg(ex.getMessage(), false);
    88       return null;
    89     }
    90   }
    91   
    92   /**
    93    * Private constructor.
    94    * @param name
    95    * @param id
    96    */
    97   Group(final String name, final long id, final int flags)
    98   {
    99     this.id    = id;
   100     this.flags = flags;
   101     this.name  = name;
   102   }
   103 
   104   @Override
   105   public boolean equals(Object obj)
   106   {
   107     if(obj instanceof Group)
   108     {
   109       return ((Group)obj).id == this.id;
   110     }
   111     else
   112     {
   113       return false;
   114     }
   115   }
   116     
   117   public List<Pair<Long, ArticleHead>> getArticleHeads(final int first, final int last)
   118     throws SQLException
   119   {
   120     return Database.getInstance().getArticleHeads(this, first, last);
   121   }
   122   
   123   public List<Long> getArticleNumbers()
   124     throws SQLException
   125   {
   126     return Database.getInstance().getArticleNumbers(id);
   127   }
   128 
   129   public int getFirstArticleNumber()
   130     throws SQLException
   131   {
   132     return Database.getInstance().getFirstArticleNumber(this);
   133   }
   134 
   135   /**
   136    * Returns the group id.
   137    */
   138   public long getID()
   139   {
   140     assert id > 0;
   141 
   142     return id;
   143   }
   144   
   145   public boolean isMailingList()
   146   {
   147     return (this.flags & MAILINGLIST) != 0;
   148   }
   149 
   150   public int getLastArticleNumber()
   151     throws SQLException
   152   {
   153     return Database.getInstance().getLastArticleNumber(this);
   154   }
   155 
   156   public String getName()
   157   {
   158     return name;
   159   }
   160 
   161   /**
   162    * Performs this.flags |= flag to set a specified flag and updates the data
   163    * in the Database.
   164    * @param flag
   165    */
   166   public void setFlag(final int flag)
   167   {
   168     this.flags |= flag;
   169   }
   170 
   171   public void setName(final String name)
   172   {
   173     this.name = name;
   174   }
   175 
   176   /**
   177    * @return Number of posted articles in this group.
   178    * @throws java.sql.SQLException
   179    */
   180   public int getPostingsCount()
   181     throws SQLException
   182   {
   183     return Database.getInstance().getPostingsCount(this.name);
   184   }
   185 
   186 }