Permalink
Browse files

Initial commit of the common classes that are being used by the web f…

…ramework implementations.

Currently these classes are being used by the ZK and Click web framework implementations for the
books project.
  • Loading branch information...
1 parent d90a083 commit 9f59d652797803cb5b22a9c574befe1a60c2b93b @ccorsi committed Sep 20, 2010
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry combineaccessrules="false" kind="src" path="/BooksPersistenceBase"/>
+ <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
+ <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v6.0">
+ <attributes>
+ <attribute name="owner.project.facets" value="jst.utility"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/18">
+ <attributes>
+ <attribute name="owner.project.facets" value="jst.java"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>BooksCommonWeb</name>
+ <comment></comment>
+ <projects>
+ <project>BooksPersistenceBase</project>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.wst.common.project.facet.core.builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.wst.validation.validationbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
+ <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path:
+
@@ -0,0 +1,195 @@
+/**
+
+ BSD License
+
+ Copyright (c) 2010- , Claudio Corsi
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
+ in the documentation and/or other materials provided with the distribution.
+ * Neither the name of Claudio Corsi nor the names of its contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ */
+package org.books;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Properties;
+
+import org.books.proxy.BookProxy;
+import org.books.proxy.BookProxyPersistent;
+import org.valhalla.services.Service;
+import org.valhalla.services.ServiceException;
+import org.valhalla.services.ServiceFactory;
+
+import books.persistence.manager.DatabaseManager;
+import books.persistence.types.Book;
+import books.persistence.types.Gender;
+import books.persistence.types.Person;
+import books.persistence.types.PersonType;
+
+/**
+ * @author Claudio Corsi
+ *
+ */
+public class BooksManager {
+
+ private static DatabaseManager dbMgr;
+ private static BooksManager booksManager = new BooksManager();
+
+ private BooksManager() {
+ }
+
+ public static BooksManager getInstance() {
+ return booksManager;
+ }
+
+ public Map<String, String> getBooks() {
+ HashMap<String, String> booksMap = new HashMap<String, String>();
+
+ for (Book book : dbMgr.getBooks()) {
+ String title = book.getTitle(); // Key
+ System.out.println("Title: " + title);
+ if (title == null)
+ continue;
+ Collection<Person> persons = book.getPersons(PersonType.Author);
+ StringBuilder authors = new StringBuilder();
+ for (Person person : persons) {
+ if (authors.length() > 0) {
+ authors.append(", ");
+ }
+ authors.append(person.getFirstName());
+ for (String middleName : person.getMiddleNames()) {
+ authors.append(' ').append(middleName);
+ }
+ for (String lastName : person.getLastNames()) {
+ authors.append(' ').append(lastName);
+ }
+ }
+ booksMap.put(title, authors.toString());
+ }
+ return booksMap;
+ }
+
+ public Collection<BookProxy> getBookProxies() {
+ Collection<BookProxy> bookProxies = new LinkedList<BookProxy>();
+
+ for (Book book : dbMgr.getBooks()) {
+ bookProxies.add(new BookProxyPersistent(book));
+ }
+
+ return bookProxies;
+ }
+
+ public Map<String, String> getAuthors() {
+ return getPersonsMap(PersonType.Author);
+ }
+
+ // TODO: Find a better method name since this one is pretty lame....
+ public Map<String, String> getPersonsMap(PersonType type) {
+ HashMap<String, String> personsMap = new HashMap<String, String>();
+
+ for (Person person : dbMgr.getPersons(type)) {
+ // This if statement should NEVER true....
+ if (person.getFirstName() == null) {
+ System.out.println("Person instance first name is null!!!");
+ continue;
+ }
+ StringBuilder name = new StringBuilder(person.getFirstName());
+ System.out.println("Appended first name " + name.toString());
+ if (person.getMiddleNames() != null) {
+ for (String middleName : person.getMiddleNames()) {
+ name.append(' ').append(middleName);
+ }
+ }
+ if (person.getLastNames() != null) {
+ for (String lastName : person.getLastNames()) {
+ name.append(' ').append(lastName);
+ }
+ }
+ String value = dbMgr.getIdentifier(person);
+ personsMap.put(name.toString(), value);
+ }
+
+ return personsMap;
+ }
+
+ public void close() {
+ BooksManager.dbMgr.close();
+ }
+
+ public void init() {
+ // do nothing...
+ Service<DatabaseManager> service = ServiceFactory.createService(
+ "books.persistence.manager.databasemanager",
+ "books.persistence.manager.databasemanager",
+ DatabaseManager.class);
+
+ try {
+ dbMgr = service.create();
+
+ Properties properties = new Properties();
+
+ dbMgr.init(properties);
+ } catch (ServiceException e) {
+ e.printStackTrace();
+ dbMgr = null;
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Book createBook() {
+ return dbMgr.createBook();
+ }
+
+ public Collection<Person> getPersons(PersonType type) {
+ return dbMgr.getPersons(type);
+ }
+
+ public Gender createGender() {
+ return dbMgr.createGender();
+ }
+
+ public Person createPerson(PersonType type) {
+ return dbMgr.createPerson(type);
+ }
+
+ public Collection<Gender> getGenders() {
+ return dbMgr.getGenders();
+ }
+
+ public void startTransaction() {
+ dbMgr.beginTx();
+ }
+
+ public void commitTransaction() {
+ dbMgr.commitTx();
+ }
+
+ public void rollbackTransaction() {
+ // TODO: Add code to rollback transaction....
+ }
+
+ public String getIdentifier(Object object) {
+ return dbMgr.getIdentifier(object);
+ }
+
+ public <T> T getObject(String identifier) {
+ T object = dbMgr.getObject(identifier);
+ return object;
+ }
+
+}
Oops, something went wrong.

0 comments on commit 9f59d65

Please sign in to comment.