protected void makeAttributes()

in modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java [404:531]


	protected void makeAttributes(EOModelHelper helper, EOObjEntity objEntity) {
		Map entityPlistMap = helper.entityPListMap(objEntity.getName());
		List primaryKeys = (List) entityPlistMap.get("primaryKeyAttributes");

		List classProperties;
		classProperties = (List) entityPlistMap.get("classProperties");

		List attributes = (List) entityPlistMap.get("attributes");
		DbEntity dbEntity = objEntity.getDbEntity();

		if (primaryKeys == null) {
			primaryKeys = Collections.EMPTY_LIST;
		}

		if (classProperties == null) {
			classProperties = Collections.EMPTY_LIST;
		}

		if (attributes == null) {
			attributes = Collections.EMPTY_LIST;
		}

		// detect single table inheritance
		boolean singleTableInheritance = false;
		String parentName = (String) entityPlistMap.get("parent");
		while (parentName != null) {
			Map parentData = helper.entityPListMap(parentName);
			if (parentData == null) {
				break;
			}

			String parentExternalName = (String) parentData.get("externalName");
			if (parentExternalName == null) {
				parentName = (String) parentData.get("parent");
				continue;
			}

			if (dbEntity.getName() != null && dbEntity.getName().equals(parentExternalName)) {
				singleTableInheritance = true;
			}

			break;
		}

		Iterator it = attributes.iterator();
		while (it.hasNext()) {
			Map attrMap = (Map) it.next();

			String prototypeName = (String) attrMap.get("prototypeName");
			Map prototypeAttrMap = helper.getPrototypeAttributeMapFor(prototypeName);

			String dbAttrName = getStringValueFromMap( "columnName", attrMap, prototypeAttrMap );
			String attrName = getStringValueFromMap( "name", attrMap, prototypeAttrMap );
			String attrType = getStringValueFromMap( "valueClassName", attrMap, prototypeAttrMap );
			String valueType = getStringValueFromMap( "valueType", attrMap, prototypeAttrMap );
			String externalType = getStringValueFromMap( "externalType", attrMap, prototypeAttrMap );

			String javaType = helper.javaTypeForEOModelerType(attrType, valueType);
			EODbAttribute dbAttr = null;

			if (dbAttrName != null && dbEntity != null) {

				// if inherited attribute, skip it for DbEntity...
				if (!singleTableInheritance || dbEntity.getAttribute(dbAttrName) == null) {

					// create DbAttribute...since EOF allows the same column name for
					// more than one Java attribute, we need to check for name duplicates
					int i = 0;
					String dbAttributeBaseName = dbAttrName;
					while (dbEntity.getAttribute(dbAttrName) != null) {
						dbAttrName = dbAttributeBaseName + i++;
					}

					int sqlType = TypesMapping.getSqlTypeByJava(javaType);
					int width = getInt("width", attrMap, prototypeAttrMap, -1);
					if (sqlType == Types.VARCHAR && width < 0 && externalTypeIsClob(externalType)) {
						// CLOB, or TEXT as PostgreSQL calls it, is usally noted as having no width. In order to
						// not mistake any VARCHAR columns that just happen to have no width set in the model
						// for CLOB columns, use externalType as an additional check.
						sqlType = Types.CLOB;
					} else if(sqlType == TypesMapping.NOT_DEFINED && externalType != null) {
						// At this point we usually hit a custom Java class through a prototype, which isn't resolvable
						// with the model alone. But we can use the externalType as a hint. If that still doesn't match
						// anything, sqlType will still be NOT_DEFINED.
						sqlType = TypesMapping.getSqlTypeByName(externalType.toUpperCase());
					}
					dbAttr = new EODbAttribute(dbAttrName, sqlType, dbEntity);
					dbAttr.setEoAttributeName(attrName);
					dbEntity.addAttribute(dbAttr);

					if (width >= 0) {
						dbAttr.setMaxLength(width);
					}

					int scale = getInt("scale", attrMap, prototypeAttrMap, -1);
					if (scale >= 0) {
						dbAttr.setScale(scale);
					}

					if (primaryKeys.contains(attrName))
						dbAttr.setPrimaryKey(true);

					Object allowsNull = attrMap.get("allowsNull");
					// TODO: Unclear that allowsNull should be inherited from EOPrototypes
					// if (null == allowsNull) allowsNull = prototypeAttrMap.get("allowsNull");;

					dbAttr.setMandatory(!"Y".equals(allowsNull));
				}
			}

			if (classProperties.contains(attrName)) {
				EOObjAttribute attr = new EOObjAttribute(attrName, javaType, objEntity);

				// set readOnly flag of Attribute if either attribute is read or
				// if entity is readOnly
				String entityReadOnlyString = (String) entityPlistMap.get("isReadOnly");
				String attributeReadOnlyString = (String) attrMap.get("isReadOnly");
				if ("Y".equals(entityReadOnlyString) || "Y".equals(attributeReadOnlyString)) {
					attr.setReadOnly(true);
				}

				// set name instead of the actual attribute, as it may be
				// inherited....
				attr.setDbAttributePath(dbAttrName);
				objEntity.addAttribute(attr);
			}
		}
	}