Example usage for org.hibernate.type StringType INSTANCE

List of usage examples for org.hibernate.type StringType INSTANCE

Introduction

In this page you can find the example usage for org.hibernate.type StringType INSTANCE.

Prototype

StringType INSTANCE

To view the source code for org.hibernate.type StringType INSTANCE.

Click Source Link

Usage

From source file:org.openeos.hibernate.internal.configurators.ListTypeUserType.java

License:Apache License

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    try {/*from  w  w  w .j a v a2s.com*/
        String value = (String) StringType.INSTANCE.nullSafeGet(rs, names, session, owner);
        Class<?> returnedClass = returnedClass();
        Constructor<?> constructor = returnedClass.getConstructor(String.class);
        Object retValue = constructor.newInstance(value);
        return retValue;
    } catch (NoSuchMethodException e) {
        throw new HibernateException(e);
    } catch (InstantiationException e) {
        throw new HibernateException(e);
    } catch (IllegalAccessException e) {
        throw new HibernateException(e);
    } catch (IllegalArgumentException e) {
        throw new HibernateException(e);
    } catch (InvocationTargetException e) {
        throw new HibernateException(e);
    }
}

From source file:org.openeos.hibernate.internal.configurators.ListTypeUserType.java

License:Apache License

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    String listValue = ((ListType) value).getValue();
    StringType.INSTANCE.nullSafeSet(st, listValue, index, session);
}

From source file:org.openeos.services.ui.form.abstractform.UIBeanSelectorProvider.java

License:Apache License

private Criterion buildSqlRestriction(String sqlRestriction, Object value) {
    try {//from  w w  w .ja  va  2 s.  c  om
        String result = sqlRestriction;
        int first = result.indexOf("@");
        List<Object> values = new ArrayList<Object>();
        List<Type> types = new ArrayList<Type>();
        while (first != -1) {
            String parameter = result.substring(first);
            int second = parameter.indexOf("@", 1);
            if (second == -1) {
                LOG.warn(
                        "There are a sql restrictoion not well formed, the sql restriction is: '{}' for bean class {} ",
                        sqlRestriction, beanClass.getName());
                break;
            }
            parameter = parameter.substring(0, second + 1);
            String parameterWithoutRim = parameter.substring(1, parameter.length() - 1);
            Map<String, Object> mvelContext = new HashMap<String, Object>();
            mvelContext.put("source", value);
            Object parameterValue = MVEL.eval(parameterWithoutRim, mvelContext);
            // TODO Make more types
            if (String.class.isAssignableFrom(parameterValue.getClass())) {
                values.add(parameterValue);
                types.add(StringType.INSTANCE);
            } else if (Boolean.class.isAssignableFrom(parameterValue.getClass())) {
                values.add(parameterValue);
                types.add(BooleanType.INSTANCE);
            } else {
                throw new UnsupportedOperationException();
            }
            result = result.substring(0, first) + "? " + result.substring(second + first + 1);
            first = result.indexOf("@");
        }
        Type[] t = new Type[types.size()];
        return Restrictions.sqlRestriction(result, values.toArray(), types.toArray(t));
    } catch (CompileException ex) {
        LOG.warn(String.format("Compiling validation expression '%s' of class '%s' has thrown an error: %s",
                sqlRestriction, beanClass.toString(), ex.getMessage()), ex);
        return Restrictions.sqlRestriction("0=1");
    }
}

From source file:org.openmrs.api.db.hibernate.PatientSearchCriteria.java

License:Mozilla Public License

/**
 * Utility method to add identifier expression to an existing criteria
 *
 * @param identifier/*from w w  w. ja va 2s.co  m*/
 * @param identifierTypes
 * @param matchIdentifierExactly
 * @param includeVoided true/false whether or not to included voided patients
 */
private Criterion prepareCriterionForIdentifier(String identifier, List<PatientIdentifierType> identifierTypes,
        boolean matchIdentifierExactly, boolean includeVoided) {

    identifier = HibernateUtil.escapeSqlWildcards(identifier, sessionFactory);
    Conjunction conjunction = Restrictions.conjunction();

    if (!includeVoided) {
        conjunction.add(Restrictions.eq("ids.voided", false));
    }
    // do the identifier restriction
    if (identifier != null) {
        // if the user wants an exact search, match on that.
        if (matchIdentifierExactly) {
            SimpleExpression matchIdentifier = Restrictions.eq("ids.identifier", identifier);
            if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) {
                matchIdentifier.ignoreCase();
            }
            conjunction.add(matchIdentifier);
        } else {
            AdministrationService adminService = Context.getAdministrationService();
            String regex = adminService
                    .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, "");
            String patternSearch = adminService
                    .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_PATTERN, "");

            // remove padding from identifier search string
            if (Pattern.matches("^\\^.{1}\\*.*$", regex)) {
                identifier = removePadding(identifier, regex);
            }

            if (org.springframework.util.StringUtils.hasLength(patternSearch)) {
                conjunction.add(splitAndGetSearchPattern(identifier, patternSearch));
            }
            // if the regex is empty, default to a simple "like" search or if
            // we're in hsql world, also only do the simple like search (because
            // hsql doesn't know how to deal with 'regexp'
            else if ("".equals(regex) || HibernateUtil.isHSQLDialect(sessionFactory)) {
                conjunction.add(getCriterionForSimpleSearch(identifier, adminService));
            }
            // if the regex is present, search on that
            else {
                regex = replaceSearchString(regex, identifier);
                conjunction.add(Restrictions.sqlRestriction("identifier regexp ?", regex, StringType.INSTANCE));
            }
        }
    }

    // TODO add a junit test for patientIdentifierType restrictions   

    // do the type restriction
    if (!CollectionUtils.isEmpty(identifierTypes)) {
        criteria.add(Restrictions.in("ids.identifierType", identifierTypes));
    }

    return conjunction;
}

From source file:org.openmrs.module.ugandaemrsync.api.dao.UgandaEMRSyncDao.java

License:Mozilla Public License

public List getFinalResults(List<String> columns, String finalQuery) {
    SQLQuery sqlQuery = getSession().createSQLQuery(finalQuery);
    for (String column : columns) {
        sqlQuery.addScalar(column, StringType.INSTANCE);
    }//w  ww  .  ja  va2s  . c  o  m
    return sqlQuery.list();
}

From source file:org.opennms.netmgt.dao.hibernate.CategoryDaoHibernate.java

License:Open Source License

/** {@inheritDoc} */
@Override//from   www. ja va2s.c  o m
public List<OnmsCategory> getCategoriesWithAuthorizedGroup(String groupName) {
    OnmsCriteria crit = new OnmsCriteria(OnmsCategory.class);
    crit.add(Restrictions.sqlRestriction(
            "{alias}.categoryId in (select cg.categoryId from category_group cg where cg.groupId = ?)",
            groupName, StringType.INSTANCE));
    return findMatching(crit);
}

From source file:org.opennms.netmgt.model.InetAddressUserType.java

License:Open Source License

@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final Object owner)
        throws HibernateException, SQLException {
    return InetAddressUtils.addr((String) StringType.INSTANCE.nullSafeGet(rs, names[0]));
}

From source file:org.opennms.netmgt.model.InetAddressUserType.java

License:Open Source License

@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index)
        throws HibernateException, SQLException {
    if (value == null) {
        StringType.INSTANCE.nullSafeSet(st, null, index);
    } else if (value instanceof InetAddress) {
        // Format the IP address into a uniform format
        StringType.INSTANCE.nullSafeSet(st, InetAddressUtils.str((InetAddress) value), index);
    } else if (value instanceof String) {
        try {/*from  w ww  .  ja  v a2s . c om*/
            // Format the IP address into a uniform format
            StringType.INSTANCE.nullSafeSet(st, InetAddressUtils.normalize((String) value), index);
        } catch (final IllegalArgumentException e) {
            // If the argument is not a valid IP address, then just pass it as-is. This
            // can occur of the query is performing a LIKE query (ie. '192.168.%').
            //
            // TODO: Add more validation of this string
            //
            StringType.INSTANCE.nullSafeSet(st, (String) value, index);
        }
    }
}

From source file:org.opennms.netmgt.model.NodeLabelSourceUserType.java

License:Open Source License

@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index)
        throws HibernateException, SQLException {
    if (value == null) {
        StringType.INSTANCE.nullSafeSet(st, null, index);
    } else if (value instanceof NodeLabelSource) {
        CharacterType.INSTANCE.nullSafeSet(st, ((NodeLabelSource) value).toString().charAt(0), index);
    } else if (value instanceof String) {
        for (NodeLabelSource type : NodeLabelSource.values()) {
            if (type.toString().equals(value)) {
                CharacterType.INSTANCE.nullSafeSet(st, type.toString().charAt(0), index);
            }/*from  w  w w  .  ja  v a 2 s. c  om*/
        }
    }
}

From source file:org.opennms.netmgt.model.NodeTypeUserType.java

License:Open Source License

@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index)
        throws HibernateException, SQLException {
    if (value == null) {
        StringType.INSTANCE.nullSafeSet(st, null, index);
    } else if (value instanceof NodeType) {
        CharacterType.INSTANCE.nullSafeSet(st, ((NodeType) value).toString().charAt(0), index);
    } else if (value instanceof String) {
        for (NodeType type : NodeType.values()) {
            if (type.toString().equals(value)) {
                CharacterType.INSTANCE.nullSafeSet(st, type.toString().charAt(0), index);
            }/*  w ww .  j a va  2  s .  c  o  m*/
        }
    }
}