List of usage examples for javax.persistence TypedQuery getSingleResult
X getSingleResult();
From source file:org.orcid.persistence.dao.impl.ProfileDaoImpl.java
@Override public Iso3166Country getCountry(String orcid) { TypedQuery<Iso3166Country> query = entityManager .createQuery("select iso2_country from ProfileEntity where orcid = :orcid", Iso3166Country.class); query.setParameter("orcid", orcid); return query.getSingleResult(); }
From source file:org.orcid.persistence.dao.impl.ProfileDaoImpl.java
@Override public boolean isLocked(String orcid) { TypedQuery<Boolean> query = entityManager .createQuery("select recordLocked from ProfileEntity where orcid = :orcid", Boolean.class); query.setParameter("orcid", orcid); Boolean result = query.getSingleResult(); return result; }
From source file:org.orcid.persistence.dao.impl.ProfileDaoImpl.java
@Override public boolean isDeactivated(String orcid) { TypedQuery<Date> query = entityManager .createQuery("select deactivationDate from ProfileEntity where orcid = :orcid", Date.class); query.setParameter("orcid", orcid); Date result = query.getSingleResult(); return (result == null) ? false : true; }
From source file:org.rhq.enterprise.server.cloud.TopologyManagerBean.java
@RequiredPermissions({ @RequiredPermission(Permission.MANAGE_SETTINGS), @RequiredPermission(Permission.MANAGE_INVENTORY) }) public Integer getResourceIdOfAgent(Subject subject, int agentId) { TypedQuery<Integer> query = entityManager .<Integer>createNamedQuery(Agent.QUERY_FIND_AGENT_RESOURCE_ID_AGENT_ID, Integer.class); query.setParameter("agentId", agentId); try {/*from w ww . j av a 2 s . c om*/ Integer resourceId = query.getSingleResult(); return resourceId; } catch (NoResultException nre) { return null; } }
From source file:org.sigmah.server.dao.impl.FileHibernateDAO.java
/** * {@inheritDoc}//from w ww . j a v a2 s. co m */ @Override public FileVersion getVersion(final Integer versionId) { final TypedQuery<FileVersion> query = em().createQuery("SELECT fv FROM FileVersion fv WHERE fv.id = :id", FileVersion.class); query.setParameter("id", versionId); return query.getSingleResult(); }
From source file:org.sigmah.server.dao.impl.FileHibernateDAO.java
/** * {@inheritDoc}/*www .j a v a 2 s . c o m*/ */ @Override public FileVersion getLastVersion(Integer fileId) { final TypedQuery<FileVersion> query = em().createQuery( "SELECT fv FROM FileVersion fv WHERE fv.parentFile.id = :fileId ORDER BY fv.versionNumber DESC", FileVersion.class); query.setParameter("fileId", fileId); query.setMaxResults(1); return query.getSingleResult(); }
From source file:org.sigmah.server.dao.impl.FileHibernateDAO.java
/** * Saves a new file.//from ww w . ja va 2 s . co m * * @param properties * The properties map of the uploaded file (see {@link FileUploadUtils}). * @param physicalName * The uploaded file content. * @param size * Size of the uploaded file. * @param authorId * The author id. * @return The id of the just saved file. * @throws IOException */ @Transactional protected Integer saveNewFile(Map<String, String> properties, String physicalName, int size, int authorId) throws IOException { final EntityManager em = em(); LOGGER.debug("[saveNewFile] New file."); // -------------------------------------------------------------------- // STEP 1 : saves the file. // -------------------------------------------------------------------- LOGGER.debug("[saveNewFile] Saves the new file."); final File file = new File(); // Gets the details of the name of the file. final String fullName = ValueResultUtils.normalizeFileName(properties.get(FileUploadUtils.DOCUMENT_NAME)); final int index = fullName.indexOf('.'); final String name = index > 0 ? fullName.substring(0, index) : fullName; final String extension = index > 0 && index < fullName.length() ? fullName.substring(index + 1) : null; file.setName(name); // Creates and adds the new version. file.addVersion(createVersion(1, name, extension, authorId, physicalName, size)); em.persist(file); // -------------------------------------------------------------------- // STEP 2 : gets the current value for this list of files. // -------------------------------------------------------------------- // Element id. final int elementId = ClientUtils.asInt(properties.get(FileUploadUtils.DOCUMENT_FLEXIBLE_ELEMENT), 0); // Project id. final int projectId = ClientUtils.asInt(properties.get(FileUploadUtils.DOCUMENT_PROJECT), 0); // Retrieving the current value final TypedQuery<Value> query = em.createQuery( "SELECT v FROM Value v WHERE v.containerId = :projectId and v.element.id = :elementId", Value.class); query.setParameter("projectId", projectId); query.setParameter("elementId", elementId); Value currentValue = null; try { currentValue = query.getSingleResult(); } catch (NoResultException nre) { // No current value } // -------------------------------------------------------------------- // STEP 3 : creates or updates the value with the new file id. // -------------------------------------------------------------------- // The value already exists, must update it. if (currentValue != null) { currentValue.setLastModificationAction('U'); // Sets the value (adds a new file id). currentValue.setValue(currentValue.getValue() + ValueResultUtils.DEFAULT_VALUE_SEPARATOR + String.valueOf(file.getId())); } // The value for this list of files doesn't exist already, must // create it. else { currentValue = new Value(); // Creation of the value currentValue.setLastModificationAction('C'); // Parent element final FlexibleElement element = em.find(FlexibleElement.class, elementId); currentValue.setElement(element); // Container currentValue.setContainerId(projectId); // Sets the value (one file id). currentValue.setValue(String.valueOf(file.getId())); } // Modifier final User user = em.find(User.class, authorId); currentValue.setLastModificationUser(user); // Last update date currentValue.setLastModificationDate(new Date()); // Saves or updates the new value. em.merge(currentValue); return file.getId(); }
From source file:org.sigmah.server.handler.GetValueHandler.java
/** * Gets a flexible element value from the database. * //from w ww . j a v a 2 s . c o m * @param cmd * {@link GetValue} command containing the flexible element class, its id, and the project id * @return a {@link ValueResult} object containing the value of the flexible element or containing {@code null} if * there is no value defined for this element. * @throws org.sigmah.shared.dispatch.CommandException */ @Override public ValueResult execute(final GetValue cmd, final UserExecutionContext context) throws CommandException { LOG.debug("Getting value object from the database for command: '{}'.", cmd); // Command result. final ValueResult valueResult = new ValueResult(); // Amendment String historyValue = null; if (cmd.getAmendmentId() != null) { final TypedQuery<Amendment> amedmentQuery = em() .createQuery("SELECT a FROM Amendment a WHERE a.id = :amendmentId", Amendment.class); amedmentQuery.setParameter("amendmentId", cmd.getAmendmentId()); Amendment amendment = amedmentQuery.getSingleResult(); final List<HistoryToken> tokens = amendment.getValues(); if (tokens != null) { for (final HistoryToken token : tokens) { if (token.getElementId().equals(cmd.getElementId())) { historyValue = token.getValue(); } } } } // -------------------------------------------------------------------- // STEP 1 : gets the string value (regardless of the element). // -------------------------------------------------------------------- final String valueFromDatabase; if (DefaultFlexibleElementDTO.ENTITY_NAME.equals(cmd.getElementEntityName())) { valueFromDatabase = findCurrentValueOfDefaultElement(cmd.getProjectId(), cmd.getElementId()); } else { valueFromDatabase = findCurrentValue(cmd.getProjectId(), cmd.getElementId()); } String valueAsString = valueFromDatabase; boolean isValueExisting = valueFromDatabase != null; // Overriding the value by the old one if we have to display an amendment if (historyValue != null) { valueAsString = historyValue; isValueExisting = true; valueResult.setAmendment(true); } // No value exists for the flexible element. if (!isValueExisting) { LOG.debug("No value for this flexible element #{}.", cmd.getElementId()); return valueResult; } // -------------------------------------------------------------------- // STEP 2 : gets the true values (depending of the element). // Can be a list of id with requires a sub-select query. // -------------------------------------------------------------------- Query query = null; String elementClassName = cmd.getElementEntityName(); ListableValue dto = null; Boolean isList = null; // Creates the sub-select query to get the true value. if (elementClassName.equals("element.TripletsListElement")) { LOG.debug("Case TripletsListElementDTO."); dto = new TripletValueDTO(); isList = true; query = em().createQuery("SELECT tv FROM TripletValue tv WHERE tv.id IN (:idsList)"); query.setParameter("idsList", ValueResultUtils.splitValuesAsInteger(valueAsString)); } else if (elementClassName.equals("element.IndicatorsListElement")) { LOG.debug("Case IndicatorsListElementDTO."); dto = new IndicatorsListValueDTO(); isList = true; query = em().createQuery("SELECT ilv FROM IndicatorsListValue ilv WHERE ilv.id.idList = :value"); query.setParameter("value", Integer.valueOf(valueAsString)); } else if (elementClassName.equals("element.BudgetDistributionElement")) { LOG.debug("Case BudgetDistributionElementDTO."); dto = new BudgetPartsListValueDTO(); isList = true; query = em().createQuery("SELECT bplv FROM BudgetPartsListValue bplv WHERE bplv.id = :value"); query.setParameter("value", Integer.valueOf(valueAsString)); } else if (elementClassName.equals("element.FilesListElement")) { LOG.debug("Case FilesListElementDTO."); dto = new FileDTO(); isList = true; query = em().createQuery("SELECT f FROM File f WHERE f.id IN (:idsList)"); query.setParameter("idsList", ValueResultUtils.splitValuesAsInteger(valueAsString)); } else if (elementClassName.equals("element.ReportListElement")) { LOG.debug("Case ReportListElementDTO."); dto = new ReportReference(); isList = true; query = em().createQuery("SELECT r FROM ProjectReport r WHERE r.id IN (:idList)"); query.setParameter("idList", ValueResultUtils.splitValuesAsInteger(valueAsString)); } else if (!(elementClassName.equals("element.MessageElement"))) { LOG.debug("Case others (but MessageElementDTO)."); dto = null; isList = false; } // -------------------------------------------------------------------- // STEP 3 : fill the command result with the values. // -------------------------------------------------------------------- // No value for this kind of elements. if (isList == null) { return valueResult; } // Multiple results case if (isList) { LOG.debug("Multiple values for the element #{}.", cmd.getElementId()); @SuppressWarnings("unchecked") final List<Object> objectsList = query.getResultList(); final List<ListableValue> serializablesList = new ArrayList<>(); for (Object o : objectsList) { serializablesList.add(mapper().map(o, dto)); } if (elementClassName.equals(FilesListElementDTO.ENTITY_NAME)) { for (final ListableValue value : serializablesList) { if (value instanceof FileDTO) { final FileDTO file = (FileDTO) value; for (final FileVersionDTO version : file.getVersions()) { version.setAvailable(fileStorageProvider.exists(version.getPath())); } } } } valueResult.setValuesObject(serializablesList); } // Single result case else { LOG.debug("Single value for the element #{}.", cmd.getElementId()); // A single value is always interpreted as a string. valueResult.setValueObject(valueAsString); } LOG.debug("Returned value = {}.", valueResult); return valueResult; }
From source file:org.sigmah.server.handler.GetValueHandler.java
private String findCurrentValue(int projectId, int elementId) { // Creates the query to get the value for the flexible element (as // string) in the Value table. final TypedQuery<String> valueQuery = em().createQuery( "SELECT v.value FROM Value v WHERE v.containerId = :projectId AND v.element.id = :elementId", String.class); valueQuery.setParameter("projectId", projectId); valueQuery.setParameter("elementId", elementId); String valueAsString;// w ww . j a v a 2s.co m // Executes the query and tests if a value exists for this flexible // element. try { valueAsString = valueQuery.getSingleResult(); if (StringUtils.isBlank(valueAsString)) { valueAsString = null; } } catch (NoResultException | ClassCastException e) { valueAsString = null; } return valueAsString; }
From source file:org.soulwing.credo.repository.JpaUserGroupMemberRepository.java
/** * {@inheritDoc}/* w w w . ja va2 s .co m*/ */ @Override public UserGroupMember findByGroupAndProfileId(String groupName, Long profileId) { TypedQuery<UserGroupMember> query = entityManager.createNamedQuery("findGroupMemberWithGroupAndProfileId", UserGroupMember.class); query.setParameter("groupName", groupName); query.setParameter("profileId", profileId); try { return query.getSingleResult(); } catch (NoResultException ex) { return null; } }