@Test public void testFindComponentsByBaseDisplayedNamePattern() { User mockUser = Mockito.mock(User.class); Mockito.when(mockPlatform.getCurrentUser()).thenReturn(mockUser); Mockito.when(mockUser.getUserId()).thenReturn("testUser"); String[] bdns = { "hit 0", "miss 0", "hit 1", "miss 0", "hit 2" }; em.getTransaction().begin(); for (int i = 0 ; i < bdns.length; i++) { em.persist(createComponentSpec( "search" + i, (i==0) ? "testUser" : "otherUser", bdns[i], "123", "xyz", new ArrayList<Tag>(), Collections.<String,String>emptyMap())); } em.getTransaction().commit(); serviceImpl.bind(mockPlatform); Assert.assertEquals( serviceImpl.findComponentsByBaseDisplayedNamePattern("", null).getCount(), 5); Assert.assertEquals( serviceImpl.findComponentsByBaseDisplayedNamePattern("hit*", null).getCount(), 3); Assert.assertEquals( serviceImpl.findComponentsByBaseDisplayedNamePattern("hit*", new Properties()).getCount(), 3); Properties creatorProps = new Properties(); creatorProps.put("creator", "testUser"); Assert.assertEquals( serviceImpl.findComponentsByBaseDisplayedNamePattern("hit*", creatorProps).getCount(), 1); creatorProps.put("creator", "otherUser"); Assert.assertEquals( serviceImpl.findComponentsByBaseDisplayedNamePattern("hit*", creatorProps).getCount(), 2); }
@SuppressWarnings("unchecked") public QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props) { String username = platform.getCurrentUser().getUserId(); pattern = pattern.isEmpty() ? "%" : pattern.replace('*', '%'); String countQuery = "select count(*) from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; String entitiesQuery = "select c.* from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; EntityManager em = entityManagerFactory.createEntityManager(); try { Query q = em.createNativeQuery(countQuery); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); int count = ((Number) q.getSingleResult()).intValue(); q = em.createNativeQuery(entitiesQuery, ComponentSpec.class); q.setMaxResults(maxResults); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); List<ComponentSpec> daoObjects = q.getResultList(); return new QueryResult(count, daoObjects); } catch (Exception t) { LOGGER.error("error executing query", t); return null; } finally { em.close(); } }
PersistenceServiceImpl implements PersistenceProvider { @SuppressWarnings("unchecked") public QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props) { String username = platform.getCurrentUser().getUserId(); pattern = pattern.isEmpty() ? "%" : pattern.replace('*', '%'); String countQuery = "select count(*) from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; String entitiesQuery = "select c.* from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; EntityManager em = entityManagerFactory.createEntityManager(); try { Query q = em.createNativeQuery(countQuery); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); int count = ((Number) q.getSingleResult()).intValue(); q = em.createNativeQuery(entitiesQuery, ComponentSpec.class); q.setMaxResults(maxResults); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); List<ComponentSpec> daoObjects = q.getResultList(); return new QueryResult(count, daoObjects); } catch (Exception t) { LOGGER.error("error executing query", t); return null; } finally { em.close(); } } }
PersistenceServiceImpl implements PersistenceProvider { @SuppressWarnings("unchecked") public QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props) { String username = platform.getCurrentUser().getUserId(); pattern = pattern.isEmpty() ? "%" : pattern.replace('*', '%'); String countQuery = "select count(*) from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; String entitiesQuery = "select c.* from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; EntityManager em = entityManagerFactory.createEntityManager(); try { Query q = em.createNativeQuery(countQuery); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); int count = ((Number) q.getSingleResult()).intValue(); q = em.createNativeQuery(entitiesQuery, ComponentSpec.class); q.setMaxResults(maxResults); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); List<ComponentSpec> daoObjects = q.getResultList(); return new QueryResult(count, daoObjects); } catch (Exception t) { LOGGER.error("error executing query", t); return null; } finally { em.close(); } }  }
PersistenceServiceImpl implements PersistenceProvider { @SuppressWarnings("unchecked") public QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props) { String username = platform.getCurrentUser().getUserId(); pattern = pattern.isEmpty() ? "%" : pattern.replace('*', '%'); String countQuery = "select count(*) from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; String entitiesQuery = "select c.* from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; EntityManager em = entityManagerFactory.createEntityManager(); try { Query q = em.createNativeQuery(countQuery); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); int count = ((Number) q.getSingleResult()).intValue(); q = em.createNativeQuery(entitiesQuery, ComponentSpec.class); q.setMaxResults(maxResults); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); List<ComponentSpec> daoObjects = q.getResultList(); return new QueryResult(count, daoObjects); } catch (Exception t) { LOGGER.error("error executing query", t); return null; } finally { em.close(); } }  void bind(Platform platform); void unbind(Platform platform); void setEntityManagerProperties(Properties p); void activate(ComponentContext context); @Override AbstractComponent getComponentFromStore(String componentId); @Override AbstractComponent getComponent(String componentId); @Override Collection<AbstractComponent> getReferences(AbstractComponent component); @Override void startRelatedOperations(); @Override void completeRelatedOperations(boolean save); @Override void addComponentToWorkUnit(AbstractComponent component); Collection<AbstractComponent> getCurrentComponents(); @Override Set<String> getAllUsers(); @Override Collection<String> getUsersInGroup(String group); @Override void persist(Collection<AbstractComponent> componentsToPersist); @Override void delete(Collection<AbstractComponent> componentsToDelete); @Override boolean hasComponentsTaggedBy(String tagId); @Override List<AbstractComponent> getReferencedComponents(
			AbstractComponent component); @Override User getUser(String userId); @SuppressWarnings("unchecked") QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props); @Override T getComponent(String externalKey, Class<T> componentType); @Override void updateComponentsFromDatabase(); @Override List<AbstractComponent> getBootstrapComponents(); @Override void addNewUser(String userId, String groupId, AbstractComponent mysandbox, AbstractComponent dropbox); @Override Map<String, ExtendedProperties> getAllProperties(String componentId); @Override AbstractComponent getComponent(String externalKey,
			String componentType); @Override void tagComponents(String tag,
			Collection<AbstractComponent> components); }
PersistenceServiceImpl implements PersistenceProvider { @SuppressWarnings("unchecked") public QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props) { String username = platform.getCurrentUser().getUserId(); pattern = pattern.isEmpty() ? "%" : pattern.replace('*', '%'); String countQuery = "select count(*) from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; String entitiesQuery = "select c.* from component_spec c " + "where c.creator_user_id like :creator " + "and c.component_id not in (select component_id from component_spec where component_type = 'gov.nasa.arc.mct.core.components.TelemetryDataTaxonomyComponent' and component_name = 'All')" + "and (c.component_type != 'gov.nasa.arc.mct.core.components.MineTaxonomyComponent' or c.owner = :owner) " + "and c.component_name like :pattern "; EntityManager em = entityManagerFactory.createEntityManager(); try { Query q = em.createNativeQuery(countQuery); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); int count = ((Number) q.getSingleResult()).intValue(); q = em.createNativeQuery(entitiesQuery, ComponentSpec.class); q.setMaxResults(maxResults); q.setParameter("pattern", pattern); q.setParameter("owner", username); q.setParameter("creator", (props != null && props.get("creator") != null) ? props.get("creator") : "%" ); List<ComponentSpec> daoObjects = q.getResultList(); return new QueryResult(count, daoObjects); } catch (Exception t) { LOGGER.error("error executing query", t); return null; } finally { em.close(); } }  void bind(Platform platform); void unbind(Platform platform); void setEntityManagerProperties(Properties p); void activate(ComponentContext context); @Override AbstractComponent getComponentFromStore(String componentId); @Override AbstractComponent getComponent(String componentId); @Override Collection<AbstractComponent> getReferences(AbstractComponent component); @Override void startRelatedOperations(); @Override void completeRelatedOperations(boolean save); @Override void addComponentToWorkUnit(AbstractComponent component); Collection<AbstractComponent> getCurrentComponents(); @Override Set<String> getAllUsers(); @Override Collection<String> getUsersInGroup(String group); @Override void persist(Collection<AbstractComponent> componentsToPersist); @Override void delete(Collection<AbstractComponent> componentsToDelete); @Override boolean hasComponentsTaggedBy(String tagId); @Override List<AbstractComponent> getReferencedComponents(
			AbstractComponent component); @Override User getUser(String userId); @SuppressWarnings("unchecked") QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props); @Override T getComponent(String externalKey, Class<T> componentType); @Override void updateComponentsFromDatabase(); @Override List<AbstractComponent> getBootstrapComponents(); @Override void addNewUser(String userId, String groupId, AbstractComponent mysandbox, AbstractComponent dropbox); @Override Map<String, ExtendedProperties> getAllProperties(String componentId); @Override AbstractComponent getComponent(String externalKey,
			String componentType); @Override void tagComponents(String tag,
			Collection<AbstractComponent> components);  }