/**
*
*/
package org.mbhcare.server;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import org.mbhcare.client.rpc.DiagnosisService;
import org.mbhcare.client.rpc.FetalsService;
import org.mbhcare.shared.FieldVerifier;
import org.mbhcare.shared.entity.EntityDisplay;
import org.mbhcare.shared.entity.Fetal;
import org.mbhcare.shared.entity.Patient;
import org.mbhcare.shared.exception.ServerException;
import com.google.appengine.repackaged.com.google.common.collect.Lists;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* @author Tuan Truong
*
*/
@SuppressWarnings("serial")
public class FetalsServiceImpl extends RemoteServiceServlet
implements FetalsService {
@Override
public void delete(List<String> ids) throws ServerException {
PersistenceManager pm = PMF.get().getPersistenceManager();
String result = "";
try {
//pm.currentTransaction().begin();
for(String id : ids) {
//delete fetal
Fetal fetal = pm.getObjectById(Fetal.class, id);
pm.deletePersistent(fetal);
}
//pm.currentTransaction().commit();
} catch(Exception e) {
pm.currentTransaction().rollback();
result = result + e.toString();
}
if(!result.isEmpty()) {
throw new ServerException(result);
}
}
@Override
public Fetal selectById(String id) throws ServerException {
PersistenceManager pm = PMF.get().getPersistenceManager();
Fetal fetal;
try {
fetal = pm.getObjectById(Fetal.class, id);
} catch(Exception e) {
throw new ServerException(e.toString());
}
return fetal;
}
@SuppressWarnings("unchecked")
@Override
public List<EntityDisplay> selectByPatient(String id) throws ServerException {
PersistenceManager pm = PMF.get().getPersistenceManager();
List<EntityDisplay> result;
try {
Query query = pm.newQuery(Fetal.class);
query.setFilter("patient == :patient");
query.setOrdering("time asc");
List<Fetal> fetal = (List<Fetal>)query.execute(id);
result = parseEntityDisplay(fetal);
} catch(Exception e) {
throw new ServerException(e.toString());
} finally {
pm.close();
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public List<EntityDisplay> selectDisplay() throws ServerException {
PersistenceManager pm = PMF.get().getPersistenceManager();
List<EntityDisplay> result;
try {
Query query = pm.newQuery(Fetal.class);
query.setOrdering("ngaykham desc");
List<Fetal> fetal = (List<Fetal>) query.execute();
result = parseEntityDisplay(fetal);
} catch(Exception e) {
throw new ServerException(e.toString());
} finally {
pm.close();
}
return result;
}
private List<EntityDisplay> parseEntityDisplay(List<Fetal> fetal) {
ArrayList<EntityDisplay> result = new ArrayList<EntityDisplay>();
for(Fetal item: fetal) {
String ngaykinhcuoi = "";
if (item.getNgaykinhcuoi() != null)
{
ngaykinhcuoi = Long.toString(item.getNgaykinhcuoi().getTime());
}
result.add(new EntityDisplay(new String[] {
item.getId(),
ngaykinhcuoi,
item.getTime().toString(),
"0"//String.valueOf(item.getDisplayType())
}));
}
return result;
}
private void ClearAll() throws ServerException{
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query1 = pm.newQuery(Fetal.class);
List<Fetal> fetal = (List<Fetal>) query1.execute();
try {
pm.deletePersistentAll(fetal);
} catch(Exception e) {
throw new ServerException(e.toString());
} finally {
pm.close();
}
}
@Override
public Fetal save(Fetal fetal) throws ServerException {
PersistenceManager pm = PMF.get().getPersistenceManager();
// // Is a insert statement?
// if(fetal.getId() == null) {
// Query query = pm.newQuery(Fetal.class);
// query.setResult("count(this)");
//
// }
try {
pm.currentTransaction().begin();
// TODO check this
if(fetal.getId() != null) {
Fetal temp = pm.getObjectById(Fetal.class, fetal.getId());
//temp.getFlags();
}
pm.makePersistent(fetal);
pm.currentTransaction().commit();
} catch(Exception e) {
pm.currentTransaction().rollback();
throw new ServerException(e.toString());
} finally {
pm.close();
}
return fetal;
}
}