package com.gadglet.admin.gadget.server;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.gadglet.admin.gadget.client.GadgetService;
import com.gadglet.admin.gadget.shared.FieldVerifier;
import com.gadglet.admin.gadget.shared.GadgetTransit;
import com.gadglet.data.utils.PMF;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class GadgetServiceImpl extends RemoteServiceServlet implements
GadgetService {
@Override
public String saveGadget(GadgetTransit gadget) throws IllegalArgumentException {
// Verify that the input is valid.
if (!FieldVerifier.isValidName(gadget.getName())) {
// If the input is not valid, throw an IllegalArgumentException back to
// the client.
throw new IllegalArgumentException(
"Name must be at least 4 characters long");
}
// save NEW gadget in DB
com.gadglet.data.Gadget storedGadget = new com.gadglet.data.Gadget(gadget);
if(storedGadget.add())
return "Gadget " + storedGadget.getName() + " was saved ";
else
throw new IllegalArgumentException("Error savind the Gadget");
}
@Override
public GadgetTransit getGadget(String gadgetName) throws IllegalArgumentException {
GadgetTransit gadget = null;
com.gadglet.data.Gadget storedGadget ;
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(com.gadglet.data.Gadget.class);
query.setFilter("name == nameParam");
query.declareParameters("String nameParam");
try
{
query.setUnique(true); //solve the one gadget issue
storedGadget = (com.gadglet.data.Gadget) query.execute(gadgetName);
if (storedGadget!=null )
gadget = storedGadget.toRpcGadegt();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
query.closeAll();
pm.close();
}
return gadget;
}
}