List of usage examples for org.apache.commons.beanutils PropertyUtils isWriteable
public static boolean isWriteable(Object bean, String name)
Return true
if the specified property name identifies a writeable property on the specified bean; otherwise, return false
.
For more details see PropertyUtilsBean
.
From source file:org.kuali.rice.kns.web.ui.SectionBridge.java
/** * Builds a list of Rows with Fields of type containers for a many section. * /* ww w . ja va 2 s.c o m*/ * @param s The Section containing the Collection/Container. * @param collectionDefinition The DD definition of the Collection. * @param o The BusinessObject from which the Container/Collection will be populated. * @param m The Maintainable for the BO (needed by some methods called on FieldBridge, FieldUtils etc.) * @param displayedFieldNames * @param containerRowErrorKey The error key for the Container/Collection. * @param parents * @param hideAdd Should the add line be added to the Container/Collection? * @param numberOfColumns In how many columns in the UI will the fields in the Container/Collection be shown? * @return */ public static final List<Row> getContainerRows(Section s, CollectionDefinitionI collectionDefinition, BusinessObject o, Maintainable m, Maintainable oldMaintainable, List<String> displayedFieldNames, Set<String> conditionallyRequiredMaintenanceFields, StringBuffer containerRowErrorKey, String parents, boolean hideAdd, int numberOfColumns, Inquirable inquirable) { List<Row> containerRows = new ArrayList<Row>(); List<Field> collFields = new ArrayList<Field>(); String collectionName = collectionDefinition.getName(); // add the toggle inactive record display button for the collection if (m != null && Inactivatable.class.isAssignableFrom(collectionDefinition.getBusinessObjectClass()) && StringUtils.isBlank(parents)) { addShowInactiveButtonField(s, collectionName, !m.getShowInactiveRecords(collectionName)); } if (inquirable != null && Inactivatable.class.isAssignableFrom(collectionDefinition.getBusinessObjectClass()) && StringUtils.isBlank(parents)) { addShowInactiveButtonField(s, collectionName, !inquirable.getShowInactiveRecords(collectionName)); } // first need to populate the containerRows with the "new" form if available if (!hideAdd) { List<Field> newFormFields = new ArrayList<Field>(); if (collectionDefinition.getIncludeAddLine()) { newFormFields = FieldBridge.getNewFormFields(collectionDefinition, o, m, displayedFieldNames, conditionallyRequiredMaintenanceFields, containerRowErrorKey, parents, hideAdd, numberOfColumns); } else if (collectionDefinition instanceof MaintainableCollectionDefinition) { MaintainableCollectionDefinition mcd = (MaintainableCollectionDefinition) collectionDefinition; if (FieldUtils.isCollectionMultipleLookupEnabled(mcd)) { //do just the top line for collection if add is not allowed newFormFields = FieldBridge.constructContainerField(collectionDefinition, parents, o, hideAdd, numberOfColumns, mcd.getName(), new ArrayList<Field>()); } } if (null != newFormFields) { containerRows.add(new Row(newFormFields)); } } Collection<? extends CollectionDefinitionI> collections = collectionDefinition.getCollections(); for (CollectionDefinitionI collection : collections) { int subCollectionNumberOfColumn = numberOfColumns; if (collectionDefinition instanceof InquiryCollectionDefinition) { InquiryCollectionDefinition icd = (InquiryCollectionDefinition) collection; if (icd.getNumberOfColumns() != null) { subCollectionNumberOfColumn = icd.getNumberOfColumns(); } } // no colNum for add rows containerRows.addAll(getContainerRows(s, collection, o, m, oldMaintainable, displayedFieldNames, conditionallyRequiredMaintenanceFields, containerRowErrorKey, parents + collectionDefinition.getName() + ".", true, subCollectionNumberOfColumn, inquirable)); } // then we need to loop through the existing collection and add those fields Collection<? extends FieldDefinitionI> collectionFields = collectionDefinition.getFields(); // get label for collection String collectionLabel = getDataDictionaryService().getCollectionLabel(o.getClass(), collectionDefinition.getName()); // retrieve the summary label either from the override or from the DD String collectionElementLabel = collectionDefinition.getSummaryTitle(); if (StringUtils.isEmpty(collectionElementLabel)) { collectionElementLabel = getDataDictionaryService().getCollectionElementLabel(o.getClass().getName(), collectionDefinition.getName(), collectionDefinition.getBusinessObjectClass()); } boolean translateCodes = getMaintenanceDocumentDictionaryService().translateCodes(o.getClass()); if (o != null) { if (PropertyUtils.isWriteable(o, collectionDefinition.getName()) && ObjectUtils.getPropertyValue(o, collectionDefinition.getName()) != null) { Object obj = ObjectUtils.getPropertyValue(o, collectionName); Object oldObj = null; if (oldMaintainable != null && oldMaintainable.getBusinessObject() != null) { oldObj = ObjectUtils.getPropertyValue(oldMaintainable.getBusinessObject(), collectionName); } if (obj instanceof List) { Map summaryFields = new HashMap(); boolean hidableRowsPresent = false; for (int i = 0; i < ((List) obj).size(); i++) { BusinessObject lineBusinessObject = (BusinessObject) ((List) obj).get(i); if (lineBusinessObject instanceof PersistableBusinessObject) { ((PersistableBusinessObject) lineBusinessObject).refreshNonUpdateableReferences(); } /* * Handle display of inactive records. The old maintainable is used to compare the old side (if it exists). If the row should not be displayed, it is set as * hidden and will be handled in the maintenance rowDisplay.tag. */ boolean setRowHidden = false; BusinessObject oldLineBusinessObject = null; if (oldObj != null && ((List) oldObj).size() > i) { oldLineBusinessObject = (BusinessObject) ((List) oldObj).get(i); } if (lineBusinessObject instanceof Inactivatable && !((Inactivatable) lineBusinessObject).isActive()) { if (m != null) { // rendering a maint doc if (!hidableRowsPresent) { hidableRowsPresent = isRowHideableForMaintenanceDocument(lineBusinessObject, oldLineBusinessObject); } setRowHidden = isRowHiddenForMaintenanceDocument(lineBusinessObject, oldLineBusinessObject, m, collectionName); } if (inquirable != null) { // rendering an inquiry screen if (!hidableRowsPresent) { hidableRowsPresent = isRowHideableForInquiry(lineBusinessObject); } setRowHidden = isRowHiddenForInquiry(lineBusinessObject, inquirable, collectionName); } } collFields = new ArrayList<Field>(); List<String> duplicateIdentificationFieldNames = new ArrayList<String>(); //We only need to do this if the collection definition is a maintainable collection definition, //don't need it for inquiry collection definition. if (collectionDefinition instanceof MaintainableCollectionDefinition) { Collection<MaintainableFieldDefinition> duplicateFieldDefs = ((MaintainableCollectionDefinition) collectionDefinition) .getDuplicateIdentificationFields(); for (MaintainableFieldDefinition eachFieldDef : duplicateFieldDefs) { duplicateIdentificationFieldNames.add(eachFieldDef.getName()); } } for (FieldDefinitionI collectionField : collectionFields) { // construct Field UI object from definition Field collField = FieldUtils.getPropertyField( collectionDefinition.getBusinessObjectClass(), collectionField.getName(), false); if (translateCodes) { FieldUtils.setAdditionalDisplayPropertyForCodes(lineBusinessObject.getClass(), collField.getPropertyName(), collField); } FieldBridge.setupField(collField, collectionField, conditionallyRequiredMaintenanceFields); setPrimaryKeyFieldsReadOnly(collectionDefinition.getBusinessObjectClass(), collField); //If the duplicateIdentificationFields were specified in the maint. doc. DD, we'll need //to set the fields to be read only as well, in addition to the primary key fields. if (duplicateIdentificationFieldNames.size() > 0) { setDuplicateIdentificationFieldsReadOnly(collField, duplicateIdentificationFieldNames); } FieldUtils.setInquiryURL(collField, lineBusinessObject, collectionField.getName()); // save the simple property name String name = collField.getPropertyName(); // prefix name for multi line (indexed) collField.setPropertyName(collectionDefinition.getName() + "[" + (new Integer(i)).toString() + "]." + collField.getPropertyName()); // commenting out codes for sub-collections show/hide for now // subCollField.setContainerName(collectionDefinition.getName() + "["+i+"]" +"." + // subCollectionDefinition.getName() + "[" + j + "]"); if (collectionField instanceof MaintainableFieldDefinition) { MaintenanceUtils.setFieldQuickfinder(lineBusinessObject, collectionDefinition.getName(), false, i, name, collField, displayedFieldNames, m, (MaintainableFieldDefinition) collectionField); MaintenanceUtils.setFieldDirectInquiry(lineBusinessObject, name, (MaintainableFieldDefinition) collectionField, collField, displayedFieldNames); } else { LookupUtils.setFieldQuickfinder(lineBusinessObject, collectionDefinition.getName(), false, i, name, collField, displayedFieldNames, m); LookupUtils.setFieldDirectInquiry(lineBusinessObject, name, collField); } String propertyValue = ObjectUtils.getFormattedPropertyValueUsingDataDictionary( lineBusinessObject, collectionField.getName()); // For files the FormFile is not being persisted instead the file data is stored in // individual fields as defined by PersistableAttachment. However, newly added rows contain all data // in the FormFile, so check if it's empty. if (StringUtils.isBlank(propertyValue) && Field.FILE.equals(collField.getFieldType())) { Object fileName = ObjectUtils.getNestedValue(lineBusinessObject, KRADConstants.BO_ATTACHMENT_FILE_NAME); collField.setPropertyValue(fileName); } else { collField.setPropertyValue(propertyValue); } if (Field.FILE.equals(collField.getFieldType())) { Object fileType = ObjectUtils.getNestedValue(lineBusinessObject, KRADConstants.BO_ATTACHMENT_FILE_CONTENT_TYPE); if (fileType == null && collField.getPropertyName().contains(".")) { // fileType not found on bo, so check in the attachment field on bo String tempName = collField.getPropertyName() .substring(collField.getPropertyName().lastIndexOf('.') + 1); fileType = ObjectUtils.getNestedValue(lineBusinessObject, (tempName + "." + KRADConstants.BO_ATTACHMENT_FILE_CONTENT_TYPE)); } collField.setImageSrc(WebUtils.getAttachmentImageForUrl((String) fileType)); } if (StringUtils.isNotBlank(collField.getAlternateDisplayPropertyName())) { String alternateDisplayPropertyValue = ObjectUtils .getFormattedPropertyValueUsingDataDictionary(lineBusinessObject, collField.getAlternateDisplayPropertyName()); collField.setAlternateDisplayPropertyValue(alternateDisplayPropertyValue); } if (StringUtils.isNotBlank(collField.getAdditionalDisplayPropertyName())) { String additionalDisplayPropertyValue = ObjectUtils .getFormattedPropertyValueUsingDataDictionary(lineBusinessObject, collField.getAdditionalDisplayPropertyName()); collField.setAdditionalDisplayPropertyValue(additionalDisplayPropertyValue); } //update user fields with universal id and/or name updateUserFields(collField, lineBusinessObject); // the the field as read only (if appropriate) if (collectionField.isReadOnlyAfterAdd()) { collField.setReadOnly(true); } // check if this is a summary field if (collectionDefinition.hasSummaryField(collectionField.getName())) { summaryFields.put(collectionField.getName(), collField); } collFields.add(collField); } Field containerField; containerField = FieldUtils.constructContainerField( KRADConstants.EDIT_PREFIX + "[" + (new Integer(i)).toString() + "]", collectionLabel + " " + (i + 1), collFields, numberOfColumns); // why is this only on collections and not subcollections any significance or just oversight? containerField.setContainerName( collectionDefinition.getName() + "[" + (new Integer(i)).toString() + "]."); /* If the collection line is pending (meaning added by this document) the isNewCollectionRecord will be set to true. In this case we give an option to delete the line. The parameters for the delete action method are embedded into the button name. */ if (lineBusinessObject instanceof PersistableBusinessObject && (((PersistableBusinessObject) lineBusinessObject).isNewCollectionRecord() || collectionDefinition.isAlwaysAllowCollectionDeletion())) { containerField.getContainerRows() .add(new Row(getDeleteRowButtonField(parents + collectionDefinition.getName(), (new Integer(i)).toString()))); } if (StringUtils.isNotEmpty(collectionElementLabel)) { //We don't want to associate any indexes to the containerElementName anymore so that //when the element is deleted, the currentTabIndex won't be associated with the //wrong tab for the remaining tab. //containerField.setContainerElementName(collectionElementLabel + " " + (i + 1)); containerField.setContainerElementName(collectionElementLabel); // reorder summaryFields to make sure they are in the order specified in the summary section List orderedSummaryFields = getSummaryFields(summaryFields, collectionDefinition); containerField.setContainerDisplayFields(orderedSummaryFields); } Row containerRow = new Row(containerField); if (setRowHidden) { containerRow.setHidden(true); } containerRows.add(containerRow); Collection<? extends CollectionDefinitionI> subCollections = collectionDefinition .getCollections(); List<Field> subCollFields = new ArrayList<Field>(); summaryFields = new HashMap(); // iterate over the subCollections directly on this collection for (CollectionDefinitionI subCollection : subCollections) { Collection<? extends FieldDefinitionI> subCollectionFields = subCollection.getFields(); int subCollectionNumberOfColumns = numberOfColumns; if (!s.getContainedCollectionNames() .contains(collectionDefinition.getName() + "." + subCollection.getName())) { s.getContainedCollectionNames() .add(collectionDefinition.getName() + "." + subCollection.getName()); } if (subCollection instanceof InquiryCollectionDefinition) { InquiryCollectionDefinition icd = (InquiryCollectionDefinition) subCollection; if (icd.getNumberOfColumns() != null) { subCollectionNumberOfColumns = icd.getNumberOfColumns(); } } // get label for collection String subCollectionLabel = getDataDictionaryService().getCollectionLabel(o.getClass(), subCollection.getName()); // retrieve the summary label either from the override or from the DD String subCollectionElementLabel = subCollection.getSummaryTitle(); if (StringUtils.isEmpty(subCollectionElementLabel)) { subCollectionElementLabel = getDataDictionaryService().getCollectionElementLabel( o.getClass().getName(), subCollection.getName(), subCollection.getBusinessObjectClass()); } // make sure it's really a collection (i.e. list) String subCollectionName = subCollection.getName(); Object subObj = ObjectUtils.getPropertyValue(lineBusinessObject, subCollectionName); Object oldSubObj = null; if (oldLineBusinessObject != null) { oldSubObj = ObjectUtils.getPropertyValue(oldLineBusinessObject, subCollectionName); } if (subObj instanceof List) { /* recursively call this method to get the add row and exisiting members of the subCollections subcollections containerRows.addAll(getContainerRows(subCollectionDefinition, displayedFieldNames,containerRowErrorKey, parents+collectionDefinition.getName()+"["+i+"]"+".","[0]",false, subCollectionNumberOfColumn)); */ containerField.getContainerRows() .addAll(getContainerRows(s, subCollection, o, m, oldMaintainable, displayedFieldNames, conditionallyRequiredMaintenanceFields, containerRowErrorKey, parents + collectionDefinition.getName() + "[" + i + "]" + ".", false, subCollectionNumberOfColumns, inquirable)); // iterate over the fields for (int j = 0; j < ((List) subObj).size(); j++) { BusinessObject lineSubBusinessObject = (BusinessObject) ((List) subObj).get(j); if (lineSubBusinessObject instanceof PersistableBusinessObject) { ((PersistableBusinessObject) lineSubBusinessObject) .refreshNonUpdateableReferences(); } // determine if sub collection line is inactive and should be hidden boolean setSubRowHidden = false; if (lineSubBusinessObject instanceof Inactivatable && !((Inactivatable) lineSubBusinessObject).isActive()) { if (oldSubObj != null) { // get corresponding elements in both the new list and the old list BusinessObject oldLineSubBusinessObject = (BusinessObject) ((List) oldSubObj) .get(j); if (m != null) { if (!hidableRowsPresent) { hidableRowsPresent = isRowHideableForMaintenanceDocument( lineSubBusinessObject, oldLineSubBusinessObject); } setSubRowHidden = isRowHiddenForMaintenanceDocument( lineSubBusinessObject, oldLineSubBusinessObject, m, collectionName); } } if (inquirable != null) { if (!hidableRowsPresent) { hidableRowsPresent = isRowHideableForInquiry(lineSubBusinessObject); } setSubRowHidden = isRowHiddenForInquiry(lineSubBusinessObject, inquirable, collectionName); } } subCollFields = new ArrayList<Field>(); // construct field objects based on fields for (FieldDefinitionI subCollectionField : subCollectionFields) { // construct Field UI object from definition Field subCollField = FieldUtils.getPropertyField( subCollection.getBusinessObjectClass(), subCollectionField.getName(), false); String subCollectionFullName = collectionDefinition.getName() + "[" + i + "]" + "." + subCollection.getName(); if (translateCodes) { FieldUtils.setAdditionalDisplayPropertyForCodes( lineSubBusinessObject.getClass(), subCollField.getPropertyName(), subCollField); } FieldBridge.setupField(subCollField, subCollectionField, conditionallyRequiredMaintenanceFields); setPrimaryKeyFieldsReadOnly(subCollection.getBusinessObjectClass(), subCollField); // save the simple property name String name = subCollField.getPropertyName(); // prefix name for multi line (indexed) subCollField.setPropertyName(subCollectionFullName + "[" + j + "]." + subCollField.getPropertyName()); // commenting out codes for sub-collections show/hide for now if (subCollectionField instanceof MaintainableFieldDefinition) { MaintenanceUtils.setFieldQuickfinder(lineSubBusinessObject, subCollectionFullName, false, j, name, subCollField, displayedFieldNames, m, (MaintainableFieldDefinition) subCollectionField); MaintenanceUtils.setFieldDirectInquiry(lineSubBusinessObject, subCollectionFullName, false, j, name, subCollField, displayedFieldNames, m, (MaintainableFieldDefinition) subCollectionField); } else { LookupUtils.setFieldQuickfinder(lineSubBusinessObject, subCollectionFullName, false, j, name, subCollField, displayedFieldNames); LookupUtils.setFieldDirectInquiry(lineBusinessObject, name, subCollField); } String propertyValue = ObjectUtils .getFormattedPropertyValueUsingDataDictionary(lineSubBusinessObject, subCollectionField.getName()); subCollField.setPropertyValue(propertyValue); if (StringUtils .isNotBlank(subCollField.getAlternateDisplayPropertyName())) { String alternateDisplayPropertyValue = ObjectUtils .getFormattedPropertyValueUsingDataDictionary( lineSubBusinessObject, subCollField.getAlternateDisplayPropertyName()); subCollField.setAlternateDisplayPropertyValue( alternateDisplayPropertyValue); } if (StringUtils .isNotBlank(subCollField.getAdditionalDisplayPropertyName())) { String additionalDisplayPropertyValue = ObjectUtils .getFormattedPropertyValueUsingDataDictionary( lineSubBusinessObject, subCollField.getAdditionalDisplayPropertyName()); subCollField.setAdditionalDisplayPropertyValue( additionalDisplayPropertyValue); } // check if this is a summary field if (subCollection.hasSummaryField(subCollectionField.getName())) { summaryFields.put(subCollectionField.getName(), subCollField); } if (subCollectionField.isReadOnlyAfterAdd()) { subCollField.setReadOnly(true); } subCollFields.add(subCollField); } Field subContainerField = FieldUtils.constructContainerField( KRADConstants.EDIT_PREFIX + "[" + (new Integer(j)).toString() + "]", subCollectionLabel, subCollFields); if (lineSubBusinessObject instanceof PersistableBusinessObject && (((PersistableBusinessObject) lineSubBusinessObject) .isNewCollectionRecord() || subCollection.isAlwaysAllowCollectionDeletion())) { subContainerField.getContainerRows() .add(new Row(getDeleteRowButtonField( parents + collectionDefinition.getName() + "[" + i + "]" + "." + subCollectionName, (new Integer(j)).toString()))); } // summary line code if (StringUtils.isNotEmpty(subCollectionElementLabel)) { //We don't want to associate any indexes to the containerElementName anymore so that //when the element is deleted, the currentTabIndex won't be associated with the //wrong tab for the remaining tab. //subContainerField.setContainerElementName(subCollectionElementLabel + " " + (j + 1)); subContainerField.setContainerElementName( collectionElementLabel + "-" + subCollectionElementLabel); } subContainerField.setContainerName( collectionDefinition.getName() + "." + subCollectionName); if (!summaryFields.isEmpty()) { // reorder summaryFields to make sure they are in the order specified in the summary section List orderedSummaryFields = getSummaryFields(summaryFields, subCollection); subContainerField.setContainerDisplayFields(orderedSummaryFields); } Row subContainerRow = new Row(subContainerField); if (setRowHidden || setSubRowHidden) { subContainerRow.setHidden(true); } containerField.getContainerRows().add(subContainerRow); } } } } if (!hidableRowsPresent) { s.setExtraButtonSource(""); } } } } return containerRows; }
From source file:org.kuali.rice.krad.dao.impl.LookupDaoJpa.java
public Long findCountByMap(Object example, Map formProps) { Criteria criteria = new Criteria(example.getClass().getName()); // iterate through the parameter map for key values search criteria Iterator propsIter = formProps.keySet().iterator(); while (propsIter.hasNext()) { String propertyName = (String) propsIter.next(); String searchValue = (String) formProps.get(propertyName); // if searchValue is empty and the key is not a valid property ignore if (StringUtils.isBlank(searchValue) || !(PropertyUtils.isWriteable(example, propertyName))) { continue; }/*from ww w. jav a 2s. c o m*/ // get property type which is used to determine type of criteria Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService); if (propertyType == null) { continue; } Boolean caseInsensitive = Boolean.TRUE; if (KRADServiceLocatorWeb.getDataDictionaryService().isAttributeDefined(example.getClass(), propertyName)) { // If forceUppercase is true, both the database value and the user entry should be converted to Uppercase -- so change the caseInsensitive to false since we don't need to // worry about the values not matching. However, if forceUppercase is false, make sure to do a caseInsensitive search because the database value and user entry // could be mixed case. Thus, caseInsensitive will be the opposite of forceUppercase. caseInsensitive = !KRADServiceLocatorWeb.getDataDictionaryService() .getAttributeForceUppercase(example.getClass(), propertyName); } if (caseInsensitive == null) { caseInsensitive = Boolean.TRUE; } boolean treatWildcardsAndOperatorsAsLiteral = KNSServiceLocator.getBusinessObjectDictionaryService() .isLookupFieldTreatWildcardsAndOperatorsAsLiteral(example.getClass(), propertyName); // build criteria if (!caseInsensitive) { // Verify that the searchValue is uppercased if caseInsensitive is false searchValue = searchValue.toUpperCase(); } addCriteria(propertyName, searchValue, propertyType, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria); } // execute query and return result list return (Long) new QueryByCriteria(entityManager, criteria).toCountQuery().getSingleResult(); }
From source file:org.kuali.rice.krad.dao.impl.LookupDaoJpa.java
/** * Return whether or not an attribute is writeable. This method is aware * that that Collections may be involved and handles them consistently with * the way in which OJB handles specifying the attributes of elements of a * Collection./*from w ww . j a v a 2 s. co m*/ * * @param o * @param p * @return * @throws IllegalArgumentException */ private boolean isWriteable(Object o, String p) throws IllegalArgumentException { if (null == o || null == p) { throw new IllegalArgumentException("Cannot check writeable status with null arguments."); } boolean b = false; // Try the easy way. if (!(PropertyUtils.isWriteable(o, p))) { // If that fails lets try to be a bit smarter, understanding that // Collections may be involved. if (-1 != p.indexOf('.')) { String[] parts = p.split("\\."); // Get the type of the attribute. Class c = ObjectUtils.getPropertyType(o, parts[0], persistenceStructureService); Object i = null; // If the next level is a Collection, look into the collection, // to find out what type its elements are. if (Collection.class.isAssignableFrom(c)) { Map<String, Class> m = persistenceStructureService.listCollectionObjectTypes(o.getClass()); c = m.get(parts[0]); } // Look into the attribute class to see if it is writeable. try { i = c.newInstance(); StringBuffer sb = new StringBuffer(); for (int x = 1; x < parts.length; x++) { sb.append(1 == x ? "" : ".").append(parts[x]); } b = isWriteable(i, sb.toString()); } catch (InstantiationException ie) { LOG.info(ie); } catch (IllegalAccessException iae) { LOG.info(iae); } } } else { b = true; } return b; }
From source file:org.kuali.rice.krad.dao.impl.LookupDaoJpa.java
/** * @see org.kuali.rice.krad.dao.LookupDao#findObjectByMap(java.lang.Object, * java.util.Map)//ww w .ja v a 2 s. c o m */ public Object findObjectByMap(Object example, Map formProps) { Criteria jpaCriteria = new Criteria(example.getClass().getName()); Iterator propsIter = formProps.keySet().iterator(); while (propsIter.hasNext()) { String propertyName = (String) propsIter.next(); String searchValue = ""; if (formProps.get(propertyName) != null) { searchValue = (formProps.get(propertyName)).toString(); } if (StringUtils.isNotBlank(searchValue) & PropertyUtils.isWriteable(example, propertyName)) { Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService); if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType)) { if (propertyType.equals(Long.class)) { jpaCriteria.eq(propertyName, new Long(searchValue)); } else { jpaCriteria.eq(propertyName, new Integer(searchValue)); } } else if (TypeUtils.isTemporalClass(propertyType)) { jpaCriteria.eq(propertyName, parseDate(ObjectUtils.clean(searchValue))); } else { jpaCriteria.eq(propertyName, searchValue); } } } return new QueryByCriteria(entityManager, jpaCriteria).toQuery().getSingleResult(); }
From source file:org.kuali.rice.krad.dao.impl.LookupDaoOjb.java
/** * Find count of records meeting criteria based on the object and map. */// w ww . j a v a 2s .c om public Long findCountByMap(Object example, Map formProps) { Criteria criteria = new Criteria(); // iterate through the parameter map for key values search criteria Iterator propsIter = formProps.keySet().iterator(); while (propsIter.hasNext()) { String propertyName = (String) propsIter.next(); String searchValue = (String) formProps.get(propertyName); // if searchValue is empty and the key is not a valid property ignore if (StringUtils.isBlank(searchValue) || !(PropertyUtils.isWriteable(example, propertyName))) { continue; } // get property type which is used to determine type of criteria Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService); if (propertyType == null) { continue; } Boolean caseInsensitive = Boolean.TRUE; if (KRADServiceLocatorWeb.getDataDictionaryService().isAttributeDefined(example.getClass(), propertyName)) { caseInsensitive = !KRADServiceLocatorWeb.getDataDictionaryService() .getAttributeForceUppercase(example.getClass(), propertyName); } if (caseInsensitive == null) { caseInsensitive = Boolean.TRUE; } boolean treatWildcardsAndOperatorsAsLiteral = KNSServiceLocator.getBusinessObjectDictionaryService() .isLookupFieldTreatWildcardsAndOperatorsAsLiteral(example.getClass(), propertyName); if (!caseInsensitive) { // Verify that the searchValue is uppercased if caseInsensitive is false searchValue = searchValue.toUpperCase(); } // build criteria addCriteria(propertyName, searchValue, propertyType, caseInsensitive, treatWildcardsAndOperatorsAsLiteral, criteria); } // execute query and return result list Query query = QueryFactory.newQuery(example.getClass(), criteria); return new Long(getPersistenceBrokerTemplate().getCount(query)); }
From source file:org.kuali.rice.krad.dao.impl.LookupDaoOjb.java
/** * @see org.kuali.rice.krad.dao.LookupDao#findObjectByMap(java.lang.Object, java.util.Map) *///from w w w . j a v a2 s.co m @Override public <T extends Object> T findObjectByMap(Class<T> type, Map<String, String> formProps) { T example = null; try { example = type.newInstance(); } catch (IllegalAccessException e) { throw new RuntimeException("Cannot get new instance of " + type.getName(), e); } catch (InstantiationException e) { throw new RuntimeException("Cannot instantiate " + type.getName(), e); } if (KRADServiceLocatorWeb.getLegacyDataAdapter().isPersistable(example.getClass())) { Criteria criteria = new Criteria(); // iterate through the parameter map for search criteria for (Map.Entry<String, String> formProp : formProps.entrySet()) { String propertyName = formProp.getKey(); String searchValue = ""; if (formProp.getValue() != null) { searchValue = formProp.getValue(); } if (StringUtils.isNotBlank(searchValue) & PropertyUtils.isWriteable(example, propertyName)) { Class propertyType = ObjectUtils.getPropertyType(example, propertyName, persistenceStructureService); if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType)) { criteria.addEqualTo(propertyName, cleanNumeric(searchValue)); } else if (TypeUtils.isTemporalClass(propertyType)) { criteria.addEqualTo(propertyName, parseDate(ObjectUtils.clean(searchValue))); } else { criteria.addEqualTo(propertyName, searchValue); } } } // execute query and return result list Query query = QueryFactory.newQuery(example.getClass(), criteria); return (T) getPersistenceBrokerTemplate().getObjectByQuery(query); } return null; }
From source file:org.kuali.rice.krad.service.impl.LookupCriteriaGeneratorImpl.java
@Override public QueryByCriteria.Builder createObjectCriteriaFromMap(Object example, Map<String, String> formProps) { Predicates criteria = new Predicates(); // iterate through the parameter map for search criteria for (Map.Entry<String, String> formProp : formProps.entrySet()) { String propertyName = formProp.getKey(); String searchValue = ""; if (formProp.getValue() != null) { searchValue = formProp.getValue(); }/*www . java 2 s . c o m*/ Object instanObject = instantiateLookupDataObject((Class<?>) example); if (StringUtils.isNotBlank(searchValue) & PropertyUtils.isWriteable(instanObject, propertyName)) { Class<?> propertyType = getPropertyType(instanObject, propertyName); if (TypeUtils.isIntegralClass(propertyType) || TypeUtils.isDecimalClass(propertyType)) { addEqualNumeric(criteria, propertyName, propertyType, searchValue); } else if (TypeUtils.isTemporalClass(propertyType)) { addEqualTemporal(criteria, propertyName, searchValue); } else { addEqual(criteria, propertyName, searchValue); } } } return criteria.toQueryBuilder(); }
From source file:org.kuali.rice.krad.service.impl.LookupCriteriaGeneratorImpl.java
/** * Return whether or not an attribute is writeable. This method is aware * that that Collections may be involved and handles them consistently with * the way in which OJB handles specifying the attributes of elements of a * Collection./* w w w .jav a2s .co m*/ * * @param o * @param p * @return * @throws IllegalArgumentException */ protected boolean isWriteable(Object o, String p) throws IllegalArgumentException { if (null == o || null == p) { throw new IllegalArgumentException("Cannot check writable status with null arguments."); } boolean b = false; // Try the easy way. if (!(PropertyUtils.isWriteable(o, p))) { // If that fails lets try to be a bit smarter, understanding that // Collections may be involved. if (-1 != p.indexOf('.')) { String[] parts = p.split("\\."); // Get the type of the attribute. Class<?> c = getPropertyType(o, parts[0]); Object i = null; // If the next level is a Collection, look into the collection, // to find out what type its elements are. if (Collection.class.isAssignableFrom(c)) { c = getDataObjectService().getMetadataRepository().getMetadata(o.getClass()) .getCollection(parts[0]).getRelatedType(); } // Look into the attribute class to see if it is writeable. try { i = c.newInstance(); StringBuffer sb = new StringBuffer(); for (int x = 1; x < parts.length; x++) { sb.append(1 == x ? "" : ".").append(parts[x]); } b = isWriteable(i, sb.toString()); } catch (InstantiationException ie) { LOG.info(ie); } catch (IllegalAccessException iae) { LOG.info(iae); } } } else { b = true; } return b; }
From source file:org.kuali.rice.krad.util.ObjectUtils.java
/** * Return whether or not an attribute is writeable. This method is aware that that Collections may be involved and * handles them//from www.j av a2 s. c o m * consistently with the way in which OJB handles specifying the attributes of elements of a Collection. * * @param object * @param property * @return * @throws IllegalArgumentException */ public static boolean isWriteable(Object object, String property, PersistenceStructureService persistenceStructureService) throws IllegalArgumentException { if (null == object || null == property) { throw new IllegalArgumentException("Cannot check writeable status with null arguments."); } // Try the easy way. try { if (!(PropertyUtils.isWriteable(object, property))) { // If that fails lets try to be a bit smarter, understanding that Collections may be involved. return isWriteableHelper(object, property, persistenceStructureService); } else { return true; } } catch (NestedNullException nestedNullException) { // If a NestedNullException is thrown then the property has a null // value. Call the helper to find the class of the property and // get a newInstance of it. return isWriteableHelper(object, property, persistenceStructureService); } }
From source file:org.malaguna.cmdit.service.reflection.HibernateProxyUtils.java
private void deepLoadDomainObject(AbstractObject<?> object, Object guideObj) { PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(unproxy(object)); for (PropertyDescriptor property : properties) { String pName = property.getName(); if (PropertyUtils.isWriteable(object, pName) && PropertyUtils.isReadable(object, pName)) { try { Object propGuideObject = property.getReadMethod().invoke(guideObj); if (null != propGuideObject) { Object unproxied = deepLoad(property.getReadMethod().invoke(object), propGuideObject); property.getWriteMethod().invoke(object, unproxied); }//from w w w . ja v a2 s . com } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } }