org/sonews/storage/Group.java
author cli
Wed May 12 11:18:02 2010 +0200 (2010-05-12)
changeset 31 087ef6fe6a1a
parent 16 5a4a41cfc0a3
permissions -rw-r--r--
Group.getByName() removed. Channel.getByName() does no longer catch StorageBackendExceptions but throw them further.
     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     {
    46       return StorageManager.current().getGroups();
    47     }
    48     catch(StorageBackendException ex)
    49     {
    50       Log.get().severe(ex.getMessage());
    51       return null;
    52     }
    53   }
    54   
    55   /**
    56    * @param name
    57    * @param id
    58    */
    59   public Group(final String name, final long id, final int flags)
    60   {
    61     this.id    = id;
    62     this.flags = flags;
    63     this.name  = name;
    64   }
    65 
    66   @Override
    67   public boolean equals(Object obj)
    68   {
    69     if(obj instanceof Group)
    70     {
    71       return ((Group)obj).id == this.id;
    72     }
    73     else
    74     {
    75       return false;
    76     }
    77   }
    78 
    79   public Article getArticle(long idx)
    80     throws StorageBackendException
    81   {
    82     return StorageManager.current().getArticle(idx, this.id);
    83   }
    84 
    85   public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
    86     throws StorageBackendException
    87   {
    88     return StorageManager.current().getArticleHeads(this, first, last);
    89   }
    90   
    91   public List<Long> getArticleNumbers()
    92     throws StorageBackendException
    93   {
    94     return StorageManager.current().getArticleNumbers(id);
    95   }
    96 
    97   public long getFirstArticleNumber()
    98     throws StorageBackendException
    99   {
   100     return StorageManager.current().getFirstArticleNumber(this);
   101   }
   102 
   103   public int getFlags()
   104   {
   105     return this.flags;
   106   }
   107 
   108   public long getIndexOf(Article art)
   109     throws StorageBackendException
   110   {
   111     return StorageManager.current().getArticleIndex(art, this);
   112   }
   113 
   114   /**
   115    * Returns the group id.
   116    */
   117   public long getInternalID()
   118   {
   119     assert id > 0;
   120 
   121     return id;
   122   }
   123 
   124   public boolean isDeleted()
   125   {
   126     return (this.flags & DELETED) != 0;
   127   }
   128 
   129   public boolean isMailingList()
   130   {
   131     return (this.flags & MAILINGLIST) != 0;
   132   }
   133 
   134   public boolean isWriteable()
   135   {
   136     return true;
   137   }
   138 
   139   public long getLastArticleNumber()
   140     throws StorageBackendException
   141   {
   142     return StorageManager.current().getLastArticleNumber(this);
   143   }
   144 
   145   public String getName()
   146   {
   147     return name;
   148   }
   149 
   150   /**
   151    * Performs this.flags |= flag to set a specified flag and updates the data
   152    * in the JDBCDatabase.
   153    * @param flag
   154    */
   155   public void setFlag(final int flag)
   156   {
   157     this.flags |= flag;
   158   }
   159 
   160   public void setName(final String name)
   161   {
   162     this.name = name;
   163   }
   164 
   165   /**
   166    * @return Number of posted articles in this group.
   167    * @throws java.sql.SQLException
   168    */
   169   public long getPostingsCount()
   170     throws StorageBackendException
   171   {
   172     return StorageManager.current().getPostingsCount(this.name);
   173   }
   174 
   175   /**
   176    * Updates flags and name in the backend.
   177    */
   178   public void update()
   179     throws StorageBackendException
   180   {
   181     StorageManager.current().update(this);
   182   }
   183 
   184 }