org/sonews/storage/Group.java
author cli
Thu Aug 20 21:31:03 2009 +0200 (2009-08-20)
changeset 16 5a4a41cfc0a3
parent 3 2fdc9cc89502
child 31 087ef6fe6a1a
permissions -rw-r--r--
Issue #538 fixed.
     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    * Returns a Group identified by its full name.
    41    * @param name
    42    * @return
    43    */
    44   public static Group getByName(final String name)
    45   {
    46     try
    47     {
    48       return StorageManager.current().getGroup(name);
    49     }
    50     catch(StorageBackendException ex)
    51     {
    52       ex.printStackTrace();
    53       return null;
    54     }
    55   }
    56 
    57   /**
    58    * @return List of all groups this server handles.
    59    */
    60   public static List<Channel> getAll()
    61   {
    62     try
    63     {
    64       return StorageManager.current().getGroups();
    65     }
    66     catch(StorageBackendException ex)
    67     {
    68       Log.get().severe(ex.getMessage());
    69       return null;
    70     }
    71   }
    72   
    73   /**
    74    * @param name
    75    * @param id
    76    */
    77   public Group(final String name, final long id, final int flags)
    78   {
    79     this.id    = id;
    80     this.flags = flags;
    81     this.name  = name;
    82   }
    83 
    84   @Override
    85   public boolean equals(Object obj)
    86   {
    87     if(obj instanceof Group)
    88     {
    89       return ((Group)obj).id == this.id;
    90     }
    91     else
    92     {
    93       return false;
    94     }
    95   }
    96 
    97   public Article getArticle(long idx)
    98     throws StorageBackendException
    99   {
   100     return StorageManager.current().getArticle(idx, this.id);
   101   }
   102 
   103   public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
   104     throws StorageBackendException
   105   {
   106     return StorageManager.current().getArticleHeads(this, first, last);
   107   }
   108   
   109   public List<Long> getArticleNumbers()
   110     throws StorageBackendException
   111   {
   112     return StorageManager.current().getArticleNumbers(id);
   113   }
   114 
   115   public long getFirstArticleNumber()
   116     throws StorageBackendException
   117   {
   118     return StorageManager.current().getFirstArticleNumber(this);
   119   }
   120 
   121   public int getFlags()
   122   {
   123     return this.flags;
   124   }
   125 
   126   public long getIndexOf(Article art)
   127     throws StorageBackendException
   128   {
   129     return StorageManager.current().getArticleIndex(art, this);
   130   }
   131 
   132   /**
   133    * Returns the group id.
   134    */
   135   public long getInternalID()
   136   {
   137     assert id > 0;
   138 
   139     return id;
   140   }
   141 
   142   public boolean isDeleted()
   143   {
   144     return (this.flags & DELETED) != 0;
   145   }
   146 
   147   public boolean isMailingList()
   148   {
   149     return (this.flags & MAILINGLIST) != 0;
   150   }
   151 
   152   public boolean isWriteable()
   153   {
   154     return true;
   155   }
   156 
   157   public long getLastArticleNumber()
   158     throws StorageBackendException
   159   {
   160     return StorageManager.current().getLastArticleNumber(this);
   161   }
   162 
   163   public String getName()
   164   {
   165     return name;
   166   }
   167 
   168   /**
   169    * Performs this.flags |= flag to set a specified flag and updates the data
   170    * in the JDBCDatabase.
   171    * @param flag
   172    */
   173   public void setFlag(final int flag)
   174   {
   175     this.flags |= flag;
   176   }
   177 
   178   public void setName(final String name)
   179   {
   180     this.name = name;
   181   }
   182 
   183   /**
   184    * @return Number of posted articles in this group.
   185    * @throws java.sql.SQLException
   186    */
   187   public long getPostingsCount()
   188     throws StorageBackendException
   189   {
   190     return StorageManager.current().getPostingsCount(this.name);
   191   }
   192 
   193   /**
   194    * Updates flags and name in the backend.
   195    */
   196   public void update()
   197     throws StorageBackendException
   198   {
   199     StorageManager.current().update(this);
   200   }
   201 
   202 }