/** * A part of the facade over the JPA API * Find an entity with the given name and id in JPA * @param tenantId * @param entityName * @param id * @param properties - query hints used on the find * @return */ public Object find(Map<String, String> tenantId, String entityName, Object id, Map<String, Object> properties) { EntityManager em = getEmf().createEntityManager(tenantId); try { return em.find(getClass(entityName), id, properties); } finally { em.close(); } }
/** * A part of the facade over the JPA API * Find an entity with the given name and id in JPA * @param tenantId * @param entityName * @param id * @param properties - query hints used on the find * @return */ public Object find(Map<String, String> tenantId, String entityName, Object id, Map<String, Object> properties) { EntityManager em = getEmf().createEntityManager(tenantId); try { return em.find(getClass(entityName), id, properties); } finally { em.close(); } }
/** * Unmarshal entity. * * @param type the type of the entity to unmarshal * @param acceptedMediaType the accepted media type * @param in the input stream to unmarshal * @return the object * @throws JAXBException the JAXB exception */ public Object unmarshalEntity(String type, MediaType acceptedMediaType, InputStream in) throws JAXBException { if (JPARSLogger.isLoggableFinest()) { in = in.markSupported() ? in : new BufferedInputStream(in); // TODO: Make readlimit configurable. Some http servers allow http post size to be unlimited. // If this is the case and if an application is sending huge post requests while jpars log // level configured to finest, this readlimit might not be sufficient. in.mark(52428800); // (~50MB) JPARSLogger.entering(CLASS_NAME, "unmarshalEntity", in); } Object unmarshalled = unmarshal(getClass(type), acceptedMediaType, in); JPARSLogger.exiting(CLASS_NAME, "unmarshalEntity", new Object[] { unmarshalled.getClass().getName(), unmarshalled }); return unmarshalled; }
/** * Unmarshal entity. * * @param type the type of the entity to unmarshal * @param acceptedMediaType the accepted media type * @param in the input stream to unmarshal * @return the object * @throws JAXBException the JAXB exception */ public Object unmarshalEntity(String type, MediaType acceptedMediaType, InputStream in) throws JAXBException { if (JPARSLogger.isLoggableFinest(getSessionLog())) { in = in.markSupported() ? in : new BufferedInputStream(in); // TODO: Make readlimit configurable. Some http servers allow http post size to be unlimited. // If this is the case and if an application is sending huge post requests while jpars log // level configured to finest, this readlimit might not be sufficient. in.mark(52428800); // (~50MB) JPARSLogger.entering(getSessionLog(), CLASS_NAME, "unmarshalEntity", in); } Object unmarshalled = unmarshal(getClass(type), acceptedMediaType, in); JPARSLogger.exiting(getSessionLog(), CLASS_NAME, "unmarshalEntity", new Object[] { unmarshalled.getClass().getName(), unmarshalled }); return unmarshalled; }
/** * A part of the facade over the JPA API * Delete the given entity in JPA and commit the changes */ public void delete(Map<String, String> tenantId, String type, Object id) { EntityManager em = getEmf().createEntityManager(tenantId); try { transaction.beginTransaction(em); Object entity = em.find(getClass(type), id); if (entity != null) { em.remove(entity); } transaction.commitTransaction(em); } catch (RollbackException ex) { throw JPARSException.exceptionOccurred(ex); } catch (Exception ex) { transaction.rollbackTransaction(em); throw JPARSException.exceptionOccurred(ex); } finally { em.close(); } }
/** * A part of the facade over the JPA API * Delete the given entity in JPA and commit the changes */ public void delete(Map<String, String> tenantId, String type, Object id) { EntityManager em = getEmf().createEntityManager(tenantId); try { transaction.beginTransaction(em); Object entity = em.find(getClass(type), id); if (entity != null) { em.remove(entity); } transaction.commitTransaction(em); } catch (RollbackException ex) { throw JPARSException.exceptionOccurred(ex); } catch (Exception ex) { transaction.rollbackTransaction(em); throw JPARSException.exceptionOccurred(ex); } finally { em.close(); } }
/** * Gets the persistence context. * * @param persistenceUnit the persistence unit * @param baseURI the base uri * @param version the version * @param initializationProperties the initialization properties * @return the persistence context */ protected PersistenceContext getPersistenceContext(String persistenceUnit, String entityType, URI baseURI, String version, Map<String, Object> initializationProperties) { if (!isValidVersion(version)) { JPARSLogger.error("unsupported_service_version_in_the_request", new Object[] { version }); throw new IllegalArgumentException(); } PersistenceContext context = getPersistenceFactory().get(persistenceUnit, baseURI, version, initializationProperties); if (context == null) { JPARSLogger.error("jpars_could_not_find_persistence_context", new Object[] { persistenceUnit }); throw JPARSException.persistenceContextCouldNotBeBootstrapped(persistenceUnit); } if ((entityType != null) && (context.getClass(entityType) == null)) { JPARSLogger.error("jpars_could_not_find_class_in_persistence_unit", new Object[] { entityType, persistenceUnit }); throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(entityType, persistenceUnit); } return context; }
Object parameterValue; if (context != null) { parameterClass = context.getClass(param.getTypeName());
/** * Gets the persistence context. * * @param persistenceUnit the persistence unit * @param baseURI the base uri * @param version the version * @param initializationProperties the initialization properties * @return the persistence context */ protected PersistenceContext getPersistenceContext(String persistenceUnit, String entityType, URI baseURI, String version, Map<String, Object> initializationProperties) { if (!isValidVersion(version)) { JPARSLogger.error("unsupported_service_version_in_the_request", new Object[] { version }); throw new IllegalArgumentException(); } PersistenceContext context = getPersistenceFactory().get(persistenceUnit, baseURI, version, initializationProperties); if (context == null) { JPARSLogger.error("jpars_could_not_find_persistence_context", new Object[] { persistenceUnit }); throw JPARSException.persistenceContextCouldNotBeBootstrapped(persistenceUnit); } if ((entityType != null) && (context.getClass(entityType) == null)) { JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_class_in_persistence_unit", new Object[] { entityType, persistenceUnit }); throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(entityType, persistenceUnit); } return context; }
Object parameterValue; if (context != null) { parameterClass = context.getClass(param.getTypeName());
Class<?> clazz = getClass(entityName); ClassDescriptor descriptor = getServerSession().getClassDescriptor(clazz); DatabaseMapping mapping = descriptor.getMappingForAttributeName(attribute);
Class<?> clazz = getClass(entityName); ClassDescriptor descriptor = getServerSession().getClassDescriptor(clazz); DatabaseMapping mapping = descriptor.getMappingForAttributeName(attribute);
em = context.getEmf().createEntityManager(getMatrixParameters(uriInfo, persistenceUnit)); Object entity = em.find(context.getClass(type), entityId, getQueryParameters(uriInfo)); DatabaseSession serverSession = context.getServerSession(); ClassDescriptor descriptor = serverSession.getClassDescriptor(context.getClass(type)); if (descriptor == null) { throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(type, persistenceUnit);
em = context.getEmf().createEntityManager(getMatrixParameters(uriInfo, persistenceUnit)); Object entity = em.find(context.getClass(type), entityId, getQueryParameters(uriInfo)); DatabaseSession serverSession = context.getServerSession(); ClassDescriptor descriptor = serverSession.getClassDescriptor(context.getClass(type)); if (descriptor == null) { throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(type, persistenceUnit);