View Javadoc

1   package com.melloware.jukes.db.orm;
2   
3   import java.text.MessageFormat;
4   import java.util.Date;
5   import java.util.List;
6   import java.util.Arrays;
7   
8   import org.apache.commons.lang.StringUtils;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  import com.jgoodies.uif.util.ResourceUtils;
13  import com.melloware.jukes.db.HibernateDao;
14  import com.melloware.jukes.db.HibernateUtil;
15  import com.melloware.jukes.gui.tool.Settings; //AZ
16  
17  /**
18   * References all relevant project data: the project description and a
19   * list of artists. The artists refer to their child components.
20   * <p>
21   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
22   * @author Emil A. Lefkof III <info@melloware.com>
23   * @version 4.0
24   * AZ modifications 2009
25   */
26  public final class Catalog
27      extends AbstractJukesObject {
28  
29      private static final Log LOG = LogFactory.getLog(Catalog.class);
30  
31      /**
32       * Holds a list of artists
33       */
34      private final List artists;
35      
36      // since this is static it will be used by all instances of MP3Tag created
37      /**EAL **/public static Settings settings = null;
38      
39      /**EAL **/
40      // call this method only once to store the static settings variable from the MainModule code where its set
41      public static void setSettings(Settings aSettings) {
42         settings = aSettings;
43      } 
44      /**
45       * Constructs a <code>Catalog</code> with the given name and
46       * and loads all the artists.
47       *
48       * @param aFilter  the filter if there is one to apply
49       */
50      public Catalog(String aFilter) {
51      	super();
52          LOG.debug("Catalog created.");
53  
54          HibernateUtil.getSession().clear();
55          // if a filter was applied then use the filter HQL
56          if (StringUtils.isNotBlank(aFilter)) {
57          	final String resource;
58          	//AZ  Show Empty Artist Nodes
59          	if ((this.settings.isShowEmptyNode()) & (aFilter.indexOf("disc.")==-1)) {
60          	 resource = ResourceUtils.getString("hql.filter.artist.including.empty");
61          	 if (aFilter.startsWith("and")) {
62          		 aFilter = aFilter.substring(4);
63          	 }
64          	} else {
65         		 resource = ResourceUtils.getString("hql.filter.artist");	
66          	}
67              final String hql = MessageFormat.format(resource, new Object[] { aFilter });
68              this.artists = HibernateDao.findByQuery(hql);
69          } else {
70          	LOG.debug("Start findAllArtists");
71          	if (this.settings.isShowDefaultTree()) { 
72          		if (this.settings.isShowEmptyNode()) { //AZ  Show Empty Artist Nodes
73          		this.artists = findAllArtistsIncludingEmpty();}
74          		else {
75          			this.artists = findAllArtists();
76          		}
77          	}
78          	else { //AZ set empty artists list
79               this.artists = HibernateDao.findByQuery(ResourceUtils.getString("hql.artist.all"),1);
80               this.artists.clear();
81          	}
82          	LOG.debug("End findAllArtists");
83          }
84          LOG.debug("Artist Count = " + this.artists.size());
85      }
86  
87      /**
88       * Loads all artists (excluding empty ones) in the catalog and will return quickly 
89       * if they are already cached.
90       * <p>
91       * @return the complete list of artists
92       */
93      public static List findAllArtists() {
94          LOG.debug("Loading ALL artists");
95          // now just get the cached artists
96          final String hql = ResourceUtils.getString("hql.artist.all");
97          return HibernateDao.findByQuery(hql);
98      }
99  
100     /**AZ
101      * Loads all artists (including empty ones) in the catalog and will return quickly 
102      * if they are already cached.
103      * <p>
104      * @return the complete list of artists
105      */
106     public static List findAllArtistsIncludingEmpty() {
107         LOG.debug("Loading ALL artists (including empty ones)");
108         // now just get the cached artists
109         final String hql = ResourceUtils.getString("hql.artist.all.including.empty");
110         return HibernateDao.findByQuery(hql);
111     }
112     
113     /**
114      * Gets the artists.
115      * <p>
116      * @return Returns the artists.
117      */
118     public List getArtists() {
119         return this.artists;
120     }
121 
122     /**
123      * This date determines how the NEW flag is checked in the isNew() function.
124      */
125     public Date getAuditDate() {
126         return new Date();
127     }
128 
129     /* (non-Javadoc)
130      * @see com.melloware.jukes.gui.domain.Model#getCount()
131      */
132     public int getChildCount() {
133         if (artists == null) {
134             return 0;
135 
136         } else {
137             return artists.size();
138         }
139     }
140 
141     /* (non-Javadoc)
142      * @see com.melloware.jukes.db.orm.AbstractJukesObject#getCreatedDate()
143      */
144     public Date getCreatedDate() {
145         return new Date();
146     }
147 
148     /* (non-Javadoc)
149      * @see com.melloware.jukes.db.orm.AbstractJukesObject#getId()
150      */
151     public Long getId() {
152         return Long.valueOf(-99);
153     }
154 
155     /* (non-Javadoc)
156      * @see com.melloware.jukes.db.orm.AbstractJukesObject#getModifiedDate()
157      */
158     public Date getModifiedDate() {
159         return new Date();
160     }
161 
162     public String getName() {
163         return "Catalog";
164     }
165 
166     /* (non-Javadoc)
167      * @see com.melloware.jukes.db.orm.AbstractJukesObject#isValid()
168      */
169     public boolean isValid() {
170         return true;
171     }
172 
173 }