示例#1
0
 private EntityManager lookupEntityManager() {
   FacesContext facesContext = FacesContext.getCurrentInstance();
   PersistenceService persistenceService =
       facesContext
           .getApplication()
           .evaluateExpressionGet(facesContext, "#{persistenceService}", PersistenceService.class);
   return persistenceService.getEntityManager();
 }
  @Test
  public void persistenceMultiHibernate() {
    long start = System.currentTimeMillis();

    for (int i = 0; i < 1000000; i++) {
      try {
        persistenceService.saveNewCarData(
            new CarDataEntity("" + i, i, new Date(), null, null, 2, false),
            "123.456.789.000",
            "fake user agent");
      } catch (MotorpastPersistenceException e1) {
        e1.printStackTrace();
      }

      try {
        Thread.sleep(20);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    long end = System.currentTimeMillis();

    System.out.println("db fill took: " + (end - start) + "ms");
  }
  public ResetPassword create(ResetPassword resetPassword) {
    return persistenceService.process(
        new ProcessFunc<ResetPassword, ResetPassword>(resetPassword) {
          @Override
          public ResetPassword process() {
            try {
              delete(input.getHandle(), conn);

              PreparedStatement insert =
                  conn.prepareStatement(
                      "INSERT INTO reset_password (handle, nonce, created) VALUES (?,?,?)");

              input.setCreated(new Date());
              insert.setString(1, input.getHandle());
              insert.setString(2, input.getNonce());
              insert.setTimestamp(3, new Timestamp(input.getCreated().getTime()));

              insert.execute();
            } catch (SQLException e) {
              throw new GeneralException(e);
            }

            return input;
          }
        });
  }
 /**
  * Get method for retrieving a collection of PrivacyPreferences instance in XML format.
  *
  * @return an instance of PrivacyPreferencessConverter
  */
 @Secured({"ROLE_ADMIN", "ADMIN_ACCESS"})
 @GET
 @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 public PrivacyPreferencessConverter get(
     @QueryParam("start") @DefaultValue("0") int start,
     @QueryParam("max") @DefaultValue("10") int max,
     @QueryParam("expandLevel") @DefaultValue("1") int expandLevel,
     @QueryParam("query") @DefaultValue("SELECT e FROM PrivacyPreferences e") String query) {
   PersistenceService persistenceSvc = PersistenceService.getInstance();
   try {
     persistenceSvc.beginTx();
     return new PrivacyPreferencessConverter(
         getEntities(start, max, query), uriInfo.getAbsolutePath(), expandLevel);
   } finally {
     persistenceSvc.commitTx();
     persistenceSvc.close();
   }
 }
 /**
  * Persist the given entity.
  *
  * @param entity the entity to persist
  */
 protected void createEntity(PrivacyPreferences entity) {
   entity.setId(null);
   EntityManager em = PersistenceService.getInstance().getEntityManager();
   em.persist(entity);
   UbikeUser owner = entity.getOwner();
   if (owner != null) {
     owner.setPreferences(entity);
   }
 }
 /**
  * Post method for creating an instance of PrivacyPreferences using XML as the input format.
  *
  * @param data an PrivacyPreferencesConverter entity that is deserialized from an XML stream
  * @return an instance of PrivacyPreferencesConverter
  */
 @POST
 @Consumes({"application/xml", "application/json"})
 public Response post(PrivacyPreferencesConverter data) {
   PersistenceService persistenceSvc = PersistenceService.getInstance();
   try {
     persistenceSvc.beginTx();
     EntityManager em = persistenceSvc.getEntityManager();
     PrivacyPreferences entity = data.resolveEntity(em);
     createEntity(data.resolveEntity(em));
     persistenceSvc.commitTx();
     return Response.created(uriInfo.getAbsolutePath().resolve(entity.getId() + "/")).build();
   } finally {
     persistenceSvc.close();
   }
 }
示例#7
0
  public List<Integer> getProfileIdsForOrganization(int organizationId) {
    return persistenceService.process(
        new RetrieveFunc<List<Integer>>(organizationId) {
          @Override
          protected List<Integer> retrieveConcrete() throws SQLException {
            PreparedStatement select =
                conn.prepareStatement(
                    "SELECT id FROM profile WHERE organization=? ORDER BY id DESC");
            select.setInt(1, input);

            ResultSet result = select.executeQuery();

            List<Integer> ids = new ArrayList<Integer>();
            while (result.next()) {
              ids.add(result.getInt(1));
            }

            return ids;
          }
        });
  }
  public ResetPassword getByCode(String code) {
    return persistenceService.process(
        new ProcessFunc<String, ResetPassword>(code) {
          @Override
          public ResetPassword process() {
            try {
              String query = "SELECT handle, created FROM reset_password WHERE nonce=?";

              PreparedStatement statement = conn.prepareStatement(query);
              statement.setString(1, input);

              ResultSet rs = statement.executeQuery();

              if (rs.next()) {
                return new ResetPassword(rs.getString(1), input, rs.getTimestamp(2));
              } else {
                return null;
              }
            } catch (SQLException e) {
              throw new GeneralException(e);
            }
          }
        });
  }
示例#9
0
 public Profile createProfile(Profile profile) {
   return persistenceService.process(new CreateProfileFunc(profile, this));
 }
示例#10
0
 public ProfileSummary getProfileByOrganization(int organizationId, int userId) {
   return persistenceService.process(
       new RetrieveProfileForOrganizationFunc(organizationId, userId));
 }
示例#11
0
  public ProfileSummary create(Profile profile) {
    Profile savedProfile = persistenceService.process(new CreateProfileFunc(profile, this));

    return getProfileSummary(savedProfile.getId());
  }
示例#12
0
 public ProfileSummary getProfileSummary(Integer profileId) {
   return persistenceService.process(new RetrieveProfileSummaryFunc(profileId));
 }
示例#13
0
 public ProfileSummary getProfileSummary(Integer profileId, Connection conn) {
   return persistenceService.process(conn, new RetrieveProfileSummaryFunc(profileId));
 }
示例#14
0
 @Override
 public Profile get(int id) {
   return persistenceService.process(new RetrieveProfileFunc(id));
 }
示例#15
0
 @Override
 public Profile get(int id, Connection connection) {
   return persistenceService.process(connection, new RetrieveProfileFunc(id));
 }
 public void delete(String handle) {
   persistenceService.process(deleteFunc(handle));
 }
示例#17
0
 public void delete(int id) {
   persistenceService.process(new DeleteProfileByIdFunc(id));
 }
示例#18
0
 /** Constructor. */
 public UserServiceImpl() {
   em = PersistenceService.getEntityManager();
 }
 /**
  * @param id
  * @return
  */
 public PrivacyPreferences getById(long id) {
   EntityManager em = PersistenceService.getInstance().getEntityManager();
   return em.find(PrivacyPreferences.class, id);
 }
 /**
  * Returns all the entities associated with this resource.
  *
  * @return a collection of PrivacyPreferences instances
  */
 protected Collection<PrivacyPreferences> getEntities(int start, int max, String query) {
   EntityManager em = PersistenceService.getInstance().getEntityManager();
   return em.createQuery(query).setFirstResult(start).setMaxResults(max).getResultList();
 }
示例#21
0
 public Profile update(Profile profile) {
   return persistenceService.process(new UpdateProfileFunc(profile));
 }
 public void delete(String handle, Connection conn) {
   persistenceService.process(conn, deleteFunc(handle));
 }