Package org.mbhcare.server

Source Code of org.mbhcare.server.PatientsServiceImpl

/**
*
*/
package org.mbhcare.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.mbhcare.client.LoginInfo;
import org.mbhcare.client.rpc.PatientsService;
import org.mbhcare.shared.FieldVerifier;
import org.mbhcare.shared.entity.EntityDisplay;
import org.mbhcare.shared.entity.Patient;
import org.mbhcare.shared.exception.ServerException;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.repackaged.com.google.common.collect.Lists;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
* @author MCOSTA
*
*/
@SuppressWarnings("serial")
public class PatientsServiceImpl extends RemoteServiceServlet
    implements PatientsService {

  @SuppressWarnings("unchecked")
  @Override
  public List<EntityDisplay> selectByFields(List<String> fields, List<String> values) throws ServerException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    List<EntityDisplay> result;
    if (!LoginServiceImpl.isLoggedIn()) {
      throw new IllegalArgumentException("User not loggin error.");
    } else {

      try {

        //        Query query = pm.newQuery(Employee.class);
        //        query.setFilter("salary > :salary && job == :job");
        //        query.setOrdering("salary asc");
        //        List<Employee> list = (List<Employee>) query.execute(5000, "ANALYST");

        String criterias = "userAccount == :userAccount";
        for(int i = 0; i< fields.size(); i++) {
          String field = fields.get(i);
          String value = values.get(i);
          if (!value.equals("")){
            criterias += " && " + field + ".startsWith(\"" + value + "\")";     
          }
        }
        //criterias += "1==1";

        Query query = pm.newQuery(Patient.class);
        String filter =criterias;

        query.setFilter(filter);
        //query.setFilter("userAccount == :userAccount  && userAccount == 'test@example.com'");
        //query.setOrdering("firstname asc");

        List<Patient> patients = (List<Patient>)query.execute(LoginServiceImpl.getAcccout());

        //Query query2 = pm.newQuery("select from " + Patient.class.getName() + " where firstname.toLowerCase() == 'hoa'");
        //List<Patient> patients = (List<Patient>)query2.execute();
        result = parseEntityDisplay(patients);
      } catch(Exception e) {
        throw new ServerException(e.toString());
      } finally {
        pm.close();
      }
    } 
    return result;
  }
 
  @Override
  public void delete(List<String> ids) throws ServerException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    String result = "";
   
    for(String id : ids) {
      try {
        Patient patient = pm.getObjectById(Patient.class, id);
     
        // Check if categories have any products
        //Query q = pm.newQuery(Product.class, "patient == '" + id + "'");
        //q.setResult("count(this)");
     
        //if((Integer)q.execute() == 0) {
          pm.deletePersistent(patient);
        //} else {
        //  throw new ServerException("Patient '" + id + "' not empty, can't delete it.");
        //}
      } catch(Exception e) {
        result = result + e.toString();
      }
    }
   
    if(!result.isEmpty()) {
      throw new ServerException(result);
    }
  }

  @Override
  public Patient selectById(String id) throws ServerException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    Patient patient;
   
    try {
      patient = pm.getObjectById(Patient.class, id);
    } catch(Exception e) {
      throw new ServerException(e.toString());
    }

    return patient;
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<Patient> select(String filter) throws ServerException {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    List<Patient> result;
   
    try {
      Query query = pm.newQuery(Patient.class);
     
      query.setFilter(filter);
      query.setOrdering("firstname asc");
     
      result = (List<Patient>) query.execute();
      result = Lists.newArrayList(pm.detachCopyAll(result));
    } 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();
    ArrayList<EntityDisplay> result = new ArrayList<EntityDisplay>();
    String numProducts = "0";
   
    try {
      Query query = pm.newQuery(Patient.class);
      query.setOrdering("firstname asc");
     
      if (!LoginServiceImpl.isLoggedIn()) {
        throw new IllegalArgumentException("User not loggin error.");
      } else {
        query.setFilter("userAccount == :userAccount");

        List<Patient> patients = (List<Patient>) query.execute(LoginServiceImpl.getAcccout());
        for(Patient patient: patients) {
          //        query = pm.newQuery(Product.class,
          //            "patient=='" + patient.getId() + "'");
          //        query.setResult("count(this)");
          //        numProducts = ((Integer)query.execute()).toString();
          //        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
          //        Date bday = new Date();
          //       
          //        bday.setTime(patient.getBirthdayInMili());
          //       
          //        String birthday = "";
          //        if (patient.getBirthdayInMili() > 0)
          //        {
          //          birthday = formatter.format(bday);
          //        }
          //        String birthday = "";
          //        if (patient.getBirthDay() != null)
          //        {
          //          birthday = Long.toString(patient.getBirthDay().getTime());
          //        }
          result.add(new EntityDisplay(new String[] {
              patient.getId(), patient.getLastName(), patient.getFirstName(),
              patient.getBirthYear(),
              //birthday, patient.getAddress(),patient.getPhone(),
              //patient.getEmail(),
              patient.getNotes()}));
        }
      }
    } catch(Exception e) {
      throw new ServerException(e.toString());
    } finally {
      pm.close();
 
   
    return result;
  }
 
  private List<EntityDisplay> parseEntityDisplay(List<Patient> patients) {
    ArrayList<EntityDisplay> result = new ArrayList<EntityDisplay>();
   
    for(Patient patient: patients) {
//      String birthday = "";
//      if (patient.getBirthDay() != null)
//      {
//        birthday = Long.toString(patient.getBirthDay().getTime());
//      }
      result.add(new EntityDisplay(new String[] {
          patient.getId(), patient.getLastName(),patient.getFirstName(),
          patient.getBirthYear(), //birthday,
          //patient.getAddress(),patient.getPhone(),
          //patient.getEmail(),
          patient.getNotes()
          }));
    }
   
    return result;
  }
  @Override
  public Patient save(Patient patient) throws ServerException {
    // Verify that the input is valid.
    if(!FieldVerifier.isValidString(patient.getFirstName()) &&
       !FieldVerifier.isValidString(patient.getLastName())) {
      throw new IllegalArgumentException("FieldVerifier error.");
    }
   
    if (!LoginServiceImpl.isLoggedIn()) {
      throw new IllegalArgumentException("User not loggin error.");
    } else {
      patient.setUserAccount(LoginServiceImpl.getAcccout());
    }
   
    PersistenceManager pm = PMF.get().getPersistenceManager();
   
    // Is a insert statement?
    if(patient.getId() == null) {
      Query query = pm.newQuery(Patient.class);
      query.setResult("count(this)");
      Integer count = (Integer)query.execute();
     
      // Register limit
      //if(count > 25)
      //  throw new ServerException("Limit of 25 rows exceeded.");
    }

    try {       
      pm.currentTransaction().begin();
      pm.makePersistent(patient);
      pm.currentTransaction().commit();
    } catch(Exception e) {
      pm.currentTransaction().rollback();
      throw new ServerException(e.toString());
    } finally {
      pm.close();
    }
   
    return patient;
  }


}
TOP

Related Classes of org.mbhcare.server.PatientsServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.