// GHH 20080326 - added resultDistinguishedName to method signature. If // there is an element in the model named "DN" and there is no attribute // with this name in the search result, we return this new parameter // value for that column in the result // GHH 20080326 - added handling of ClassCastException when non-string // attribute is returned private Object getValue( Column modelElement, SearchResult result, Attributes attrs, boolean unwrap) throws TranslatorException, InvalidNameException { String modelAttrName = modelElement.getSourceName(); Class<?> modelAttrClass = modelElement.getJavaType(); String multivalAttr = modelElement.getDefaultValue(); if (modelAttrName == null) { final String msg = LDAPPlugin.Util.getString("LDAPSyncQueryExecution.nullAttrError"); // $NON-NLS-1$ throw new TranslatorException(msg); } Attribute resultAttr = attrs.get(modelAttrName); // If the attribute is not present, we return NULL. if (resultAttr == null) { // GHH 20080326 - return DN from input parameter // if DN attribute is not present in search result if (modelAttrName.equalsIgnoreCase("DN")) { // $NON-NLS-1$ return result.getNameInNamespace(); } return null; } Object objResult = null; try { if (TypeFacility.RUNTIME_TYPES.STRING.equals(modelAttrClass) && MULTIVALUED_CONCAT.equalsIgnoreCase(multivalAttr)) { // mpw 5/09 // Order the multi-valued attrs alphabetically before creating a single string, // using the delimiter to separate each token ArrayList<String> multivalList = new ArrayList<String>(); NamingEnumeration<?> attrNE = resultAttr.getAll(); int length = 0; while (attrNE.hasMore()) { String val = (String) attrNE.next(); multivalList.add(val); length += ((val == null ? 0 : val.length()) + 1); } Collections.sort(multivalList); StringBuilder multivalSB = new StringBuilder(length); Iterator<String> itr = multivalList.iterator(); while (itr.hasNext()) { multivalSB.append(itr.next()); if (itr.hasNext()) { multivalSB.append(delimiter); } } return multivalSB.toString(); } if (modelAttrClass.isArray()) { return getArray(modelAttrClass.getComponentType(), resultAttr, modelElement, modelAttrName); } if (unwrap && resultAttr.size() > 1) { return getArray(modelAttrClass, resultAttr, modelElement, modelAttrName); } // just a single value objResult = resultAttr.get(); } catch (NamingException ne) { final String msg = LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12004, modelAttrName) + " : " + ne.getExplanation(); // $NON-NLS-1$m LogManager.logWarning(LogConstants.CTX_CONNECTOR, msg); throw new TranslatorException(ne, msg); } return convertSingleValue(modelElement, modelAttrName, modelAttrClass, objResult); }
@Test public void testForeignTable() throws Exception { String ddl = "CREATE FOREIGN TABLE G1(\n" + "e1 integer primary key,\n" + "e2 varchar(10) unique,\n" + "e3 date not null unique,\n" + "e4 decimal(12,3) default 12.2 options (searchable 'unsearchable'),\n" + "e5 integer auto_increment INDEX OPTIONS (UUID 'uuid', NAMEINSOURCE 'nis', SELECTABLE 'NO'),\n" + "e6 varchar index default 'hello')\n" + "OPTIONS (CARDINALITY 12, UUID 'uuid2', UPDATABLE 'true', FOO 'BAR', ANNOTATION 'Test Table')"; Schema s = helpParse(ddl, "model").getSchema(); Map<String, Table> tableMap = s.getTables(); assertTrue("Table not found", tableMap.containsKey("G1")); Table table = tableMap.get("G1"); assertTrue(table.isPhysical()); assertFalse(table.isVirtual()); assertFalse(table.isSystem()); assertFalse(table.isMaterialized()); assertFalse(table.isDeletePlanEnabled()); assertEquals("uuid2", table.getUUID()); assertEquals(12, table.getCardinality()); assertTrue(table.supportsUpdate()); assertEquals("BAR", table.getProperties().get("FOO")); assertEquals("Test Table", table.getAnnotation()); assertEquals(6, table.getColumns().size()); List<Column> columns = table.getColumns(); Column e1 = columns.get(0); Column e2 = columns.get(1); Column e3 = columns.get(2); Column e4 = columns.get(3); Column e5 = columns.get(4); Column e6 = columns.get(5); assertEquals("e1", e1.getName()); assertEquals("int", e1.getDatatype().getName()); assertEquals("primary key not same", e1, table.getPrimaryKey().getColumns().get(0)); assertEquals("e2", e2.getName()); assertEquals("string", e2.getDatatype().getName()); assertEquals("unique", e2, table.getUniqueKeys().get(0).getColumns().get(0)); assertEquals(NullType.Nullable, e2.getNullType()); assertEquals(10, e2.getLength()); assertEquals(0, e2.getPrecision()); assertEquals("e3", e3.getName()); assertEquals("date", e3.getDatatype().getName()); assertEquals("unique", e3, table.getUniqueKeys().get(1).getColumns().get(0)); assertEquals(NullType.No_Nulls, e3.getNullType()); assertEquals("e4", e4.getName()); assertEquals("bigdecimal", e4.getDatatype().getName()); assertEquals(false, e4.isAutoIncremented()); assertEquals(12, e4.getPrecision()); assertEquals(3, e4.getScale()); assertEquals(SearchType.Unsearchable, e4.getSearchType()); assertEquals("12.2", e4.getDefaultValue()); assertEquals("e5", e5.getName()); assertEquals("int", e5.getDatatype().getName()); assertEquals(true, e5.isAutoIncremented()); assertEquals("uuid", e5.getUUID()); assertEquals("nis", e5.getNameInSource()); assertEquals(false, e5.isSelectable()); assertEquals("index", e5, table.getIndexes().get(0).getColumns().get(0)); assertEquals("e6", e6.getName()); assertEquals("string", e6.getDatatype().getName()); assertEquals("index", e6, table.getIndexes().get(1).getColumns().get(0)); assertEquals("hello", e6.getDefaultValue()); }