View Javadoc

1   package com.melloware.jukes.db.orm;
2   
3   import java.text.MessageFormat;
4   import java.util.ArrayList;
5   import java.util.Date;
6   import java.util.List;
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;
16  
17  /**
18   * References all relevant project data: the project description and a list of
19   * 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 AZ modifications 2009
24   */
25  public final class Catalog extends AbstractJukesObject {
26  
27     private static final Log LOG = LogFactory.getLog(Catalog.class);
28  
29     /**
30      * Holds a list of artists
31      */
32     private final List artists;
33  
34     // since this is static it will be used by all instances of MP3Tag created
35     /** EAL **/
36     public static Settings settings = null;
37  
38     /** EAL **/
39     // call this method only once to store the static settings variable from the
40     // MainModule code where its set
41     public static void setSettings(Settings aSettings) {
42        settings = aSettings;
43     }
44  
45     /**
46      * Constructs a <code>Catalog</code> with the given name and and loads all
47      * the artists.
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 ((Catalog.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 (Catalog.settings.isShowDefaultTree()) {
72              if (Catalog.settings.isShowEmptyNode()) { // AZ Show Empty Artist
73                                                        // Nodes
74                 this.artists = findAllArtistsIncludingEmpty();
75              } else {
76                 this.artists = findAllArtists();
77              }
78           } else { // AZ: set empty artists list
79              this.artists = new ArrayList();
80           }
81           LOG.debug("End findAllArtists");
82        }
83        LOG.debug("Artist Count = " + this.artists.size());
84     }
85  
86     /**
87      * Loads all artists (excluding empty ones) in the catalog and will return
88      * quickly if they are already cached.
89      * <p>
90      * @return the complete list of artists
91      */
92     public static List findAllArtists() {
93        LOG.debug("Loading ALL artists");
94        // now just get the cached artists
95        final String hql = ResourceUtils.getString("hql.artist.all");
96        return HibernateDao.findByQuery(hql);
97     }
98  
99     /**
100     * AZ Loads all artists (including empty ones) in the catalog and will return
101     * quickly if they are already cached.
102     * <p>
103     * @return the complete list of artists
104     */
105    public static List findAllArtistsIncludingEmpty() {
106       LOG.debug("Loading ALL artists (including empty ones)");
107       // now just get the cached artists
108       final String hql = ResourceUtils.getString("hql.artist.all.including.empty");
109       return HibernateDao.findByQuery(hql);
110    }
111 
112    /**
113     * Gets the artists.
114     * <p>
115     * @return Returns the artists.
116     */
117    public List getArtists() {
118       return this.artists;
119    }
120 
121    /**
122     * This date determines how the NEW flag is checked in the isNew() function.
123     */
124    @Override
125    public Date getAuditDate() {
126       return new Date();
127    }
128 
129    /*
130     * (non-Javadoc)
131     * @see com.melloware.jukes.gui.domain.Model#getCount()
132     */
133    @Override
134    public int getChildCount() {
135       if (artists == null) {
136          return 0;
137 
138       } else {
139          return artists.size();
140       }
141    }
142 
143    /*
144     * (non-Javadoc)
145     * @see com.melloware.jukes.db.orm.AbstractJukesObject#getCreatedDate()
146     */
147    @Override
148    public Date getCreatedDate() {
149       return new Date();
150    }
151 
152    /*
153     * (non-Javadoc)
154     * @see com.melloware.jukes.db.orm.AbstractJukesObject#getId()
155     */
156    @Override
157    public Long getId() {
158       return Long.valueOf(-99);
159    }
160 
161    /*
162     * (non-Javadoc)
163     * @see com.melloware.jukes.db.orm.AbstractJukesObject#getModifiedDate()
164     */
165    @Override
166    public Date getModifiedDate() {
167       return new Date();
168    }
169 
170    @Override
171    public String getName() {
172       return "Catalog";
173    }
174 
175    /*
176     * (non-Javadoc)
177     * @see com.melloware.jukes.db.orm.AbstractJukesObject#isValid()
178     */
179    @Override
180    public boolean isValid() {
181       return true;
182    }
183 
184 }