package welcome.server;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import welcome.client.GreetingService;
import welcome.shared.store.Article;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService {
// JDO datanucleus
private static PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PersistenceManager pm; // = pmfInstance.getPersistenceManager();
// public void persistStore(Store store) throws IllegalArgumentException {
//
// System.out.println("persist store with " + store.size() + " elements");
// pm = pmfInstance.getPersistenceManager();
// // store = pm.detachCopy(store);
// pm.makePersistent(store);
//
// // Should be push to the datastore (without it can be stored in
// // cache...)
// pm.evictAll();
// pm.close();
// }
// public Store getPersistStore(String storeName) throws
// IllegalArgumentException {
//
// Store store = null;
// pm = pmfInstance.getPersistenceManager();
// try {
// store = pm.getObjectById(Store.class, storeName);
//
// // System.out.println(store);
// } catch (Exception e) {
// System.out.println("Creating a new Sotre cause of : " + e.getMessage());
// store = new Store(storeName);
//
// } finally {
// pm.close();
// }
//
// // /////// BE CAREFUL :
// // the store contain an Arraylist . But JDO
// // convert it as an org.datanucleus.sco.backed.ArrayList (which is an
// // extension of classic java.util.ArrayList. The point is : when the RPC
// // would send the Store it will serialize it with all data member. But
// // an org.datanucleus.sco.backed.ArrayList can't be serialized ! So we
// // should first convert it as a simple ArrayList befor send it !
// // AN other solution is to store ArrayList as a blob object int the data
// // store. To do that use annotation
// // @Persistent(serialized = "true") in the store.
//
// ArrayList<Article> ar = new ArrayList<Article>(store.getSupplies());
// store.setSupplies(ar);
//
// // System.out.println(store.getSupplies().getClass().getName() +
// // "Before send by rpc " + store.getSupplies().toString());
// return store;
// }
@Override
public void addArticle(Article article) throws IllegalArgumentException {
System.out.println("persist article " + article);
pm = pmfInstance.getPersistenceManager();
pm.makePersistent(article);
pm.evictAll();
pm.close();
}
@Override
public Integer removeArticle(String articleName) throws IllegalArgumentException {
int count = 0;
pm = pmfInstance.getPersistenceManager();
Query query = pm.newQuery(Article.class, "(name == '" + articleName + "')");
@SuppressWarnings("unchecked")
List<Article> list = (List<Article>) query.execute();
for (Iterator<Article> it = list.iterator(); it.hasNext(); count++) {
pm.deletePersistent(it.next());
}
pm.evictAll();
pm.close();
return count;
}
@SuppressWarnings("unchecked")
@Override
public List<Article> getArticles() throws IllegalArgumentException {
pm = pmfInstance.getPersistenceManager();
Query query = pm.newQuery(Article.class);
// query.setResultClass(Article.class);
List<Article> list;
list = (List<Article>) query.execute();
ArrayList<Article> returnList = new ArrayList<Article>(list);
pm.close();
return returnList;
}
@Override
public void removeArticle(Long id) throws IllegalArgumentException {
pm = pmfInstance.getPersistenceManager();
pm.deletePersistent(pm.getObjectById(Article.class, id));
pm.evictAll();
pm.close();
}
@Override
public Article getArticle(Long id) throws IllegalArgumentException {
pm = pmfInstance.getPersistenceManager();
Article art = pm.getObjectById(Article.class, id);
pm.close();
return art;
}
@Override
public void updateArticle (Long id, String name, Integer amount)throws IllegalArgumentException
{
pm = pmfInstance.getPersistenceManager();
Article art = pm.getObjectById(Article.class, id);
art.setName(name);
art.setAmount(amount);
pm.makePersistent(art);
pm.evictAll();
pm.close();
}
}