ViewSubResourceProvider extends AbstractResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<ViewInstanceEntity> instanceDefinitions = new HashSet<ViewInstanceEntity>(); try { Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate); int size = propertyMaps.size(); Collection<ViewInstanceEntity> viewInstanceDefinitions = viewDefinition.getInstances(); if (size == 0) { instanceDefinitions.addAll(viewInstanceDefinitions); } else { for (Map<String, Object> propertyMap : propertyMaps) { String instanceName = (String) propertyMap.get(INSTANCE_NAME_PROPERTY_ID); if (size == 1 && instanceName != null) { String resourceId = (String) propertyMap.get(pkField); if (resourceId != null) { Object bean = getResourceProvider(instanceName).getResource(resourceId, requestedIds); return Collections.singleton(getResource(bean, viewDefinition.getCommonName(), viewDefinition.getVersion(), instanceName, requestedIds)); } } if (instanceName == null) { instanceDefinitions.addAll(viewInstanceDefinitions); break; } else { ViewInstanceEntity instanceDefinition = viewDefinition.getInstanceDefinition(instanceName); if (instanceDefinition != null) { instanceDefinitions.add(instanceDefinition); } } } } Set<Resource> results = new HashSet<Resource>(); ReadRequest readRequest = new ViewReadRequest(request, requestedIds, predicate == null ? "" : predicate.toString()); for (ViewInstanceEntity instanceDefinition : instanceDefinitions) { Set<?> beans = instanceDefinition.getResourceProvider(type).getResources(readRequest); for (Object bean : beans) { Resource resource = getResource(bean, viewDefinition.getCommonName(), viewDefinition.getVersion(), instanceDefinition.getName(), requestedIds); if (predicate.evaluate(resource)) { results.add(resource); } } } return results; } catch (org.apache.ambari.view.NoSuchResourceException e) { throw new NoSuchParentResourceException(e.getMessage(), e); } catch (org.apache.ambari.view.UnsupportedPropertyException e) { throw new UnsupportedPropertyException(getResourceType(e), e.getPropertyIds()); } catch (Exception e) { LOG.error("Caught exception getting view sub resources.", e); throw new SystemException(e.getMessage(), e); } } ViewSubResourceProvider(Resource.Type type, Class<?> clazz, String pkField, ViewEntity viewDefinition); @Override RequestStatus createResources(Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate);  }
@Test public void testGetResources() throws Exception { Properties properties = new Properties(); properties.put("p1", "v1"); Configuration ambariConfig = new Configuration(properties); ViewConfig config = ViewConfigTest.getConfig(xml); ViewEntity viewEntity = ViewRegistryTest.getViewEntity(config, ambariConfig, getClass().getClassLoader(), ""); ViewRegistryTest.getViewInstanceEntity(viewEntity, config.getInstances().get(0)); Map<Resource.Type, ViewSubResourceDefinition> resourceDefinitions = viewEntity.getResourceDefinitions(); Assert.assertEquals(1, resourceDefinitions.size()); Resource.Type type = resourceDefinitions.keySet().iterator().next(); ViewSubResourceProvider viewSubResourceProvider = new ViewSubResourceProvider(type, MyResource.class, "id", viewEntity); Request request = PropertyHelper.getReadRequest("id", "properties", "metrics/myMetric"); Predicate predicate = new AlwaysPredicate(); Set<Resource> resources = viewSubResourceProvider.getResources(request, predicate); Assert.assertEquals(2, resources.size()); predicate = new PredicateBuilder().property("metrics/myMetric").greaterThan(1).toPredicate(); resources = viewSubResourceProvider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); Assert.assertTrue(((Integer) resources.iterator().next().getPropertyValue("metrics/myMetric")) > 1); }
@Test public void testGetResources_temporal() throws Exception { Properties properties = new Properties(); properties.put("p1", "v1"); Configuration ambariConfig = new Configuration(properties); ViewConfig config = ViewConfigTest.getConfig(xml); ViewEntity viewEntity = ViewRegistryTest.getViewEntity(config, ambariConfig, getClass().getClassLoader(), ""); ViewRegistryTest.getViewInstanceEntity(viewEntity, config.getInstances().get(0)); Map<Resource.Type, ViewSubResourceDefinition> resourceDefinitions = viewEntity.getResourceDefinitions(); Assert.assertEquals(1, resourceDefinitions.size()); Resource.Type type = resourceDefinitions.keySet().iterator().next(); ViewSubResourceProvider viewSubResourceProvider = new ViewSubResourceProvider(type, MyResource.class, "id", viewEntity); Set<String> requestProperties = new HashSet<String>(); requestProperties.add("metrics/myMetric"); Map<String, TemporalInfo> temporalInfoMap = new HashMap<String, TemporalInfo>(); TemporalInfo temporalInfo = new TemporalInfoImpl(1000L, 1100L, 10L); temporalInfoMap.put("metrics/myMetric", temporalInfo); Request request = PropertyHelper.getReadRequest(requestProperties, temporalInfoMap); Predicate predicate = new AlwaysPredicate(); Set<Resource> resources = viewSubResourceProvider.getResources(request, predicate); Assert.assertEquals(2, resources.size()); }