List of usage examples for java.util Map forEach
default void forEach(BiConsumer<? super K, ? super V> action)
From source file:org.wildfly.security.tool.FileSystemRealmCommand.java
/** * Handles creating the Elytron filesystem-realm from the input arrays * * @throws Exception Exception to be handled by Elytron Tool *///ww w. ja v a 2s. c o m private void createFileSystemRealm() throws Exception { Security.addProvider(new WildFlyElytronProvider()); for (int i = 0; i < descriptors.size(); i++) { Descriptor descriptor = descriptors.get(i); if (descriptor.getUsersFile() == null || descriptor.getRolesFile() == null || descriptor.getOutputLocation() == null) { continue; } List<String> usersList = parseInputFile(descriptor, USERS_FILE_PARAM, i + 1); List<String> rolesList = parseInputFile(descriptor, ROLES_FILE_PARAM, i + 1); if (usersList.isEmpty() || rolesList.isEmpty()) { descriptor.reset(); continue; } FileSystemSecurityRealm newFileSystemRealm = new FileSystemSecurityRealm( Paths.get(descriptor.getOutputLocation())); Map<String, ArrayList<String>> usersMap = new HashMap<>(); for (String userMapping : usersList) { String[] userStringSplit = userMapping.split("="); String user = userStringSplit[0].trim(); String password; if (userStringSplit.length == 1) { String message = String.format("No password was found for user %s", user); warningHandler(message); password = null; } else { password = userStringSplit[1].trim(); } ArrayList<String> userAttributes = new ArrayList<>(); userAttributes.add(password); usersMap.put(user, userAttributes); } for (String rolesMapping : rolesList) { String[] rolesStringSplit = rolesMapping.split("="); String user = rolesStringSplit[0].trim(); String[] roles = new String[] {}; if (rolesStringSplit.length < 2) { String message = String.format("No roles were found for user %s", user); warningHandler(message); } else { roles = rolesStringSplit[1].trim().split(","); } ArrayList<String> userAttributes = usersMap.get(user); if (userAttributes == null) { String message = String.format("Roles were found for user %1$s, but user %1$s was not defined.", user); warningHandler(message); ArrayList<String> attributesWithEmptyPassword = new ArrayList<>(); attributesWithEmptyPassword.add(null); attributesWithEmptyPassword.addAll(new ArrayList<>(Arrays.asList(roles))); userAttributes = attributesWithEmptyPassword; usersMap.put(user, userAttributes); } else { userAttributes.addAll(Arrays.asList(roles)); usersMap.replace(user, userAttributes); } if (summaryMode) { summaryString.append( String.format("Added roles: %s for user %s.", ArrayUtils.toString(roles), user)); summaryString.append(System.getProperty("line.separator")); } } usersMap.forEach((key, value) -> { ModifiableRealmIdentity identity = newFileSystemRealm .getRealmIdentityForUpdate(new NamePrincipal(key)); try { identity.create(); MapAttributes attributes = new MapAttributes(); attributes.addAll("roles", value.subList(1, value.size())); identity.setAttributes(attributes); String password = value.get(0); if (password != null) { byte[] hashed = ByteIterator.ofBytes(password.getBytes(StandardCharsets.UTF_8)) .asUtf8String().hexDecode().drain(); PasswordSpec passwordSpec = new DigestPasswordSpec(key, descriptor.getRealmName(), hashed); PasswordFactory factory = PasswordFactory.getInstance(DigestPassword.ALGORITHM_DIGEST_MD5); DigestPassword digestPassword = (DigestPassword) factory.generatePassword(passwordSpec); identity.setCredentials(Collections.singleton(new PasswordCredential(digestPassword))); } identity.dispose(); } catch (NullPointerException e) { warningHandler(String.format("Could not read realm name from the users file")); } catch (Exception e) { warningHandler(String.format("Could not create realm for user %s due to error: ", key) + e.getMessage()); } }); } }
From source file:org.wso2.carbon.apimgt.core.dao.impl.ApiDAOImpl.java
private void deleteEndPointsForOperation(Connection connection, String apiId) throws SQLException, IOException { final String query = "DELETE FROM AM_API_RESOURCE_ENDPOINT WHERE API_ID = ?"; Set<String> endpoints = new HashSet(); getUriTemplates(connection, apiId).forEach((k, v) -> { try {/*ww w . ja v a 2 s .c o m*/ Map<String, Endpoint> apiEndPointMap = getEndPointsForOperation(connection, apiId, v.getTemplateId()); apiEndPointMap.forEach((k1, v1) -> { if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(v1.getApplicableLevel())) { endpoints.add(v1.getId()); } }); } catch (SQLException | IOException e) { log.error("Couldn't retrieve UriTemplates for api : " + apiId, e); } }); try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, apiId); preparedStatement.execute(); endpoints.forEach((value) -> { try { if (!isEndpointAssociated(connection, value)) { deleteEndpoint(connection, value); } } catch (SQLException e) { log.error("Couldn't delete Endpoint", e); } }); } }
From source file:org.wso2.carbon.apimgt.core.dao.impl.ApiDAOImpl.java
private void deleteEndPointsForApi(Connection connection, String apiId) throws SQLException, IOException { final String query = "DELETE FROM AM_API_ENDPOINT_MAPPING WHERE API_ID = ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { Map<String, Endpoint> apiEndPointMap = getEndPointsForApi(connection, apiId); preparedStatement.setString(1, apiId); preparedStatement.execute();//w ww . j a v a 2 s . co m apiEndPointMap.forEach((k, v) -> { if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(v.getApplicableLevel())) { try { if (!isEndpointAssociated(connection, k)) { deleteEndpoint(connection, v.getId()); } } catch (SQLException e) { log.error("Endpoint Couldn't Delete", e); } } }); } }
From source file:org.wso2.carbon.apimgt.core.impl.APIPublisherImplTestCase.java
@Test(description = "Test add api with production endpoint") public void testUpdateApiEndpointOfUriTemplate() throws APIManagementException { /**// w w w.j a v a 2 s .c om * this test method verify the API Add with correct API object get invoked correctly */ Endpoint endpoint1 = new Endpoint.Builder().id(UUID.randomUUID().toString()) .endpointConfig("http://localhost").name("endpoint1") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Endpoint endpoint2 = new Endpoint.Builder().id(UUID.randomUUID().toString()) .endpointConfig("http://localhost").name("endpoint2") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Map<String, Endpoint> endpointMap = new HashMap<>(); endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, endpoint1); endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, endpoint2); Map<String, UriTemplate> uriTemplateMap = SampleTestObjectCreator.getMockUriTemplates(); uriTemplateMap.forEach((s, uriTemplate) -> uriTemplateMap.replace(s, new UriTemplate.UriTemplateBuilder(uriTemplate).endpoint(endpointMap).build())); API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id(UUID.randomUUID().toString()) .endpoint(Collections.emptyMap()).uriTemplates(uriTemplateMap); apiBuilder.apiPermission(""); apiBuilder.permissionMap(null); apiBuilder.policies(Collections.emptySet()); apiBuilder.apiPolicy(null); ApiDAO apiDAO = Mockito.mock(ApiDAO.class); Mockito.when(apiDAO.getAPI(apiBuilder.getId())).thenReturn(apiBuilder.build()); GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class); APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class); APIGateway gateway = Mockito.mock(APIGateway.class); PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class); Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY)); APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO); Mockito.when(apiDAO.getEndpoint(endpoint1.getId())).thenReturn(endpoint1); Mockito.when(apiDAO.getEndpoint(endpoint2.getId())).thenReturn(endpoint2); Mockito.when(apiDAO.getEndpoint(endpoint1.getName())).thenReturn(endpoint1); Mockito.when(apiDAO.getEndpointByName(endpoint2.getName())).thenReturn(endpoint2); apiPublisher.updateAPI(apiBuilder); Mockito.verify(apiDAO, Mockito.times(1)).updateAPI(apiBuilder.getId(), apiBuilder.build()); }
From source file:org.wso2.carbon.apimgt.core.impl.APIPublisherImplTestCase.java
@Test(description = "Test add api with Api Specific Endpoint", expectedExceptions = { APIManagementException.class }) public void testAddResourceLevelEndpointWhileResourceEndpointAlreadyExists() throws APIManagementException, LifecycleException { /**//w w w. ja va 2 s . c om * this test method verify the API Add with correct API object get invoked correctly */ ApiDAO apiDAO = Mockito.mock(ApiDAO.class); GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class); APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class); Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint") .applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build(); Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Endpoint resourceEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("resourceEndpoint") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Map<String, Endpoint> endpointMap = new HashMap(); endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint); endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint); Map<String, Endpoint> resourceEndpoints = new HashMap(); resourceEndpoints.put(APIMgtConstants.SANDBOX_ENDPOINT, resourceEndpoint); Map<String, UriTemplate> uriTemplateMap = SampleTestObjectCreator.getMockUriTemplates(); uriTemplateMap.forEach((k, v) -> { UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(v); uriTemplateBuilder.endpoint(resourceEndpoints); uriTemplateMap.replace(k, uriTemplateBuilder.build()); }); API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap) .uriTemplates(uriTemplateMap); Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)) .thenReturn(new LifecycleState()); APIGateway gateway = Mockito.mock(APIGateway.class); APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway); Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint); Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenReturn(null); Mockito.when(apiDAO.getEndpointByName(resourceEndpoint.getName())).thenReturn(resourceEndpoint); Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false); apiPublisher.addAPI(apiBuilder); Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build()); Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName()); Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(resourceEndpoint.getName()); }
From source file:org.wso2.carbon.apimgt.core.impl.APIPublisherImplTestCase.java
@Test(description = "Test add api with Api Specific Endpoint", expectedExceptions = { APIManagementException.class }) public void testAddResourceLevelEndpointWhileResourceEndpointAlreadyExistsWhileDatabaseFailure() throws APIManagementException, LifecycleException { /**/*from www .ja v a2 s . com*/ * this test method verify the API Add with correct API object get invoked correctly */ ApiDAO apiDAO = Mockito.mock(ApiDAO.class); GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class); APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class); Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint") .applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build(); Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Endpoint resourceEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("resourceEndpoint") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Map<String, Endpoint> endpointMap = new HashMap(); endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint); endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint); Map<String, Endpoint> resourceEndpoints = new HashMap(); resourceEndpoints.put(APIMgtConstants.SANDBOX_ENDPOINT, resourceEndpoint); Map<String, UriTemplate> uriTemplateMap = SampleTestObjectCreator.getMockUriTemplates(); uriTemplateMap.forEach((k, v) -> { UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(v); uriTemplateBuilder.endpoint(resourceEndpoints); uriTemplateMap.replace(k, uriTemplateBuilder.build()); }); API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap) .uriTemplates(uriTemplateMap); Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)) .thenReturn(new LifecycleState()); APIGateway gateway = Mockito.mock(APIGateway.class); APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway); Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint); Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenReturn(null); Mockito.when(apiDAO.getEndpointByName(resourceEndpoint.getName())).thenThrow(APIMgtDAOException.class); Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false); apiPublisher.addAPI(apiBuilder); Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build()); Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName()); Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(resourceEndpoint.getName()); }
From source file:org.wso2.carbon.apimgt.core.impl.APIPublisherImplTestCase.java
@Test(description = "Test add api with Api Specific Endpoint") public void testAddResourceLevelEndpoint() throws APIManagementException, LifecycleException { /**/*from w w w. j av a2s .c o m*/ * this test method verify the API Add with correct API object get invoked correctly */ ApiDAO apiDAO = Mockito.mock(ApiDAO.class); GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class); APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class); Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint") .applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build(); Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Endpoint resourceEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("resourceEndpoint") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Map<String, Endpoint> endpointMap = new HashMap(); endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint); endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint); Map<String, Endpoint> resourceEndpoints = new HashMap(); resourceEndpoints.put(APIMgtConstants.SANDBOX_ENDPOINT, resourceEndpoint); Map<String, UriTemplate> uriTemplateMap = SampleTestObjectCreator.getMockUriTemplates(); uriTemplateMap.forEach((k, v) -> { UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(v); uriTemplateBuilder.endpoint(resourceEndpoints); uriTemplateMap.replace(k, uriTemplateBuilder.build()); }); API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap) .uriTemplates(uriTemplateMap); Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)) .thenReturn(new LifecycleState()); APIGateway gateway = Mockito.mock(APIGateway.class); PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class); Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY)); Mockito.when( policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)) .thenReturn(new SubscriptionPolicy(GOLD_TIER)); Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER)); Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER)); APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO); Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint); Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenReturn(null); Mockito.when(apiDAO.getEndpointByName(resourceEndpoint.getName())).thenReturn(null); Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false); apiPublisher.addAPI(apiBuilder); Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build()); Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName()); Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(resourceEndpoint.getName()); }
From source file:org.wso2.carbon.apimgt.core.impl.APIPublisherImplTestCase.java
@Test(description = "Test add api with Api Specific Endpoint") public void testResourceProductionAndSandboxEndpoint() throws APIManagementException, LifecycleException { /**//from ww w . java2s . c o m * this test method verify the API Add with correct API object get invoked correctly */ ApiDAO apiDAO = Mockito.mock(ApiDAO.class); GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class); APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class); Endpoint globalEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("testEndpoint") .applicableLevel(APIMgtConstants.GLOBAL_ENDPOINT).build(); Endpoint apiEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("apiEndpoint") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Endpoint resourceEndpoint = new Endpoint.Builder().id(UUID.randomUUID().toString()).name("resourceEndpoint") .applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Endpoint resourceEndpoint1 = new Endpoint.Builder().id(UUID.randomUUID().toString()) .name("resourceEndpoint1").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build(); Map<String, Endpoint> endpointMap = new HashMap(); endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, globalEndpoint); endpointMap.put(APIMgtConstants.SANDBOX_ENDPOINT, apiEndpoint); Map<String, Endpoint> resourceEndpoints = new HashMap(); resourceEndpoints.put(APIMgtConstants.SANDBOX_ENDPOINT, resourceEndpoint); resourceEndpoints.put(APIMgtConstants.PRODUCTION_ENDPOINT, resourceEndpoint1); Map<String, UriTemplate> uriTemplateMap = SampleTestObjectCreator.getMockUriTemplates(); uriTemplateMap.forEach((k, v) -> { UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(v); uriTemplateBuilder.endpoint(resourceEndpoints); uriTemplateMap.replace(k, uriTemplateBuilder.build()); }); API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().endpoint(endpointMap) .uriTemplates(uriTemplateMap); Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)) .thenReturn(new LifecycleState()); APIGateway gateway = Mockito.mock(APIGateway.class); PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class); Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY)); Mockito.when( policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)) .thenReturn(new SubscriptionPolicy(GOLD_TIER)); Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER)); Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER)); APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway, policyDAO); Mockito.when(apiDAO.getEndpoint(globalEndpoint.getId())).thenReturn(globalEndpoint); Mockito.when(apiDAO.getEndpointByName(apiEndpoint.getName())).thenReturn(null); Mockito.when(apiDAO.getEndpointByName(resourceEndpoint.getName())).thenReturn(null); Mockito.when(apiDAO.isAPINameExists(apiBuilder.getName(), USER)).thenReturn(false); apiPublisher.addAPI(apiBuilder); Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build()); Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(apiEndpoint.getName()); Mockito.verify(apiDAO, Mockito.times(1)).getEndpointByName(resourceEndpoint.getName()); }
From source file:org.opencb.opencga.app.cli.main.OpenCGAMainOld.java
private int runCommand(OptionsParser optionsParser) throws Exception { int returnValue = 0; if (catalogManager == null && !optionsParser.getSubCommand().isEmpty()) { CatalogConfiguration catalogConfiguration = CatalogConfiguration.load(new FileInputStream( Paths.get(Config.getOpenCGAHome(), "conf", "catalog-configuration.yml").toFile())); catalogManager = new CatalogManager(catalogConfiguration); }//from w ww . j ava2 s . c o m String sessionId = login(optionsParser.getUserAndPasswordOptions()); switch (optionsParser.getCommand()) { case "users": switch (optionsParser.getSubCommand()) { case "create": { OptionsParser.UserCommands.CreateCommand c = optionsParser.getUserCommands().createCommand; //QueryResult<User> user = catalogManager.insertUser(new User(c.up.user, c.name, c.email, c.up.password, c.organization, User.Role.USER, "")); QueryResult<User> user = catalogManager.createUser(c.user, c.name, c.email, c.password, c.organization, null, null); System.out.println(createOutput(c.cOpt, user, null)); break; } case "info": { OptionsParser.UserCommands.InfoCommand c = optionsParser.getUserCommands().infoCommand; QueryResult<User> user = catalogManager.getUser( c.up.user != null ? c.up.user : catalogManager.getUserIdBySessionId(sessionId), null, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, user, null)); break; } case "list": { OptionsParser.UserCommands.ListCommand c = optionsParser.getUserCommands().listCommand; String indent = ""; User user = catalogManager.getUser( c.up.user != null ? c.up.user : catalogManager.getUserIdBySessionId(sessionId), null, new QueryOptions("include", Arrays.asList("id", "name", "projects.id", "projects.alias", "projects.name")), sessionId).first(); System.out.println(user.getId() + " - " + user.getName()); indent += "\t"; System.out.println(listProjects(user.getProjects(), c.recursive ? c.level : 1, indent, c.uries, new StringBuilder(), sessionId)); break; } case "login": { OptionsParser.UserCommands.LoginCommand c = optionsParser.getUserCommands().loginCommand; // if (c.up.user == null || c.up.user.isEmpty()) { // throw new CatalogException("Required userId"); // } shellSessionId = sessionId; logoutAtExit = false; if (shellSessionId != null) { shellUserId = c.up.user; } if (sessionFile == null) { sessionFile = new SessionFile(); } sessionFile.setSessionId(sessionId); sessionFile.setUserId(catalogManager.getUserIdBySessionId(sessionId)); saveUserFile(sessionFile); System.out.println(shellSessionId); break; } case "logout": { OptionsParser.UserCommands.LogoutCommand c = optionsParser.getUserCommands().logoutCommand; QueryResult logout; if (c.sessionId == null) { //Logout from interactive shell logout = catalogManager.logout(shellUserId, shellSessionId); shellUserId = null; shellSessionId = null; if (sessionIdFromFile) { sessionFile.setSessionId(null); sessionFile.setUserId(null); saveUserFile(sessionFile); } } else { String userId = catalogManager.getUserIdBySessionId(c.sessionId); logout = catalogManager.logout(userId, c.sessionId); } logoutAtExit = false; System.out.println(logout); break; } default: optionsParser.printUsage(); break; } break; case "projects": switch (optionsParser.getSubCommand()) { case "create": { OptionsParser.ProjectCommands.CreateCommand c = optionsParser.getProjectCommands().createCommand; String user = c.up.user == null || c.up.user.isEmpty() ? catalogManager.getUserIdBySessionId(sessionId) : c.up.user; QueryResult<Project> project = catalogManager.createProject(c.name, c.alias, c.description, c.organization, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, project, null)); break; } case "info": { OptionsParser.ProjectCommands.InfoCommand c = optionsParser.getProjectCommands().infoCommand; long projectId = catalogManager.getProjectId(c.id); QueryResult<Project> project = catalogManager.getProject(projectId, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, project, null)); break; } // case "share": { // OptionsParser.CommandShareResource c = optionsParser.commandShareResource; // // int projectId = catalogManager.getProjectId(c.id); // QueryResult result = catalogManager.shareProject(projectId, new AclEntry(c.user, c.read, c.write, c.execute, c.delete), sessionId); // System.out.println(createOutput(c.cOpt, result, null)); // // break; // } default: optionsParser.printUsage(); break; } break; case "studies": switch (optionsParser.getSubCommand()) { case "create": { OptionsParser.StudyCommands.CreateCommand c = optionsParser.getStudyCommands().createCommand; URI uri = null; if (c.uri != null && !c.uri.isEmpty()) { uri = UriUtils.createUri(c.uri); } Map<File.Bioformat, DataStore> dataStoreMap = parseBioformatDataStoreMap(c); long projectId = catalogManager.getProjectId(c.projectId); ObjectMap attributes = new ObjectMap(); attributes.put(VariantStorageManager.Options.AGGREGATED_TYPE.key(), c.aggregated.toString()); QueryResult<Study> study = catalogManager.createStudy(projectId, c.name, c.alias, c.type, null, c.description, null, null, null, uri, dataStoreMap, null, attributes, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); if (uri != null) { File root = catalogManager.searchFile(study.first().getId(), new Query(FileDBAdaptor.QueryParams.PATH.key(), ""), sessionId).first(); new FileScanner(catalogManager).scan(root, uri, FileScanner.FileScannerPolicy.REPLACE, true, false, sessionId); } System.out.println(createOutput(c.cOpt, study, null)); break; } case "resync": { OptionsParser.StudyCommands.ResyncCommand c = optionsParser.getStudyCommands().resyncCommand; long studyId = catalogManager.getStudyId(c.id); Study study = catalogManager.getStudy(studyId, sessionId).first(); FileScanner fileScanner = new FileScanner(catalogManager); List<File> scan = fileScanner.reSync(study, c.calculateChecksum, sessionId); System.out.println(createOutput(c.cOpt, scan, null)); break; } case "check-files": { OptionsParser.StudyCommands.CheckCommand c = optionsParser.getStudyCommands().checkCommand; long studyId = catalogManager.getStudyId(c.id); Study study = catalogManager.getStudy(studyId, sessionId).first(); FileScanner fileScanner = new FileScanner(catalogManager); List<File> check = fileScanner.checkStudyFiles(study, c.calculateChecksum, sessionId); System.out.println(createOutput(c.cOpt, check, null)); break; } case "info": { OptionsParser.StudyCommands.InfoCommand c = optionsParser.getStudyCommands().infoCommand; long studyId = catalogManager.getStudyId(c.id); QueryResult<Study> study = catalogManager.getStudy(studyId, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, study, null)); break; } case "list": { OptionsParser.StudyCommands.ListCommand c = optionsParser.getStudyCommands().listCommand; long studyId = catalogManager.getStudyId(c.id); List<Study> studies = catalogManager.getStudy(studyId, sessionId).getResult(); String indent = ""; System.out.println(listStudies(studies, c.recursive ? c.level : 1, indent, c.uries, new StringBuilder(), sessionId)); break; } case "status": { OptionsParser.StudyCommands.StatusCommand c = optionsParser.getStudyCommands().statusCommand; long studyId = catalogManager.getStudyId(c.id); Study study = catalogManager.getStudy(studyId, sessionId).first(); FileScanner fileScanner = new FileScanner(catalogManager); /** First, run CheckStudyFiles to find new missing files **/ List<File> checkStudyFiles = fileScanner.checkStudyFiles(study, false, sessionId); List<File> found = checkStudyFiles.stream() .filter(f -> f.getStatus().getName().equals(File.FileStatus.READY)) .collect(Collectors.toList()); int maxFound = found.stream().map(f -> f.getPath().length()).max(Comparator.<Integer>naturalOrder()) .orElse(0); /** Get untracked files **/ // List<URI> untrackedFiles = fileScanner.untrackedFiles(study, sessionId); // // URI studyUri = catalogManager.getStudyUri(studyId); // Map<URI, String> relativeUrisMap = untrackedFiles.stream().collect(Collectors.toMap((k) -> k, (u) -> studyUri.relativize(u).toString())); Map<String, URI> relativeUrisMap = fileScanner.untrackedFiles(study, sessionId); int maxUntracked = relativeUrisMap.keySet().stream().map(String::length) .max(Comparator.<Integer>naturalOrder()).orElse(0); /** Get missing files **/ List<File> missingFiles = catalogManager.getAllFiles(studyId, new Query(FileDBAdaptor.QueryParams.FILE_STATUS.key(), File.FileStatus.MISSING), new QueryOptions(), sessionId).getResult(); int maxMissing = missingFiles.stream().map(f -> f.getPath().length()) .max(Comparator.<Integer>naturalOrder()).orElse(0); /** Print pretty **/ String format = "\t%-" + Math.max(Math.max(maxMissing, maxUntracked), maxFound) + "s -> %s\n"; if (!relativeUrisMap.isEmpty()) { System.out.println("UNTRACKED files"); relativeUrisMap.forEach((s, u) -> System.out.printf(format, s, u)); System.out.println("\n"); } if (!missingFiles.isEmpty()) { System.out.println("MISSING files"); for (File file : missingFiles) { System.out.printf(format, file.getPath(), catalogManager.getFileUri(file)); } System.out.println("\n"); } if (!found.isEmpty()) { System.out.println("FOUND files"); for (File file : found) { System.out.printf(format, file.getPath(), catalogManager.getFileUri(file)); } } break; } case "annotate-variants": { OptionsParser.StudyCommands.AnnotationCommand c = optionsParser .getStudyCommands().annotationCommand; VariantStorage variantStorage = new VariantStorage(catalogManager); long studyId = catalogManager.getStudyId(c.id); long outdirId = catalogManager.getFileId(c.outdir); QueryOptions queryOptions = new QueryOptions(c.cOpt.getQueryOptions()); queryOptions.put(ExecutorManager.EXECUTE, !c.enqueue); queryOptions.add(AnalysisFileIndexer.PARAMETERS, c.dashDashParameters); queryOptions.add(AnalysisFileIndexer.LOG_LEVEL, logLevel); System.out.println(createOutput(c.cOpt, variantStorage.annotateVariants(studyId, outdirId, sessionId, queryOptions), null)); break; } // case "share": { // OptionsParser.CommandShareResource c = optionsParser.commandShareResource; // // int studyId = catalogManager.getStudyId(c.id); // QueryResult result = catalogManager.shareProject(studyId, new AclEntry(c.user, c.read, c.write, c.execute, c.delete), sessionId); // System.out.println(createOutput(c.cOpt, result, null)); // // break; // } default: optionsParser.printUsage(); break; } break; case "files": { switch (optionsParser.getSubCommand()) { case "create": { OptionsParser.FileCommands.CreateCommand c = optionsParser.getFileCommands().createCommand; long studyId = catalogManager.getStudyId(c.studyId); Path inputFile = Paths.get(c.inputFile); URI sourceUri = new URI(null, c.inputFile, null); if (sourceUri.getScheme() == null || sourceUri.getScheme().isEmpty()) { sourceUri = inputFile.toUri(); } if (!catalogManager.getCatalogIOManagerFactory().get(sourceUri).exists(sourceUri)) { throw new IOException("File " + sourceUri + " does not exist"); } QueryResult<File> file = catalogManager.createFile(studyId, c.format, c.bioformat, Paths.get(c.path, inputFile.getFileName().toString()).toString(), c.description, c.parents, -1, sessionId); new CatalogFileUtils(catalogManager).upload(sourceUri, file.first(), null, sessionId, false, false, c.move, c.calculateChecksum); FileMetadataReader.get(catalogManager).setMetadataInformation(file.first(), null, new QueryOptions(c.cOpt.getQueryOptions()), sessionId, false); System.out.println(createOutput(c.cOpt, file, null)); break; } case "create-folder": { OptionsParser.FileCommands.CreateFolderCommand c = optionsParser .getFileCommands().createFolderCommand; long studyId = catalogManager.getStudyId(c.studyId); QueryResult<File> folder = catalogManager.createFolder(studyId, Paths.get(c.path), c.parents, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, folder, null)); break; } case "upload": { OptionsParser.FileCommands.UploadCommand c = optionsParser.getFileCommands().uploadCommand; URI sourceUri = new URI(null, c.inputFile, null); if (sourceUri.getScheme() == null || sourceUri.getScheme().isEmpty()) { sourceUri = Paths.get(c.inputFile).toUri(); } if (!catalogManager.getCatalogIOManagerFactory().get(sourceUri).exists(sourceUri)) { throw new IOException("File " + sourceUri + " does not exist"); } long fileId = catalogManager.getFileId(c.id); QueryResult<File> file = catalogManager.getFile(fileId, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); new CatalogFileUtils(catalogManager).upload(sourceUri, file.first(), null, sessionId, c.replace, c.replace, c.move, c.calculateChecksum); System.out.println(createOutput(c.cOpt, catalogManager.getFile(file.first().getId(), new QueryOptions(c.cOpt.getQueryOptions()), sessionId), null)); break; } case "link": { OptionsParser.FileCommands.LinkCommand c = optionsParser.getFileCommands().linkCommand; Path inputFile = Paths.get(c.inputFile); URI inputUri = UriUtils.createUri(c.inputFile); CatalogIOManager ioManager = catalogManager.getCatalogIOManagerFactory().get(inputUri); if (!ioManager.exists(inputUri)) { throw new FileNotFoundException("File " + inputUri + " not found"); } // long studyId = catalogManager.getStudyId(c.studyId); String path = c.path.isEmpty() ? inputFile.getFileName().toString() : Paths.get(c.path, inputFile.getFileName().toString()).toString(); File file; CatalogFileUtils catalogFileUtils = new CatalogFileUtils(catalogManager); if (ioManager.isDirectory(inputUri)) { ObjectMap params = new ObjectMap("parents", c.parents); file = catalogManager.link(inputUri, c.path, c.studyId, params, sessionId).first(); // file = catalogFileUtils.linkFolder(studyId, path, c.parents, c.description, c.calculateChecksum, inputUri, false, false, sessionId); new FileScanner(catalogManager).scan(file, null, FileScanner.FileScannerPolicy.REPLACE, c.calculateChecksum, false, sessionId); } else { ObjectMap params = new ObjectMap("parents", c.parents); file = catalogManager.link(inputUri, c.path, c.studyId, params, sessionId).first(); // file = catalogManager.createFile(studyId, null, null, // path, c.description, c.parents, -1, sessionId).first(); // file = catalogFileUtils.link(file, c.calculateChecksum, inputUri, false, false, sessionId); file = FileMetadataReader.get(catalogManager).setMetadataInformation(file, null, new QueryOptions(c.cOpt.getQueryOptions()), sessionId, false); } System.out.println(createOutput(c.cOpt, file, null)); break; } case "relink": { OptionsParser.FileCommands.RelinkCommand c = optionsParser.getFileCommands().relinkCommand; Path inputFile = Paths.get(c.inputFile); URI uri = UriUtils.createUri(c.inputFile); if (!inputFile.toFile().exists()) { throw new FileNotFoundException("File " + uri + " not found"); } long fileId = catalogManager.getFileId(c.id, sessionId); File file = catalogManager.getFile(fileId, sessionId).first(); new CatalogFileUtils(catalogManager).link(file, c.calculateChecksum, uri, false, true, sessionId); file = catalogManager.getFile(file.getId(), new QueryOptions(c.cOpt.getQueryOptions()), sessionId) .first(); file = FileMetadataReader.get(catalogManager).setMetadataInformation(file, null, new QueryOptions(c.cOpt.getQueryOptions()), sessionId, false); System.out.println(createOutput(c.cOpt, file, null)); break; } case "refresh": { OptionsParser.FileCommands.RefreshCommand c = optionsParser.getFileCommands().refreshCommand; long fileId = catalogManager.getFileId(c.id); File file = catalogManager.getFile(fileId, sessionId).first(); List<File> files; QueryOptions queryOptions = new QueryOptions(c.cOpt.getQueryOptions()); CatalogFileUtils catalogFileUtils = new CatalogFileUtils(catalogManager); FileMetadataReader fileMetadataReader = FileMetadataReader.get(catalogManager); if (file.getType() == File.Type.FILE) { File file1 = catalogFileUtils.checkFile(file, false, sessionId); file1 = fileMetadataReader.setMetadataInformation(file1, null, queryOptions, sessionId, false); if (file == file1) { //If the file is the same, it was not modified. Only return modified files. files = Collections.emptyList(); } else { files = Collections.singletonList(file); } } else { List<File> result = catalogManager.getAllFilesInFolder(file.getId(), null, sessionId) .getResult(); files = new ArrayList<>(result.size()); for (File f : result) { File file1 = fileMetadataReader.setMetadataInformation(f, null, queryOptions, sessionId, false); if (f != file1) { //Add only modified files. files.add(file1); } } } System.out.println(createOutput(c.cOpt, files, null)); break; } case "info": { OptionsParser.FileCommands.InfoCommand c = optionsParser.getFileCommands().infoCommand; long fileId = catalogManager.getFileId(c.id); QueryResult<File> file = catalogManager.getFile(fileId, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(optionsParser.getCommonOptions(), file, null)); break; } case "search": { OptionsParser.FileCommands.SearchCommand c = optionsParser.getFileCommands().searchCommand; long studyId = catalogManager.getStudyId(c.studyId); Query query = new Query(); if (c.name != null) query.put(FileDBAdaptor.QueryParams.NAME.key(), "~" + c.name); if (c.directory != null) query.put(FileDBAdaptor.QueryParams.DIRECTORY.key(), c.directory); if (c.bioformats != null) query.put(FileDBAdaptor.QueryParams.BIOFORMAT.key(), c.bioformats); if (c.types != null) query.put(FileDBAdaptor.QueryParams.TYPE.key(), c.types); if (c.status != null) query.put(FileDBAdaptor.QueryParams.STATUS_NAME.key(), c.status); QueryResult<File> fileQueryResult = catalogManager.searchFile(studyId, query, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(optionsParser.getCommonOptions(), fileQueryResult, null)); break; } case "list": { OptionsParser.FileCommands.ListCommand c = optionsParser.getFileCommands().listCommand; long fileId = catalogManager.getFileId(c.id); List<File> result = catalogManager.getFile(fileId, sessionId).getResult(); long studyId = catalogManager.getStudyIdByFileId(fileId); System.out.println(listFiles(result, studyId, c.recursive ? c.level : 1, "", c.uries, new StringBuilder(), sessionId)); break; } case "index": { OptionsParser.FileCommands.IndexCommand c = optionsParser.getFileCommands().indexCommand; AnalysisFileIndexer analysisFileIndexer = new AnalysisFileIndexer(catalogManager); long fileId = catalogManager.getFileId(c.id); long outdirId = catalogManager.getFileId(c.outdir); if (outdirId < 0) { outdirId = catalogManager.getFileParent(fileId, null, sessionId).first().getId(); } String sid = sessionId; QueryOptions queryOptions = new QueryOptions(c.cOpt.getQueryOptions()); if (c.enqueue) { queryOptions.put(ExecutorManager.EXECUTE, false); if (c.up.sessionId == null || c.up.sessionId.isEmpty()) { sid = login(c.up); } } else { queryOptions.add(ExecutorManager.EXECUTE, true); } queryOptions.put(AnalysisFileIndexer.TRANSFORM, c.transform); queryOptions.put(AnalysisFileIndexer.LOAD, c.load); queryOptions.add(AnalysisFileIndexer.PARAMETERS, c.dashDashParameters); queryOptions.add(AnalysisFileIndexer.LOG_LEVEL, logLevel); queryOptions.add(VariantStorageManager.Options.CALCULATE_STATS.key(), c.calculateStats); queryOptions.add(VariantStorageManager.Options.ANNOTATE.key(), c.annotate); logger.debug("logLevel: {}", logLevel); QueryResult<Job> queryResult = analysisFileIndexer.index(fileId, outdirId, sid, queryOptions); System.out.println(createOutput(c.cOpt, queryResult, null)); break; } default: optionsParser.printUsage(); break; } break; } case "samples": { switch (optionsParser.getSubCommand()) { case "info": { OptionsParser.SampleCommands.InfoCommand c = optionsParser.sampleCommands.infoCommand; QueryResult<Sample> sampleQueryResult = catalogManager.getSample(c.id, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, sampleQueryResult, null)); break; } case "search": { OptionsParser.SampleCommands.SearchCommand c = optionsParser.sampleCommands.searchCommand; long studyId = catalogManager.getStudyId(c.studyId); QueryOptions queryOptions = new QueryOptions(c.cOpt.getQueryOptions()); Query query = new Query(); if (c.sampleIds != null && !c.sampleIds.isEmpty()) { query.append(SampleDBAdaptor.QueryParams.ID.key(), c.sampleIds); } if (c.sampleNames != null && !c.sampleNames.isEmpty()) { query.append(SampleDBAdaptor.QueryParams.NAME.key(), c.sampleNames); } if (c.annotation != null && !c.annotation.isEmpty()) { for (String s : c.annotation) { String[] strings = org.opencb.opencga.storage.core.variant.adaptors.VariantDBAdaptorUtils .splitOperator(s); query.append(SampleDBAdaptor.QueryParams.ANNOTATION.key() + "." + strings[0], strings[1] + strings[2]); } } if (c.variableSetId != null && !c.variableSetId.isEmpty()) { query.append(SampleDBAdaptor.QueryParams.VARIABLE_SET_ID.key(), c.variableSetId); } QueryResult<Sample> sampleQueryResult = catalogManager.getAllSamples(studyId, query, queryOptions, sessionId); System.out.println(createOutput(c.cOpt, sampleQueryResult, null)); break; } case "load": { OptionsParser.SampleCommands.LoadCommand c = optionsParser.sampleCommands.loadCommand; CatalogSampleAnnotationsLoader catalogSampleAnnotationsLoader = new CatalogSampleAnnotationsLoader( catalogManager); long fileId = catalogManager.getFileId(c.pedigreeFileId); File pedigreeFile = catalogManager.getFile(fileId, sessionId).first(); QueryResult<Sample> sampleQueryResult = catalogSampleAnnotationsLoader.loadSampleAnnotations( pedigreeFile, c.variableSetId == 0 ? null : c.variableSetId, sessionId); System.out.println(createOutput(c.cOpt, sampleQueryResult, null)); break; } case "delete": { OptionsParser.SampleCommands.DeleteCommand c = optionsParser.sampleCommands.deleteCommand; QueryResult<Sample> sampleQueryResult = catalogManager.deleteSample(c.id, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, sampleQueryResult, null)); break; } default: { optionsParser.printUsage(); break; } } break; } case "cohorts": { switch (optionsParser.getSubCommand()) { case OptionsParser.CohortCommands.InfoCommand.COMMAND_NAME: { OptionsParser.CohortCommands.InfoCommand c = optionsParser.cohortCommands.infoCommand; QueryResult<Cohort> cohortQueryResult = catalogManager.getCohort(c.id, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, cohortQueryResult, null)); break; } case OptionsParser.CohortCommands.SamplesCommand.COMMAND_NAME: { OptionsParser.CohortCommands.SamplesCommand c = optionsParser.cohortCommands.samplesCommand; Cohort cohort = catalogManager.getCohort(c.id, null, sessionId).first(); QueryOptions queryOptions = new QueryOptions(c.cOpt.getQueryOptions()); Query query = new Query(SampleDBAdaptor.QueryParams.ID.key(), cohort.getSamples()); QueryResult<Sample> sampleQueryResult = catalogManager.getAllSamples( catalogManager.getStudyIdByCohortId(cohort.getId()), query, queryOptions, sessionId); OptionsParser.CommonOptions cOpt = c.cOpt; StringBuilder sb = createOutput(cOpt, sampleQueryResult, null); System.out.println(sb.toString()); break; } case OptionsParser.CohortCommands.CreateCommand.COMMAND_NAME: { OptionsParser.CohortCommands.CreateCommand c = optionsParser.cohortCommands.createCommand; Map<String, List<Sample>> cohorts = new HashMap<>(); long studyId = catalogManager.getStudyId(c.studyId); if (c.sampleIds != null && !c.sampleIds.isEmpty()) { QueryOptions queryOptions = new QueryOptions("include", "projects.studies.samples.id"); Query query = new Query(SampleDBAdaptor.QueryParams.ID.key(), c.sampleIds); // queryOptions.put("variableSetId", c.variableSetId); QueryResult<Sample> sampleQueryResult = catalogManager.getAllSamples(studyId, query, queryOptions, sessionId); cohorts.put(c.name, sampleQueryResult.getResult()); } else if (StringUtils.isNotEmpty(c.tagmap)) { List<QueryResult<Cohort>> queryResults = createCohorts(sessionId, studyId, c.tagmap, catalogManager, logger); System.out.println(createOutput(c.cOpt, queryResults, null)); } else { // QueryOptions queryOptions = c.cOpt.getQueryOptions(); // queryOptions.put("annotation", c.annotation); final long variableSetId; final VariableSet variableSet; if (StringUtils.isNumeric(c.variableSet)) { variableSetId = Long.parseLong(c.variableSet); variableSet = catalogManager.getVariableSet(variableSetId, null, sessionId).first(); } else if (StringUtils.isEmpty(c.variableSet)) { List<VariableSet> variableSets = catalogManager.getStudy(studyId, new QueryOptions("include", "projects.studies.variableSets"), sessionId).first() .getVariableSets(); if (!variableSets.isEmpty()) { variableSet = variableSets.get(0); //Get the first VariableSetId variableSetId = variableSet.getId(); } else { throw new CatalogException("Expected variableSetId"); } } else { QueryOptions query = new QueryOptions(StudyDBAdaptor.VariableSetParams.NAME.key(), c.variableSet); variableSet = catalogManager.getAllVariableSet(studyId, query, sessionId).first(); if (variableSet == null) { throw new CatalogException("Variable set \"" + c.variableSet + "\" not found"); } variableSetId = variableSet.getId(); } c.name = ((c.name == null) || c.name.isEmpty()) ? "" : (c.name + "."); for (Variable variable : variableSet.getVariables()) { if (variable.getName().equals(c.variable)) { for (String value : variable.getAllowedValues()) { QueryOptions queryOptions = new QueryOptions(c.cOpt.getQueryOptions()); Query query = new Query( SampleDBAdaptor.QueryParams.ANNOTATION.key() + "." + c.variable, value) .append(SampleDBAdaptor.QueryParams.VARIABLE_SET_ID.key(), variableSetId); QueryResult<Sample> sampleQueryResult = catalogManager.getAllSamples(studyId, query, queryOptions, sessionId); cohorts.put(c.name + value, sampleQueryResult.getResult()); } } } if (cohorts.isEmpty()) { logger.error("VariableSetId {} does not contain any variable with id = {}.", variableSetId, c.variable); returnValue = 2; } } if (!cohorts.isEmpty()) { List<QueryResult<Cohort>> queryResults = new ArrayList<>(cohorts.size()); for (Map.Entry<String, List<Sample>> entry : cohorts.entrySet()) { List<Long> sampleIds = new LinkedList<>(); for (Sample sample : entry.getValue()) { sampleIds.add(sample.getId()); } QueryResult<Cohort> cohort = catalogManager.createCohort(studyId, entry.getKey(), c.type, c.description, sampleIds, c.cOpt.getQueryOptions(), sessionId); queryResults.add(cohort); } System.out.println(createOutput(c.cOpt, queryResults, null)); } // System.out.println(createSamplesOutput(c.cOpt, sampleQueryResult)); break; } case OptionsParser.CohortCommands.StatsCommand.COMMAND_NAME: { OptionsParser.CohortCommands.StatsCommand c = optionsParser.cohortCommands.statsCommand; VariantStorage variantStorage = new VariantStorage(catalogManager); long outdirId = catalogManager.getFileId(c.outdir); QueryOptions queryOptions = new QueryOptions(c.cOpt.getQueryOptions()); if (c.enqueue) { queryOptions.put(ExecutorManager.EXECUTE, false); } else { queryOptions.add(ExecutorManager.EXECUTE, true); } queryOptions.add(AnalysisFileIndexer.PARAMETERS, c.dashDashParameters); queryOptions.add(AnalysisFileIndexer.LOG_LEVEL, logLevel); if (c.tagmap != null) { queryOptions.put(VariantStorageManager.Options.AGGREGATION_MAPPING_PROPERTIES.key(), c.tagmap); } else if (c.cohortIds == null) { logger.error("--cohort-id nor --aggregation-mapping-file provided"); throw new IllegalArgumentException( "--cohort-id or --aggregation-mapping-file is required to specify cohorts"); } System.out.println(createOutput(c.cOpt, variantStorage.calculateStats(outdirId, c.cohortIds, sessionId, queryOptions), null)); break; } default: { optionsParser.printUsage(); break; } } break; } case "jobs": { switch (optionsParser.getSubCommand()) { case "info": { OptionsParser.JobsCommands.InfoCommand c = optionsParser.getJobsCommands().infoCommand; QueryResult<Job> jobQueryResult = catalogManager.getJob(c.id, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, jobQueryResult, null)); break; } case "finished": { OptionsParser.JobsCommands.DoneJobCommand c = optionsParser.getJobsCommands().doneJobCommand; QueryResult<Job> jobQueryResult = catalogManager.getJob(c.id, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); Job job = jobQueryResult.first(); if (c.force) { if (job.getStatus().getName().equals(Job.JobStatus.ERROR) || job.getStatus().getName().equals(Job.JobStatus.READY)) { logger.info("Job status is '{}' . Nothing to do.", job.getStatus().getName()); System.out.println(createOutput(c.cOpt, jobQueryResult, null)); } } else if (!job.getStatus().getName().equals(Job.JobStatus.DONE)) { throw new Exception("Job status != DONE. Need --force to continue"); } /** Record output **/ ExecutionOutputRecorder outputRecorder = new ExecutionOutputRecorder(catalogManager, sessionId); if (c.discardOutput) { String tempJobsDir = catalogManager.getCatalogConfiguration().getTempJobsDir(); URI tmpOutDirUri = IndexDaemon.getJobTemporaryFolder(job.getId(), tempJobsDir).toUri(); CatalogIOManager ioManager = catalogManager.getCatalogIOManagerFactory().get(tmpOutDirUri); if (ioManager.exists(tmpOutDirUri)) { logger.info("Deleting temporal job output folder: {}", tmpOutDirUri); ioManager.deleteDirectory(tmpOutDirUri); } else { logger.info("Temporal job output folder already removed: {}", tmpOutDirUri); } } else { outputRecorder.recordJobOutput(job); } outputRecorder.postProcessJob(job, c.error); /** Change status to ERROR or READY **/ ObjectMap parameters = new ObjectMap(); if (c.error) { parameters.put("status.name", Job.JobStatus.ERROR); parameters.put("error", Job.ERRNO_ABORTED); parameters.put("errorDescription", Job.ERROR_DESCRIPTIONS.get(Job.ERRNO_ABORTED)); } else { parameters.put("status.name", Job.JobStatus.READY); } catalogManager.modifyJob(job.getId(), parameters, sessionId); jobQueryResult = catalogManager.getJob(c.id, new QueryOptions(c.cOpt.getQueryOptions()), sessionId); System.out.println(createOutput(c.cOpt, jobQueryResult, null)); break; } case "status": { OptionsParser.JobsCommands.StatusCommand c = optionsParser.getJobsCommands().statusCommand; final List<Long> studyIds; if (c.studyId == null || c.studyId.isEmpty()) { studyIds = catalogManager .getAllStudies(new Query(), new QueryOptions("include", "id"), sessionId).getResult() .stream().map(Study::getId).collect(Collectors.toList()); } else { studyIds = new LinkedList<>(); for (String s : c.studyId.split(",")) { studyIds.add(catalogManager.getStudyId(s)); } } for (Long studyId : studyIds) { QueryResult<Job> allJobs = catalogManager.getAllJobs(studyId, new Query(JobDBAdaptor.QueryParams.STATUS_NAME.key(), Collections.singletonList(Job.JobStatus.RUNNING.toString())), new QueryOptions(), sessionId); for (Iterator<Job> iterator = allJobs.getResult().iterator(); iterator.hasNext();) { Job job = iterator.next(); System.out.format("Job - %s [%d] - %s\n", job.getName(), job.getId(), job.getDescription()); // URI tmpOutDirUri = job.getTmpOutDirUri(); String tempJobsDir = catalogManager.getCatalogConfiguration().getTempJobsDir(); URI tmpOutDirUri = IndexDaemon.getJobTemporaryFolder(job.getId(), tempJobsDir).toUri(); CatalogIOManager ioManager = catalogManager.getCatalogIOManagerFactory().get(tmpOutDirUri); try { ioManager.listFilesStream(tmpOutDirUri).sorted().forEach(uri -> { String count; try { long fileSize = ioManager.getFileSize(uri); count = humanReadableByteCount(fileSize, false); } catch (CatalogIOException e) { count = "ERROR"; } System.out.format("\t%s [%s]\n", tmpOutDirUri.relativize(uri), count); }); } catch (CatalogIOException e) { System.out.println("Unable to read files from " + tmpOutDirUri + " - " + e.getCause().getMessage()); } if (iterator.hasNext()) { System.out.println("-----------------------------------------"); } } } break; } case "run": { OptionsParser.JobsCommands.RunJobCommand c = optionsParser.getJobsCommands().runJobCommand; long studyId = catalogManager.getStudyId(c.studyId); long outdirId = catalogManager.getFileId(c.outdir); long toolId = catalogManager.getToolId(c.toolId); String toolName; ToolManager toolManager; if (toolId < 0) { toolManager = new ToolManager(c.toolId, c.execution); //LEGACY MODE, AVOID USING toolName = c.toolId; } else { Tool tool = catalogManager.getTool(toolId, sessionId).getResult().get(0); toolManager = new ToolManager(Paths.get(tool.getPath()).getParent(), tool.getName(), c.execution); toolName = tool.getName(); } List<Long> inputFiles = new LinkedList<>(); Map<String, List<String>> localParams = new HashMap<>(); for (String key : c.params.keySet()) { localParams.put(key, c.params.getAsStringList(key)); } Execution ex = toolManager.getExecution(); // Set input param for (InputParam inputParam : ex.getInputParams()) { if (c.params.containsKey(inputParam.getName())) { List<String> filePaths = new LinkedList<>(); for (String fileId : c.params.getAsStringList(inputParam.getName())) { File file = catalogManager.getFile(catalogManager.getFileId(fileId), sessionId) .getResult().get(0); filePaths.add(catalogManager.getFileUri(file).getPath()); inputFiles.add(file.getId()); } localParams.put(inputParam.getName(), filePaths); } } // Set outdir String outputParam = toolManager.getExecution().getOutputParam(); File outdir = catalogManager.getFile(outdirId, sessionId).first(); localParams.put(outputParam, Collections.singletonList(catalogManager.getFileUri(outdir).getPath())); QueryResult<Job> jobQueryResult = new JobFactory(catalogManager).createJob(toolManager, localParams, studyId, c.name, c.description, outdir, inputFiles, sessionId, true); System.out.println(createOutput(c.cOpt, jobQueryResult, null)); break; } default: { optionsParser.printUsage(); break; } } break; } case "tools": { switch (optionsParser.getSubCommand()) { case "create": { OptionsParser.ToolCommands.CreateCommand c = optionsParser.getToolCommands().createCommand; Path path = Paths.get(c.path); FileUtils.checkDirectory(path); QueryResult<Tool> tool = catalogManager.createTool(c.alias, c.description, null, null, path.toAbsolutePath().toString(), c.openTool, sessionId); System.out.println(createOutput(c.cOpt, tool, null)); break; } case "info": { OptionsParser.ToolCommands.InfoCommand c = optionsParser.getToolCommands().infoCommand; long toolId = catalogManager.getToolId(c.id); ToolManager toolManager; String toolName; if (toolId < 0) { toolManager = new ToolManager(c.id, null); //LEGACY MODE, AVOID USING toolName = c.id; System.out.println(createOutput(c.cOpt, toolManager.getManifest(), null)); } else { Tool tool = catalogManager.getTool(toolId, sessionId).getResult().get(0); toolManager = new ToolManager(Paths.get(tool.getPath()).getParent(), tool.getName(), null); toolName = tool.getName(); System.out.println(createOutput(c.cOpt, tool, null)); } break; } default: { optionsParser.printUsage(); break; } } break; } case "exit": { } break; case "help": default: optionsParser.printUsage(); // logger.info("Unknown command"); break; } logout(sessionId); return returnValue; }
From source file:net.dstc.mkts.data.jpa.JpaSurveyDAO.java
private Query buildQuery(SurveyDO query) { String queryString = "FROM survey s"; String whereClause = StringUtils.EMPTY; Map<String, Object> parameters = new Hashtable<>(20); if (query != null) { String title = query.getTitle(); SurveyStatus status = query.getStatus(); Date startDate = query.getStartDate(); SurveyTargetDO target = query.getTarget(); if (!StringUtils.isBlank(title)) { whereClause = "title=:title"; parameters.put("title", title); }/*from w ww . j ava2s. c o m*/ if (status != null) { whereClause += !StringUtils.isEmpty(whereClause) ? " AND " : StringUtils.EMPTY; whereClause += "status=:status"; parameters.put("status", status); } if (startDate != null) { whereClause += !StringUtils.isEmpty(whereClause) ? " AND " : StringUtils.EMPTY; whereClause += "startDate=:startDate"; parameters.put("startDate", startDate); } if (target != null) { int minAge = target.getMinAge(); int maxAge = target.getMaxAge(); int minIncome = target.getMinIncome(); int maxIncome = target.getMaxIncome(); String gender = target.getGender(); String country = target.getCountry(); if (minAge > 0) { whereClause += !StringUtils.isEmpty(whereClause) ? " AND " : StringUtils.EMPTY; whereClause += "target.minAge=:minAge"; parameters.put("target.minAge", minAge); } if (maxAge > 0) { whereClause += !StringUtils.isEmpty(whereClause) ? " AND " : StringUtils.EMPTY; whereClause += "target.maxAge=:maxAge"; parameters.put("target.maxAge", maxAge); } if (minIncome > 0) { whereClause += !StringUtils.isEmpty(whereClause) ? " AND " : StringUtils.EMPTY; whereClause += "target.minIncome=:minIncome"; parameters.put("target.minIncome", minIncome); } if (maxIncome > 0) { whereClause += !StringUtils.isEmpty(whereClause) ? " AND " : StringUtils.EMPTY; whereClause += "target.maxIncome=:maxIncome"; parameters.put("target.maxIncome", maxIncome); } if (!StringUtils.isBlank(gender)) { whereClause += !StringUtils.isEmpty(whereClause) ? " AND " : StringUtils.EMPTY; whereClause += "target.gender=:gender"; parameters.put("gender", gender); } if (!StringUtils.isBlank(country)) { whereClause += !StringUtils.isEmpty(whereClause) ? " AND " : StringUtils.EMPTY; whereClause += "target.country=:country"; parameters.put("target.country", country); } } if (!StringUtils.isEmpty(whereClause)) { queryString += " WHERE " + whereClause; } } Query jpaQuery = entityManager.createQuery(queryString); parameters.forEach((String parmName, Object parmValue) -> { jpaQuery.setParameter(parmName, parmValue); }); return jpaQuery; }