Example usage for org.hibernate Session getIdentifier

List of usage examples for org.hibernate Session getIdentifier

Introduction

In this page you can find the example usage for org.hibernate Session getIdentifier.

Prototype

Serializable getIdentifier(Object object);

Source Link

Document

Return the identifier value of the given entity as associated with this session.

Usage

From source file:np.com.drose.studentmanagementsystem.dao.impl.ProjectDAOImpl.java

@Override
@Transactional/*from w w  w.java  2 s  . co m*/
public int insertProject(Project project) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.saveOrUpdate(project);
    tx.commit();
    Serializable id = session.getIdentifier(project);
    session.close();
    return (Integer) id;
}

From source file:np.com.drose.studentmanagementsystem.dao.impl.ProjectDAOImpl.java

@Transactional
@Override/*from  w w w . j  a v a2  s .  c  o m*/
public int updateRow(Project project) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    if (project.getProjectId() != null) {
        session.saveOrUpdate(project);
    }
    tx.commit();
    Serializable projectId = session.getIdentifier(project);
    session.close();
    return (Integer) projectId;
}

From source file:np.com.drose.studentmanagementsystem.dao.impl.RoleDAOImpl.java

@Transactional
@Override/*  w  ww . ja  va  2  s.c o m*/
public int insertRow(Role role) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.saveOrUpdate(role);
    Serializable id = session.getIdentifier(role);
    tx.commit();
    session.close();
    return (Integer) id;

}

From source file:np.com.drose.studentmanagementsystem.dao.impl.TeamDAOImpl.java

@Transactional
@Override//from   www .j a  v  a  2  s  .c o m
public int addTeam(Team team) {
    //open the session 
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    session.saveOrUpdate(team);
    tx.commit();
    Serializable id = session.getIdentifier(tx);
    session.close();
    return (Integer) id;
}

From source file:np.com.drose.studentmanagementsystem.dao.impl.UserDAOImpl.java

@Transactional
@Override//from   www. ja  v  a 2 s .c  o m
public int insertUser(User user) {
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    try {
        user.setPassword(Sha3Utils.hashPassword(user.getPassword()));
    } catch (Exception ex) {
        Logger.getLogger(UserDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    session.saveOrUpdate(user);
    tx.commit();
    Serializable id = session.getIdentifier(user);
    session.close();
    return (Integer) id;
}

From source file:org.apache.tapestry5.internal.hibernate.EntityPersistentFieldStrategyTest.java

License:Apache License

public void persistent_entity() {
    SampleEntity entity = new SampleEntity();
    SampleEntity restoredEntity = new SampleEntity();
    Session session = newMock(Session.class);
    EntityPersistentFieldStrategy strategy = new EntityPersistentFieldStrategy(session, null);

    expect(session.contains(entity)).andReturn(true);
    expect(session.getEntityName(entity)).andReturn("SampleEntity");
    expect(session.getIdentifier(entity)).andReturn(42);

    expect(session.get("SampleEntity", 42)).andReturn(restoredEntity);

    replay();/*from   ww  w  . ja v a2 s. co  m*/

    Object persisted = strategy.convertApplicationValueToPersisted(entity);

    assert persisted instanceof SessionRestorable;

    Object restored = strategy.convertPersistedToApplicationValue(persisted);

    assertSame(restored, restoredEntity);

    verify();

}

From source file:org.broadleafcommerce.openadmin.server.service.persistence.module.provider.BasicFieldPersistenceProvider.java

License:Apache License

@Override
public FieldProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) {
    if (!canHandlePersistence(populateValueRequest, instance)) {
        return FieldProviderResponse.NOT_HANDLED;
    }//from  www  .  jav a2  s.c  o  m
    boolean dirty = false;
    try {
        Property prop = populateValueRequest.getProperty();
        Object origValue = populateValueRequest.getFieldManager().getFieldValue(instance, prop.getName());
        switch (populateValueRequest.getMetadata().getFieldType()) {
        case BOOLEAN:
            boolean v = Boolean.valueOf(populateValueRequest.getRequestedValue());
            prop.setOriginalValue(String.valueOf(origValue));
            prop.setOriginalDisplayValue(prop.getOriginalValue());
            try {
                dirty = checkDirtyState(populateValueRequest, instance, v);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), v);
            } catch (IllegalArgumentException e) {
                char c = v ? 'Y' : 'N';
                dirty = checkDirtyState(populateValueRequest, instance, c);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), c);
            }
            break;
        case DATE:
            Date date = (Date) populateValueRequest.getFieldManager().getFieldValue(instance,
                    populateValueRequest.getProperty().getName());
            String oldValue = null;
            if (date != null) {
                oldValue = populateValueRequest.getDataFormatProvider().getSimpleDateFormatter().format(date);
            }
            prop.setOriginalValue(oldValue);
            prop.setOriginalDisplayValue(prop.getOriginalValue());
            dirty = !StringUtils.equals(oldValue, populateValueRequest.getRequestedValue());
            populateValueRequest.getFieldManager().setFieldValue(instance,
                    populateValueRequest.getProperty().getName(), populateValueRequest.getDataFormatProvider()
                            .getSimpleDateFormatter().parse(populateValueRequest.getRequestedValue()));
            break;
        case DECIMAL:
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
                prop.setOriginalDisplayValue(prop.getOriginalValue());
            }
            if (BigDecimal.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                DecimalFormat format = populateValueRequest.getDataFormatProvider().getDecimalFormatter();
                format.setParseBigDecimal(true);
                BigDecimal val = (BigDecimal) format.parse(populateValueRequest.getRequestedValue());
                dirty = checkDirtyState(populateValueRequest, instance, val);

                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), val);
                format.setParseBigDecimal(false);
            } else {
                Double val = populateValueRequest.getDataFormatProvider().getDecimalFormatter()
                        .parse(populateValueRequest.getRequestedValue()).doubleValue();
                dirty = checkDirtyState(populateValueRequest, instance, val);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), val);
            }
            break;
        case MONEY:
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
                prop.setOriginalDisplayValue(prop.getOriginalValue());
            }
            if (BigDecimal.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                DecimalFormat format = populateValueRequest.getDataFormatProvider().getDecimalFormatter();
                format.setParseBigDecimal(true);
                BigDecimal val = (BigDecimal) format.parse(populateValueRequest.getRequestedValue());
                dirty = checkDirtyState(populateValueRequest, instance, val);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), val);
                format.setParseBigDecimal(true);
            } else if (Double.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                Double val = populateValueRequest.getDataFormatProvider().getDecimalFormatter()
                        .parse(populateValueRequest.getRequestedValue()).doubleValue();
                dirty = checkDirtyState(populateValueRequest, instance, val);
                LOG.warn("The requested Money field is of type double and could result in a loss of precision."
                        + " Broadleaf recommends that the type of all Money fields are 'BigDecimal' in order to avoid"
                        + " this loss of precision that could occur.");
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), val);
            } else {
                DecimalFormat format = populateValueRequest.getDataFormatProvider().getDecimalFormatter();
                format.setParseBigDecimal(true);
                BigDecimal val = (BigDecimal) format.parse(populateValueRequest.getRequestedValue());
                dirty = checkDirtyState(populateValueRequest, instance, val);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), new Money(val));
                format.setParseBigDecimal(false);
            }
            break;
        case INTEGER:
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
                prop.setOriginalDisplayValue(prop.getOriginalValue());
            }
            if (int.class.isAssignableFrom(populateValueRequest.getReturnType())
                    || Integer.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                dirty = checkDirtyState(populateValueRequest, instance,
                        Integer.valueOf(populateValueRequest.getRequestedValue()));
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(),
                        Integer.valueOf(populateValueRequest.getRequestedValue()));
            } else if (byte.class.isAssignableFrom(populateValueRequest.getReturnType())
                    || Byte.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                dirty = checkDirtyState(populateValueRequest, instance,
                        Byte.valueOf(populateValueRequest.getRequestedValue()));
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(),
                        Byte.valueOf(populateValueRequest.getRequestedValue()));
            } else if (short.class.isAssignableFrom(populateValueRequest.getReturnType())
                    || Short.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                dirty = checkDirtyState(populateValueRequest, instance,
                        Short.valueOf(populateValueRequest.getRequestedValue()));
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(),
                        Short.valueOf(populateValueRequest.getRequestedValue()));
            } else if (long.class.isAssignableFrom(populateValueRequest.getReturnType())
                    || Long.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                dirty = checkDirtyState(populateValueRequest, instance,
                        Long.valueOf(populateValueRequest.getRequestedValue()));
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(),
                        Long.valueOf(populateValueRequest.getRequestedValue()));
            }
            break;
        case CODE:
            // **NOTE** We want to fall through in this case, do not break.
            setNonDisplayableValues(populateValueRequest);
        case STRING:
        case HTML_BASIC:
        case HTML:
        case EMAIL:
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
                prop.setOriginalDisplayValue(prop.getOriginalValue());
            }
            dirty = checkDirtyState(populateValueRequest, instance, populateValueRequest.getRequestedValue());
            populateValueRequest.getFieldManager().setFieldValue(instance,
                    populateValueRequest.getProperty().getName(), populateValueRequest.getRequestedValue());
            break;
        case FOREIGN_KEY: {
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
            }
            Serializable foreignInstance;
            if (StringUtils.isEmpty(populateValueRequest.getRequestedValue())) {
                foreignInstance = null;
            } else {
                if (SupportedFieldType.INTEGER.toString()
                        .equals(populateValueRequest.getMetadata().getSecondaryType().toString())) {
                    foreignInstance = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                            .retrieve(Class.forName(populateValueRequest.getMetadata().getForeignKeyClass()),
                                    Long.valueOf(populateValueRequest.getRequestedValue()));
                } else {
                    foreignInstance = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                            .retrieve(Class.forName(populateValueRequest.getMetadata().getForeignKeyClass()),
                                    populateValueRequest.getRequestedValue());
                }
            }

            if (Collection.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                Collection collection;
                try {
                    collection = (Collection) populateValueRequest.getFieldManager().getFieldValue(instance,
                            populateValueRequest.getProperty().getName());
                } catch (FieldNotAvailableException e) {
                    throw new IllegalArgumentException(e);
                }
                if (!collection.contains(foreignInstance)) {
                    collection.add(foreignInstance);
                    dirty = true;
                }
            } else if (Map.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                throw new IllegalArgumentException("Map structures are not supported for foreign key fields.");
            } else {
                dirty = checkDirtyState(populateValueRequest, instance, foreignInstance);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), foreignInstance);
            }
            break;
        }
        case ADDITIONAL_FOREIGN_KEY: {
            Serializable foreignInstance;
            if (StringUtils.isEmpty(populateValueRequest.getRequestedValue())) {
                foreignInstance = null;
            } else {
                if (SupportedFieldType.INTEGER.toString()
                        .equals(populateValueRequest.getMetadata().getSecondaryType().toString())) {
                    foreignInstance = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                            .retrieve(Class.forName(populateValueRequest.getMetadata().getForeignKeyClass()),
                                    Long.valueOf(populateValueRequest.getRequestedValue()));
                } else {
                    foreignInstance = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                            .retrieve(Class.forName(populateValueRequest.getMetadata().getForeignKeyClass()),
                                    populateValueRequest.getRequestedValue());
                }
            }

            // Best guess at grabbing the original display value
            String fkProp = populateValueRequest.getMetadata().getForeignKeyDisplayValueProperty();
            Object origDispVal = null;
            if (origValue != null) {
                if (AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY.equals(fkProp)) {
                    if (origValue instanceof AdminMainEntity) {
                        origDispVal = ((AdminMainEntity) origValue).getMainEntityName();
                    }
                } else {
                    origDispVal = populateValueRequest.getFieldManager().getFieldValue(origValue, fkProp);
                }
            }
            if (origDispVal != null) {
                prop.setOriginalDisplayValue(String.valueOf(origDispVal));
                Session session = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                        .getStandardEntityManager().unwrap(Session.class);
                prop.setOriginalValue(String.valueOf(session.getIdentifier(foreignInstance)));
            }

            if (Collection.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                Collection collection;
                try {
                    collection = (Collection) populateValueRequest.getFieldManager().getFieldValue(instance,
                            populateValueRequest.getProperty().getName());
                } catch (FieldNotAvailableException e) {
                    throw new IllegalArgumentException(e);
                }
                if (!collection.contains(foreignInstance)) {
                    collection.add(foreignInstance);
                    dirty = true;
                }
            } else if (Map.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                throw new IllegalArgumentException("Map structures are not supported for foreign key fields.");
            } else {
                dirty = checkDirtyState(populateValueRequest, instance, foreignInstance);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), foreignInstance);
            }
            break;
        }
        case ID:
            if (populateValueRequest.getSetId()) {
                switch (populateValueRequest.getMetadata().getSecondaryType()) {
                case INTEGER:
                    dirty = checkDirtyState(populateValueRequest, instance,
                            Long.valueOf(populateValueRequest.getRequestedValue()));
                    populateValueRequest.getFieldManager().setFieldValue(instance,
                            populateValueRequest.getProperty().getName(),
                            Long.valueOf(populateValueRequest.getRequestedValue()));
                    break;
                case STRING:
                    dirty = checkDirtyState(populateValueRequest, instance,
                            populateValueRequest.getRequestedValue());
                    populateValueRequest.getFieldManager().setFieldValue(instance,
                            populateValueRequest.getProperty().getName(),
                            populateValueRequest.getRequestedValue());
                    break;
                }
            }
            break;
        }
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    populateValueRequest.getProperty().setIsDirty(dirty);
    return FieldProviderResponse.HANDLED;
}

From source file:org.eclipse.emf.teneo.hibernate.resource.HibernateResource.java

License:Open Source License

/**
 * Override point, the default implementation will check if Hibernate
 * already assigned an id to an eobject. If so it is restored back to null.
 * See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=331953
 *//*from  w  ww.j  av  a  2  s.  c o m*/
protected void beforeSaveRollback() {
    // assigned ids
    Session session = sessionWrapper.getHibernateSession();
    for (EObject eobject : super.getNewEObjects()) {
        String entityName = session.getEntityName(eobject);
        String identifierName = getIdentifierName(eobject, session);
        Serializable id = session.getIdentifier(eobject);
        Criteria exists = session.createCriteria(entityName).add(Restrictions.eq(identifierName, id));
        if (exists.uniqueResult() == null) {
            rollbackID(eobject, identifierName);
        }
    }
}

From source file:org.jbpm.bpel.graph.scope.Scope.java

License:Open Source License

/**
 * Finds a handler for a fault thrown with associated data among the given handlers. For each
 * handler <i>h</i>://  w  w w.  ja  v  a2  s . c o m
 * <ul>
 * <li>if <i>h</i> has a faultVariable whose type matches the type of the fault data then <i>h</i>
 * is selected</li>
 * <li>otherwise if the type of fault data is a WSDL message which contains a single part defined
 * by an element, and <i>h</i> has a faultVariable whose type matches the type of the element
 * used to define the part then <i>h</i> is selected</li>
 * <li>otherwise if <i>h</i> does not specify a faultVariable then <i>h</i> is selected</li>
 * </ul>
 * @param dataType the type of fault data
 * @return the selected fault handler, or <code>null</code> if no handler is able to catch the
 * fault
 */
private static Catch selectCatch(Iterator handlerIt, VariableType dataType) {
    Catch selectedCatch = null;
    while (handlerIt.hasNext()) {
        Catch currentCatch = (Catch) handlerIt.next();
        if (traceEnabled)
            log.trace("examining catch: " + currentCatch);

        // look for a fault variable
        VariableDefinition variable = currentCatch.getFaultVariable();
        if (variable != null) {
            VariableType handlerType = variable.getType();
            if (handlerType.equals(dataType)) {
                // current handler matches exactly the fault type; it has the highest
                // priority, select it and stop the search
                selectedCatch = currentCatch;
                if (traceEnabled)
                    log.trace("selected catch with matching type: " + selectedCatch);
                break;
            } else if (dataType.isMessage() && handlerType.isElement()) {
                // fault data is a WSDL message, and the handler has an element
                // variable
                MessageType messageType;
                if (dataType instanceof MessageType)
                    messageType = (MessageType) dataType;
                else {
                    // reacquire proxy of the proper type
                    Session hbSession = JbpmContext.getCurrentJbpmContext().getSession();
                    messageType = (MessageType) hbSession.load(MessageType.class,
                            hbSession.getIdentifier(dataType));
                }

                QName elementName = WsdlUtil.getDocLitElementName(messageType.getMessage());
                // do the handler element and the message part element match?
                if (handlerType.getName().equals(elementName)) {
                    // current handler matches the element, select it but keep looking
                    // for a exact type match
                    selectedCatch = currentCatch;
                    if (traceEnabled)
                        log.trace("selected catch with matching element: " + selectedCatch);
                }
            }
        } else if (selectedCatch == null) {
            // this handler does not define a variable, select it only if no other
            // handler (of higher priority) has been selected
            selectedCatch = currentCatch;
            if (traceEnabled)
                log.trace("selected catch with no type:" + selectedCatch);
        }
    }
    return selectedCatch;
}

From source file:org.sparkcommerce.openadmin.server.service.persistence.module.provider.BasicFieldPersistenceProvider.java

License:Apache License

@Override
public FieldProviderResponse populateValue(PopulateValueRequest populateValueRequest, Serializable instance) {
    if (!canHandlePersistence(populateValueRequest, instance)) {
        return FieldProviderResponse.NOT_HANDLED;
    }/*w  ww.j a v  a2 s  .  co  m*/
    boolean dirty = false;
    try {
        Property prop = populateValueRequest.getProperty();
        Object origValue = populateValueRequest.getFieldManager().getFieldValue(instance, prop.getName());
        switch (populateValueRequest.getMetadata().getFieldType()) {
        case BOOLEAN:
            boolean v = Boolean.valueOf(populateValueRequest.getRequestedValue());
            prop.setOriginalValue(String.valueOf(origValue));
            prop.setOriginalDisplayValue(prop.getOriginalValue());
            try {
                dirty = checkDirtyState(populateValueRequest, instance, v);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), v);
            } catch (IllegalArgumentException e) {
                char c = v ? 'Y' : 'N';
                dirty = checkDirtyState(populateValueRequest, instance, c);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), c);
            }
            break;
        case DATE:
            Date date = (Date) populateValueRequest.getFieldManager().getFieldValue(instance,
                    populateValueRequest.getProperty().getName());
            String oldValue = null;
            if (date != null) {
                oldValue = populateValueRequest.getDataFormatProvider().getSimpleDateFormatter().format(date);
            }
            prop.setOriginalValue(oldValue);
            prop.setOriginalDisplayValue(prop.getOriginalValue());
            dirty = !StringUtils.equals(oldValue, populateValueRequest.getRequestedValue());
            populateValueRequest.getFieldManager().setFieldValue(instance,
                    populateValueRequest.getProperty().getName(), populateValueRequest.getDataFormatProvider()
                            .getSimpleDateFormatter().parse(populateValueRequest.getRequestedValue()));
            break;
        case DECIMAL:
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
                prop.setOriginalDisplayValue(prop.getOriginalValue());
            }
            if (BigDecimal.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                DecimalFormat format = populateValueRequest.getDataFormatProvider().getDecimalFormatter();
                format.setParseBigDecimal(true);
                BigDecimal val = (BigDecimal) format.parse(populateValueRequest.getRequestedValue());
                dirty = checkDirtyState(populateValueRequest, instance, val);

                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), val);
                format.setParseBigDecimal(false);
            } else {
                Double val = populateValueRequest.getDataFormatProvider().getDecimalFormatter()
                        .parse(populateValueRequest.getRequestedValue()).doubleValue();
                dirty = checkDirtyState(populateValueRequest, instance, val);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), val);
            }
            break;
        case MONEY:
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
                prop.setOriginalDisplayValue(prop.getOriginalValue());
            }
            if (BigDecimal.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                DecimalFormat format = populateValueRequest.getDataFormatProvider().getDecimalFormatter();
                format.setParseBigDecimal(true);
                BigDecimal val = (BigDecimal) format.parse(populateValueRequest.getRequestedValue());
                dirty = checkDirtyState(populateValueRequest, instance, val);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), val);
                format.setParseBigDecimal(true);
            } else if (Double.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                Double val = populateValueRequest.getDataFormatProvider().getDecimalFormatter()
                        .parse(populateValueRequest.getRequestedValue()).doubleValue();
                dirty = checkDirtyState(populateValueRequest, instance, val);
                LOG.warn("The requested Money field is of type double and could result in a loss of precision."
                        + " Spark recommends that the type of all Money fields are 'BigDecimal' in order to avoid"
                        + " this loss of precision that could occur.");
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), val);
            } else {
                DecimalFormat format = populateValueRequest.getDataFormatProvider().getDecimalFormatter();
                format.setParseBigDecimal(true);
                BigDecimal val = (BigDecimal) format.parse(populateValueRequest.getRequestedValue());
                dirty = checkDirtyState(populateValueRequest, instance, val);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), new Money(val));
                format.setParseBigDecimal(false);
            }
            break;
        case INTEGER:
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
                prop.setOriginalDisplayValue(prop.getOriginalValue());
            }
            if (int.class.isAssignableFrom(populateValueRequest.getReturnType())
                    || Integer.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                dirty = checkDirtyState(populateValueRequest, instance,
                        Integer.valueOf(populateValueRequest.getRequestedValue()));
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(),
                        Integer.valueOf(populateValueRequest.getRequestedValue()));
            } else if (byte.class.isAssignableFrom(populateValueRequest.getReturnType())
                    || Byte.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                dirty = checkDirtyState(populateValueRequest, instance,
                        Byte.valueOf(populateValueRequest.getRequestedValue()));
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(),
                        Byte.valueOf(populateValueRequest.getRequestedValue()));
            } else if (short.class.isAssignableFrom(populateValueRequest.getReturnType())
                    || Short.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                dirty = checkDirtyState(populateValueRequest, instance,
                        Short.valueOf(populateValueRequest.getRequestedValue()));
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(),
                        Short.valueOf(populateValueRequest.getRequestedValue()));
            } else if (long.class.isAssignableFrom(populateValueRequest.getReturnType())
                    || Long.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                dirty = checkDirtyState(populateValueRequest, instance,
                        Long.valueOf(populateValueRequest.getRequestedValue()));
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(),
                        Long.valueOf(populateValueRequest.getRequestedValue()));
            }
            break;
        case CODE:
            // **NOTE** We want to fall through in this case, do not break.
            setNonDisplayableValues(populateValueRequest);
        case STRING:
        case HTML_BASIC:
        case HTML:
        case EMAIL:
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
                prop.setOriginalDisplayValue(prop.getOriginalValue());
            }
            dirty = checkDirtyState(populateValueRequest, instance, populateValueRequest.getRequestedValue());
            populateValueRequest.getFieldManager().setFieldValue(instance,
                    populateValueRequest.getProperty().getName(), populateValueRequest.getRequestedValue());
            break;
        case FOREIGN_KEY: {
            if (origValue != null) {
                prop.setOriginalValue(String.valueOf(origValue));
            }
            Serializable foreignInstance;
            if (StringUtils.isEmpty(populateValueRequest.getRequestedValue())) {
                foreignInstance = null;
            } else {
                if (SupportedFieldType.INTEGER.toString()
                        .equals(populateValueRequest.getMetadata().getSecondaryType().toString())) {
                    foreignInstance = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                            .retrieve(Class.forName(populateValueRequest.getMetadata().getForeignKeyClass()),
                                    Long.valueOf(populateValueRequest.getRequestedValue()));
                } else {
                    foreignInstance = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                            .retrieve(Class.forName(populateValueRequest.getMetadata().getForeignKeyClass()),
                                    populateValueRequest.getRequestedValue());
                }
            }

            if (Collection.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                Collection collection;
                try {
                    collection = (Collection) populateValueRequest.getFieldManager().getFieldValue(instance,
                            populateValueRequest.getProperty().getName());
                } catch (FieldNotAvailableException e) {
                    throw new IllegalArgumentException(e);
                }
                if (!collection.contains(foreignInstance)) {
                    collection.add(foreignInstance);
                    dirty = true;
                }
            } else if (Map.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                throw new IllegalArgumentException("Map structures are not supported for foreign key fields.");
            } else {
                dirty = checkDirtyState(populateValueRequest, instance, foreignInstance);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), foreignInstance);
            }
            break;
        }
        case ADDITIONAL_FOREIGN_KEY: {
            Serializable foreignInstance;
            if (StringUtils.isEmpty(populateValueRequest.getRequestedValue())) {
                foreignInstance = null;
            } else {
                if (SupportedFieldType.INTEGER.toString()
                        .equals(populateValueRequest.getMetadata().getSecondaryType().toString())) {
                    foreignInstance = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                            .retrieve(Class.forName(populateValueRequest.getMetadata().getForeignKeyClass()),
                                    Long.valueOf(populateValueRequest.getRequestedValue()));
                } else {
                    foreignInstance = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                            .retrieve(Class.forName(populateValueRequest.getMetadata().getForeignKeyClass()),
                                    populateValueRequest.getRequestedValue());
                }
            }

            // Best guess at grabbing the original display value
            String fkProp = populateValueRequest.getMetadata().getForeignKeyDisplayValueProperty();
            Object origDispVal = null;
            if (origValue != null) {
                if (AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY.equals(fkProp)) {
                    if (origValue instanceof AdminMainEntity) {
                        origDispVal = ((AdminMainEntity) origValue).getMainEntityName();
                    }
                } else {
                    origDispVal = populateValueRequest.getFieldManager().getFieldValue(origValue, fkProp);
                }
            }
            if (origDispVal != null) {
                prop.setOriginalDisplayValue(String.valueOf(origDispVal));
                Session session = populateValueRequest.getPersistenceManager().getDynamicEntityDao()
                        .getStandardEntityManager().unwrap(Session.class);
                prop.setOriginalValue(String.valueOf(session.getIdentifier(foreignInstance)));
            }

            if (Collection.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                Collection collection;
                try {
                    collection = (Collection) populateValueRequest.getFieldManager().getFieldValue(instance,
                            populateValueRequest.getProperty().getName());
                } catch (FieldNotAvailableException e) {
                    throw new IllegalArgumentException(e);
                }
                if (!collection.contains(foreignInstance)) {
                    collection.add(foreignInstance);
                    dirty = true;
                }
            } else if (Map.class.isAssignableFrom(populateValueRequest.getReturnType())) {
                throw new IllegalArgumentException("Map structures are not supported for foreign key fields.");
            } else {
                dirty = checkDirtyState(populateValueRequest, instance, foreignInstance);
                populateValueRequest.getFieldManager().setFieldValue(instance,
                        populateValueRequest.getProperty().getName(), foreignInstance);
            }
            break;
        }
        case ID:
            if (populateValueRequest.getSetId()) {
                switch (populateValueRequest.getMetadata().getSecondaryType()) {
                case INTEGER:
                    dirty = checkDirtyState(populateValueRequest, instance,
                            Long.valueOf(populateValueRequest.getRequestedValue()));
                    populateValueRequest.getFieldManager().setFieldValue(instance,
                            populateValueRequest.getProperty().getName(),
                            Long.valueOf(populateValueRequest.getRequestedValue()));
                    break;
                case STRING:
                    dirty = checkDirtyState(populateValueRequest, instance,
                            populateValueRequest.getRequestedValue());
                    populateValueRequest.getFieldManager().setFieldValue(instance,
                            populateValueRequest.getProperty().getName(),
                            populateValueRequest.getRequestedValue());
                    break;
                }
            }
            break;
        }
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    populateValueRequest.getProperty().setIsDirty(dirty);
    return FieldProviderResponse.HANDLED;
}