chris@3: /*
chris@3:  *   SONEWS News Server
chris@3:  *   see AUTHORS for the list of contributors
chris@3:  *
chris@3:  *   This program is free software: you can redistribute it and/or modify
chris@3:  *   it under the terms of the GNU General Public License as published by
chris@3:  *   the Free Software Foundation, either version 3 of the License, or
chris@3:  *   (at your option) any later version.
chris@3:  *
chris@3:  *   This program is distributed in the hope that it will be useful,
chris@3:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3:  *   GNU General Public License for more details.
chris@3:  *
chris@3:  *   You should have received a copy of the GNU General Public License
chris@3:  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3:  */
chris@3: 
chris@3: package org.sonews.storage;
chris@3: 
chris@3: import java.sql.SQLException;
chris@3: import java.util.List;
chris@3: import org.sonews.util.Log;
chris@3: import org.sonews.util.Pair;
chris@3: 
chris@3: /**
chris@3:  * Represents a logical Group within this newsserver.
chris@3:  * @author Christian Lins
chris@3:  * @since sonews/0.5.0
chris@3:  */
chris@3: // TODO: This class should not be public!
chris@3: public class Group extends Channel
chris@3: {
chris@3:   
chris@3:   private long   id     = 0;
chris@3:   private int    flags  = -1;
chris@3:   private String name   = null;
chris@3:   
chris@3:   /**
chris@3:    * Returns a Group identified by its full name.
chris@3:    * @param name
chris@3:    * @return
chris@3:    */
chris@3:   public static Group getByName(final String name)
chris@3:   {
chris@3:     try
chris@3:     {
chris@3:       return StorageManager.current().getGroup(name);
chris@3:     }
chris@3:     catch(StorageBackendException ex)
chris@3:     {
chris@3:       ex.printStackTrace();
chris@3:       return null;
chris@3:     }
chris@3:   }
chris@3: 
chris@3:   /**
chris@3:    * @return List of all groups this server handles.
chris@3:    */
chris@3:   public static List<Channel> getAll()
chris@3:   {
chris@3:     try
chris@3:     {
chris@3:       return StorageManager.current().getGroups();
chris@3:     }
chris@3:     catch(StorageBackendException ex)
chris@3:     {
chris@3:       Log.msg(ex.getMessage(), false);
chris@3:       return null;
chris@3:     }
chris@3:   }
chris@3:   
chris@3:   /**
chris@3:    * @param name
chris@3:    * @param id
chris@3:    */
chris@3:   public Group(final String name, final long id, final int flags)
chris@3:   {
chris@3:     this.id    = id;
chris@3:     this.flags = flags;
chris@3:     this.name  = name;
chris@3:   }
chris@3: 
chris@3:   @Override
chris@3:   public boolean equals(Object obj)
chris@3:   {
chris@3:     if(obj instanceof Group)
chris@3:     {
chris@3:       return ((Group)obj).id == this.id;
chris@3:     }
chris@3:     else
chris@3:     {
chris@3:       return false;
chris@3:     }
chris@3:   }
chris@3: 
chris@3:   public Article getArticle(long idx)
chris@3:     throws StorageBackendException
chris@3:   {
chris@3:     return StorageManager.current().getArticle(idx, this.id);
chris@3:   }
chris@3: 
chris@3:   public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
chris@3:     throws StorageBackendException
chris@3:   {
chris@3:     return StorageManager.current().getArticleHeads(this, first, last);
chris@3:   }
chris@3:   
chris@3:   public List<Long> getArticleNumbers()
chris@3:     throws StorageBackendException
chris@3:   {
chris@3:     return StorageManager.current().getArticleNumbers(id);
chris@3:   }
chris@3: 
chris@3:   public long getFirstArticleNumber()
chris@3:     throws StorageBackendException
chris@3:   {
chris@3:     return StorageManager.current().getFirstArticleNumber(this);
chris@3:   }
chris@3: 
chris@3:   public int getFlags()
chris@3:   {
chris@3:     return this.flags;
chris@3:   }
chris@3: 
chris@3:   public long getIndexOf(Article art)
chris@3:     throws StorageBackendException
chris@3:   {
chris@3:     return StorageManager.current().getArticleIndex(art, this);
chris@3:   }
chris@3: 
chris@3:   /**
chris@3:    * Returns the group id.
chris@3:    */
chris@3:   public long getInternalID()
chris@3:   {
chris@3:     assert id > 0;
chris@3: 
chris@3:     return id;
chris@3:   }
chris@3: 
chris@3:   public boolean isDeleted()
chris@3:   {
chris@3:     return (this.flags & DELETED) != 0;
chris@3:   }
chris@3: 
chris@3:   public boolean isMailingList()
chris@3:   {
chris@3:     return (this.flags & MAILINGLIST) != 0;
chris@3:   }
chris@3: 
chris@3:   public boolean isWriteable()
chris@3:   {
chris@3:     return true;
chris@3:   }
chris@3: 
chris@3:   public long getLastArticleNumber()
chris@3:     throws StorageBackendException
chris@3:   {
chris@3:     return StorageManager.current().getLastArticleNumber(this);
chris@3:   }
chris@3: 
chris@3:   public String getName()
chris@3:   {
chris@3:     return name;
chris@3:   }
chris@3: 
chris@3:   /**
chris@3:    * Performs this.flags |= flag to set a specified flag and updates the data
chris@3:    * in the JDBCDatabase.
chris@3:    * @param flag
chris@3:    */
chris@3:   public void setFlag(final int flag)
chris@3:   {
chris@3:     this.flags |= flag;
chris@3:   }
chris@3: 
chris@3:   public void setName(final String name)
chris@3:   {
chris@3:     this.name = name;
chris@3:   }
chris@3: 
chris@3:   /**
chris@3:    * @return Number of posted articles in this group.
chris@3:    * @throws java.sql.SQLException
chris@3:    */
chris@3:   public long getPostingsCount()
chris@3:     throws StorageBackendException
chris@3:   {
chris@3:     return StorageManager.current().getPostingsCount(this.name);
chris@3:   }
chris@3: 
chris@3:   /**
chris@3:    * Updates flags and name in the backend.
chris@3:    */
chris@3:   public void update()
chris@3:     throws StorageBackendException
chris@3:   {
chris@3:     StorageManager.current().update(this);
chris@3:   }
chris@3: 
chris@3: }