Add page that lists all parties.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 30 Jul 2012 08:58:05 +0000 (10:58 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 30 Jul 2012 08:58:05 +0000 (10:58 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java
src/main/java/net/pterodactylus/demoscenemusic/page/admin/ListPartiesPage.java [new file with mode: 0644]
src/main/resources/templates/admin.list-parties [new file with mode: 0644]
src/main/resources/templates/include/footer
src/main/webapp/WEB-INF/web.xml.template

index 967faa5..0251d62 100644 (file)
@@ -73,6 +73,10 @@ public class DataManager {
        @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Style> styleCreator = new StyleCreator();
 
+       /** The party object creator. */
+       @SuppressWarnings("synthetic-access")
+       private final ObjectCreator<Party> partyCreator = new PartyCreator();
+
        /** The {@link User} object creator. */
        @SuppressWarnings("synthetic-access")
        private final ObjectCreator<User> userCreator = new UserCreator();
@@ -364,6 +368,23 @@ public class DataManager {
        }
 
        /**
+        * Returns all tracks that were released at the party with the given ID.
+        *
+        * @param partyId
+        *            The ID of the party
+        * @return All tracks that were released at the given party
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<Track> getTracksByParty(String partyId) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "TRACKS");
+               query.addField(new Field("TRACKS.*"));
+               query.addJoin(new Join(JoinType.INNER, "PARTY_TRACKS", new Field("PARTY_TRACKS.TRACK"), new Field("TRACKS.ID")));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.PARTY", new StringParameter(partyId))));
+               return loadTrackProperties(database.getMultiple(query, trackCreator));
+       }
+
+       /**
         * Loads the properties for the given track.
         *
         * @param track
@@ -584,6 +605,19 @@ public class DataManager {
        }
 
        /**
+        * Returns all parties.
+        *
+        * @return All parties
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<Party> getAllParties() throws DatabaseException {
+               Query query = new Query(Type.SELECT, "PARTIES");
+               query.addField(new Field("PARTIES.*"));
+               return loadPartyProperties(database.getMultiple(query, partyCreator));
+       }
+
+       /**
         * Returns the user with the given name.
         *
         * @param username
@@ -1014,6 +1048,63 @@ public class DataManager {
        }
 
        /**
+        * {@link Party} implementation that loads additional information only on
+        * demand.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private class LazyParty extends DefaultParty {
+
+               /**
+                * Creates a new party.
+                *
+                * @param id
+                *            The ID of the party
+                */
+               public LazyParty(String id) {
+                       super(id);
+               }
+
+               //
+               // PARTY METHODS
+               //
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Collection<Track> getReleases() {
+                       if (!hasValue("releases")) {
+                               try {
+                                       getValue("releases", Collection.class).set(getTracksByParty(getId()));
+                               } catch (DatabaseException de1) {
+                                       throw new RuntimeException("Could not loaded Tracks for Party " + getId() + ".", de1);
+                               }
+                       }
+                       return super.getReleases();
+               }
+
+       }
+
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Party}
+        * objects.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private class PartyCreator implements ObjectCreator<Party> {
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Party createObject(ResultSet resultSet) throws SQLException {
+                       return new LazyParty(resultSet.getString("PARTIES.ID")).setName(resultSet.getString("PARTIES.NAME"));
+               }
+
+       }
+
+       /**
         * {@link User} implementation that retrieves some attributes (such as
         * {@link #getOpenIds()}) from the {@link DataManager} on demand.
         *
diff --git a/src/main/java/net/pterodactylus/demoscenemusic/page/admin/ListPartiesPage.java b/src/main/java/net/pterodactylus/demoscenemusic/page/admin/ListPartiesPage.java
new file mode 100644 (file)
index 0000000..3e24aef
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * DemosceneMusic - ListPartiesPage.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.demoscenemusic.page.admin;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import net.pterodactylus.demoscenemusic.core.Core;
+import net.pterodactylus.demoscenemusic.data.DefaultParty;
+import net.pterodactylus.demoscenemusic.data.Party;
+import net.pterodactylus.demoscenemusic.page.ServletRequest;
+import net.pterodactylus.util.database.DatabaseException;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.web.RedirectException;
+
+/**
+ * Page that lists all parties.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ListPartiesPage extends AdminBasePage {
+
+       /**
+        * Creates a new “list parties” page.
+        *
+        * @param core
+        *            The core
+        * @param templateContextFactory
+        *            The template context factory
+        * @param template
+        *            The template to render
+        */
+       public ListPartiesPage(Core core, TemplateContextFactory templateContextFactory, Template template) {
+               super(core, templateContextFactory, template, "admin.list-parties");
+       }
+
+       //
+       // BASEPAGE METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException {
+               super.processTemplate(templateContext, request);
+               try {
+                       List<Party> allParties = new ArrayList<Party>(getCore().getDataManager().getAllParties());
+                       Collections.sort(allParties, DefaultParty.NAME_YEAR_COMPARATOR);
+                       templateContext.set("parties", allParties);
+               } catch (DatabaseException de1) {
+                       throw new RuntimeException("Could not load parties.", de1);
+               }
+       }
+
+}
diff --git a/src/main/resources/templates/admin.list-parties b/src/main/resources/templates/admin.list-parties
new file mode 100644 (file)
index 0000000..91578fe
--- /dev/null
@@ -0,0 +1,15 @@
+<%include include/header title=="Manage Parties">
+
+<h1>Manage Parties</h1>
+
+<%foreach parties party>
+       <%first><ul><%/first>
+       <li><% party.name|html> <a href="admin.edit-party?id=<% party.id|html>">edit</a></li>
+       <%last></ul><%/last>
+<%/foreach>
+
+<h2>Add a New Party</h2>
+
+<%include include/admin.add-party>
+
+<%include include/footer>
\ No newline at end of file
index 3384777..3f77f6f 100644 (file)
@@ -4,6 +4,8 @@
 admin:
 (
 <a href="admin.artists">artists</a>
+|
+<a href="admin.list-parties">parties</a>
 )
 |
 <%/if>
index 4b5bd70..361bf04 100644 (file)
                        <param-name>admin.edit-track</param-name>
                        <param-value>net.pterodactylus.demoscenemusic.page.admin.EditTrackPage</param-value>
                </init-param>
+               <init-param>
+                       <param-name>admin.list-parties</param-name>
+                       <param-value>net.pterodactylus.demoscenemusic.page.admin.ListPartiesPage</param-value>
+               </init-param>
                <load-on-startup>0</load-on-startup>
        </servlet>