Example usage for java.net URI relativize

List of usage examples for java.net URI relativize

Introduction

In this page you can find the example usage for java.net URI relativize.

Prototype

public URI relativize(URI uri) 

Source Link

Document

Relativizes the given URI against this URI.

Usage

From source file:org.talend.repository.ui.wizards.exportjob.scriptsmanager.esb.JobJavaScriptOSGIForESBManager.java

/**
 * DOC ycbai Comment method "getJobScriptsUncompressed".
 *
 * @param resource/*  ww w. j  a v a  2  s.  com*/
 * @param process
 * @throws IOException
 */
private void getJobScriptsUncompressed(ExportFileResource resource, ProcessItem process) throws IOException {
    final URI classRootURI = classesLocation.toURI();
    List<String> jobFolderNames = getRelatedJobFolderNames(process);
    try {
        for (String jobFolderName : jobFolderNames) {
            String[] jf = jobFolderName.split(":"); //$NON-NLS-1$
            String projectName = jf[0];
            String folderName = jf[1];
            String classRootLocation = getClassRootLocation() + projectName + File.separator;
            String classRoot = FilesUtils.getFileRealPath(classRootLocation + folderName);
            String targetPath = FilesUtils.getFileRealPath(
                    classesLocation + File.separator + projectName + File.separator + folderName);
            File sourceFile = new File(classRoot);
            File targetFile = new File(targetPath);
            FilesUtils.copyFolder(sourceFile, targetFile, true, null, null, true, false);

            List<URL> fileURLs = FilesUtils.getFileURLs(targetFile);
            for (URL url : fileURLs) {
                resource.addResource(
                        classRootURI.relativize(new File(url.toURI()).getParentFile().toURI()).toString(), url);
            }
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
}

From source file:de.betterform.agent.betty.AppletProcessor.java

/**
 * Updates an upload's value in the internal DOM. <P> The destination
 * parameter will be interpeted relative to the form base and is used as
 * follows: <ol> <li>If it is <code>null</code> or empty, the file will be
 * uploaded to the form base.</li> <li>If it denotes a non-existing file, a
 * corresponding directory will be created and the file will be uploaded to
 * that directory.</li> <li>If it denotes an existing directory, the file
 * will be uploaded to that directory.</li> <li>If it denotes an existing
 * file, the existing file will be overwritten with the uploaded file. Thus,
 * the name of the uploaded file is dropped.</li> </ol> Caution: The latter
 * option is a potential security risk !
 *
 * @param id          the id of the upload control
 * @param value       the absolute name of the file to be uploaded
 * @param type        the optional file mediatype
 * @param destination the optional file destination
 * @throws XFormsException if any error occurred during file uploading.
 *//*  w w  w.  j  a  v  a 2  s  .  com*/
public void setValue(String id, String value, String type, String destination) throws XFormsException {
    try {
        ensureContextClassLoader();

        File source = new File(value);
        String mediatype = type == null ? "" : type;
        String filename = source.getName();
        //System.out.println("upload: " + source + ", mediatype='" + mediatype + "'");

        if (this.xformsProcessor.isFileUpload(id, "anyURI")) {
            // get base uri
            URI baseURI = new URI((String) this.xformsProcessor.getContextParam(XFormsProcessor.BASE_URI));
            File baseFile = new File(baseURI);
            if (!baseFile.isDirectory()) {
                baseURI = baseFile.getParentFile().toURI();
            }

            // get target
            File target;
            if (destination != null && destination.length() > 0) {
                target = new File(baseURI.resolve(destination));
            } else {
                target = new File(baseURI.resolve(this.uploadDir));
            }

            // check directory
            if (!target.exists()) {
                target.mkdirs();
            }
            if (target.isDirectory()) {
                target = new File(target.getAbsolutePath(), filename);
            }

            // copy file
            //System.out.println("upload: copying to " + target);
            FileInputStream fis = new FileInputStream(source);
            FileOutputStream fos = new FileOutputStream(target);
            byte[] buf = new byte[1024];
            int i;
            while ((i = fis.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
            fis.close();
            fos.close();

            // generate relative uri
            URI generated = baseURI.relativize(target.toURI());
            //System.out.println("upload: generated uri " + generated);

            // update control
            this.xformsProcessor.setUploadValue(id, mediatype, filename,
                    generated.toString().getBytes("UTF-8"));
        } else {
            // read binary data into memory
            byte[] data = new byte[(int) source.length()];
            //System.out.println("upload: inlining " + data.length + " bytes");

            FileInputStream stream = new FileInputStream(source);
            stream.read(data);
            stream.close();

            // update control
            this.xformsProcessor.setUploadValue(id, mediatype, filename, data);
        }
    } catch (Exception e) {
        throw new XFormsException("failed to upload at '" + id + "'", e);
    }
}

From source file:org.sakaiproject.content.impl.CollectionAccessFormatter.java

/**
 * Format the collection as an HTML display.
 * Ths ContentHostingService is passed in here to handle the cyclic dependency between the BaseContentService
 * and this class./*from w  ww.  java  2  s  .co  m*/
 */
public void format(ContentCollection x, Reference ref, HttpServletRequest req, HttpServletResponse res,
        ResourceLoader rb, ContentHostingService contentHostingService) {
    // do not allow directory listings for /attachments and its subfolders  
    if (contentHostingService.isAttachmentResource(x.getId())) {
        try {
            res.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        } catch (IOException e) {
            return;
        }
    }

    PrintWriter out = null;

    // don't set the writer until we verify that
    // getallresources is going to work.
    boolean printedHeader = false;
    boolean printedDiv = false;

    try {
        res.setContentType("text/html; charset=UTF-8");

        out = res.getWriter();

        ResourceProperties pl = x.getProperties();
        String webappRoot = serverConfigurationService.getServerUrl();
        String skinRepo = serverConfigurationService.getString("skin.repo", "/library/skin");
        String siteId = null;
        String[] parts = StringUtils.split(x.getId(), Entity.SEPARATOR);

        // Is this a site folder (Resources or Dropbox)? If so, get the site skin
        if (x.getId().startsWith(ContentHostingService.COLLECTION_SITE)
                || x.getId().startsWith(ContentHostingService.COLLECTION_DROPBOX)) {
            if (parts.length > 1) {
                siteId = parts[1];
            }
        }
        String skinName = siteService.getSiteSkin(siteId);

        // Output the headers

        out.println(
                "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
        out.println("<html><head>");
        out.println("<title>"
                + rb.getFormattedMessage("colformat.pagetitle",
                        new Object[] { formattedText
                                .escapeHtml(pl.getProperty(ResourceProperties.PROP_DISPLAY_NAME)) })
                + "</title>");
        out.println("<link href=\"" + webappRoot + skinRepo + "/" + skinName
                + "/access.css\" type=\"text/css\" rel=\"stylesheet\" media=\"screen\">");
        out.println("<script src=\"" + webappRoot + "/library/js/jquery.js\" type=\"text/javascript\">");
        out.println("</script>");
        out.println("</head><body class=\"specialLink\">");

        out.println("<script type=\"text/javascript\" src=\"/library/js/access.js\"></script>");
        out.println("<div class=\"directoryIndex\">");

        // for content listing it's best to use a real title
        out.println("<h3>" + formattedText.escapeHtml(pl.getProperty(ResourceProperties.PROP_DISPLAY_NAME))
                + "</h3>");
        out.println("<p id=\"toggle\"><a id=\"toggler\" href=\"#\">" + rb.getString("colformat.showhide")
                + "</a></p>");
        String folderdesc = pl.getProperty(ResourceProperties.PROP_DESCRIPTION);
        if (folderdesc != null && !folderdesc.equals(""))
            out.println("<div class=\"textPanel\">" + folderdesc + "</div>");

        out.println("<ul>");
        out.println("<li style=\"display:none\">");
        out.println("</li>");

        printedHeader = true;
        printedDiv = true;

        if (parts.length > 2) {
            // go up a level
            out.println(
                    "<li class=\"upfolder\"><a href=\"../\"><img src=\"/library/image/sakai/folder-up.gif\" alt=\""
                            + rb.getString("colformat.uplevel.alttext") + "\"/>"
                            + rb.getString("colformat.uplevel") + "</a></li>");
        }

        // Sort the collection items

        List<ContentEntity> members = x.getMemberResources();

        boolean hasCustomSort = false;
        try {
            hasCustomSort = x.getProperties().getBooleanProperty(ResourceProperties.PROP_HAS_CUSTOM_SORT);
        } catch (Exception e) {
            // use false that's already there
        }

        if (hasCustomSort)
            Collections.sort(members,
                    new ContentHostingComparator(ResourceProperties.PROP_CONTENT_PRIORITY, true));
        else
            Collections.sort(members, new ContentHostingComparator(ResourceProperties.PROP_DISPLAY_NAME, true));

        // Iterate through content items

        URI baseUri = new URI(x.getUrl());

        String hiddenClassParent = x.isAvailable() ? "" : " inactive";
        String hiddenClass;
        for (ContentEntity content : members) {

            hiddenClass = hiddenClassParent;
            ResourceProperties properties = content.getProperties();
            boolean isCollection = content.isCollection();
            String xs = content.getId();
            String contentUrl = content.getUrl();

            // These both perform the same check in the implementation but we should observe the API.
            // This also checks to see if a resource is hidden or time limited.
            if (isCollection) {
                if (!contentHostingService.allowGetCollection(xs)) {
                    continue;
                }
            } else {
                if (!contentHostingService.allowGetResource(xs)) {
                    continue;
                }
            }

            if (isCollection) {
                xs = xs.substring(0, xs.length() - 1);
                xs = xs.substring(xs.lastIndexOf('/') + 1) + '/';
            } else {
                xs = xs.substring(xs.lastIndexOf('/') + 1);
            }

            try {
                // Relativize the URL (canonical item URL relative to canonical collection URL). 
                // Inter alias this will preserve alternate access paths via aliases, e.g. /web/

                URI contentUri = new URI(contentUrl);
                URI relativeUri = baseUri.relativize(contentUri);
                contentUrl = relativeUri.toString();
                if (!content.isAvailable()) {
                    hiddenClass = " inactive";
                }
                String displayName = properties.getProperty(ResourceProperties.PROP_DISPLAY_NAME);

                if (isCollection) {
                    // Folder
                    String desc = properties.getProperty(ResourceProperties.PROP_DESCRIPTION);
                    if (desc == null) {
                        desc = "";
                    } else {
                        desc = "<div class=\"textPanel\">" + desc + "</div>";
                    }
                    StringBuilder li = new StringBuilder("<li class=\"folder\"><a href=\"").append(contentUrl)
                            .append("\"");
                    li.append(" class=\"" + hiddenClass + "\"");
                    li.append(">").append(formattedText.escapeHtml(displayName)).append("</a>").append(desc)
                            .append("</li>");

                    out.println(li.toString());
                } else {
                    // File
                    String desc = properties.getProperty(ResourceProperties.PROP_DESCRIPTION);
                    if (desc == null) {
                        desc = "";
                    } else {
                        desc = "<div class=\"textPanel\">" + formattedText.escapeHtml(desc) + "</div>";
                    }
                    String resourceType = content.getResourceType().replace('.', '_');
                    StringBuilder li = new StringBuilder("<li class=\"file" + hiddenClass + "\"><a href=\"")
                            .append(contentUrl).append("\" target=_blank class=\"");
                    li.append(resourceType);
                    li.append(hiddenClass);
                    li.append("\">");
                    li.append(formattedText.escapeHtml(displayName));
                    li.append("</a>").append(desc).append("</li>");

                    out.println(li.toString());
                }
            } catch (Exception e) {
                M_log.info("Problem rendering item falling back to default rendering: " + x.getId() + ", "
                        + e.getMessage());
                out.println("<li class=\"file\"><a href=\"" + contentUrl + "\" target=_blank>"
                        + formattedText.escapeHtml(xs) + "</a></li>");
            }
        }

    } catch (Exception e) {
        M_log.warn("Problem formatting HTML for collection: " + x.getId(), e);
    }

    if (out != null && printedHeader) {
        out.println("</ul>");

        if (printedDiv)
            out.println("</div>");
        out.println("</body></html>");
    }
}

From source file:org.sparkbit.jsonrpc.SparkBitJSONRPCServiceImpl.java

/**
 * Create and populate a JSONRPCBalance object given an assetID and balance
 * @param w/*from  w w w. ja v  a 2  s .  c o  m*/
 * @param assetID
 * @param totalRaw
 * @param spendableRaw If null, we set the amount field, instead of total and spendable.
 * @return 
 */
private JSONRPCBalance createAssetBalance(Wallet w, int assetID, BigInteger totalRaw, BigInteger spendableRaw) {
    //Wallet.CoinSpark.AssetBalance assetBalance;
    CSAsset asset = w.CS.getAsset(assetID);

    String name = asset.getName();
    String nameShort = asset.getNameShort();

    if (name == null) {
        CoinSparkGenesis genesis = asset.getGenesis();
        if (genesis != null) {
            name = "Asset from " + genesis.getDomainName();
            nameShort = name;
        } else {
            // No genesis block found yet
            name = "Other Asset";
            nameShort = "Other Asset";
        }
    }

    String assetRef = CSMiscUtils.getHumanReadableAssetRef(asset);
    if (assetRef == null) {
        assetRef = "Awaiting new asset confirmation...";
    }

    JSONRPCBalance ab = new JSONRPCBalance();
    ab.setAsset_ref(assetRef);

    // Compute total balance
    Double balanceQty = CSMiscUtils.getDisplayUnitsForRawUnits(asset, totalRaw).doubleValue();
    String balanceDisplay = CSMiscUtils.getFormattedDisplayStringForRawUnits(asset, totalRaw);
    JSONRPCBalanceAmount balanceAmount = new JSONRPCBalanceAmount(totalRaw.longValue(), balanceQty,
            balanceDisplay);

    if (spendableRaw != null) {
        Double spendableQty = CSMiscUtils.getDisplayUnitsForRawUnits(asset, spendableRaw).doubleValue();
        String spendableDisplay = CSMiscUtils.getFormattedDisplayStringForRawUnits(asset, spendableRaw);
        JSONRPCBalanceAmount spendableAmount = new JSONRPCBalanceAmount(spendableRaw.longValue(), spendableQty,
                spendableDisplay);

        ab.setTotal(balanceAmount);
        ab.setSpendable(spendableAmount);
    } else {
        ab.setAmount(balanceAmount);
    }

    ab.setName(name);
    ab.setName_short(nameShort);
    String domain = CSMiscUtils.getDomainHost(asset.getDomainURL());
    ab.setDomain(domain);
    ab.setUrl(asset.getAssetWebPageURL());
    ab.setIssuer(asset.getIssuer());
    ab.setDescription(asset.getDescription());
    ab.setUnits(asset.getUnits());
    ab.setMultiple(asset.getMultiple());
    boolean isValid = (asset.getAssetState() == CSAsset.CSAssetState.VALID);

    if (asset.getAssetState() == CSAsset.CSAssetState.REFRESH) {
        ab.setRefreshing(true);
        ab.setStatus(CSMiscUtils.getHumanReadableAssetState(asset.getAssetStateBeforeRefresh()));
    } else {
        ab.setRefreshing(false);
        ab.setStatus(CSMiscUtils.getHumanReadableAssetState(asset.getAssetState()));
    }

    ab.setValid(isValid);
    Date validCheckedDate = asset.getValidChecked();
    if (validCheckedDate != null) {
        ab.setChecked_unixtime(validCheckedDate.getTime() / 1000L);
    }
    ab.setContract_url(asset.getContractUrl());

    String contractPath = asset.getContractPath();
    if (contractPath != null) {
        String appDirPath = controller.getApplicationDataDirectoryLocator().getApplicationDataDirectory();
        File file = new File(contractPath);
        File dir = new File(appDirPath);
        try {
            URI absolute = file.toURI();
            URI base = dir.toURI();
            URI relative = base.relativize(absolute);
            contractPath = relative.getPath();
        } catch (Exception e) {
            // do nothing, if error, just use full contractPath
        }
    }
    ab.setContract_file(contractPath);
    ab.setGenesis_txid(asset.getGenTxID());
    Date creationDate = asset.getDateCreation();
    if (creationDate != null) {
        ab.setAdded_unixtime(creationDate.getTime() / 1000L);
    }
    // 3 October 2014, 1:47 am
    SimpleDateFormat sdf = new SimpleDateFormat("d MMMM yyyy, h:mm");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // CoinSpark asset web page shows GMT/UTC.
    SimpleDateFormat ampmdf = new SimpleDateFormat(" a"); // by default is uppercase and we need lower to match website
    Date issueDate = asset.getIssueDate();
    if (issueDate != null) {
        ab.setIssue_date(sdf.format(issueDate) + ampmdf.format(issueDate).toLowerCase());
        ab.setIssue_unixtime(issueDate.getTime() / 1000L);
    }

    // Never expires
    Date expiryDate = asset.getExpiryDate();
    if (expiryDate != null) {
        ab.setExpiry_date(sdf.format(expiryDate) + ampmdf.format(expiryDate).toLowerCase());
        ab.setExpiry_unixtime(expiryDate.getTime() / 1000L);
    }
    ab.setTracker_urls(asset.getCoinsparkTrackerUrls());
    ab.setIcon_url(asset.getIconUrl());
    ab.setImage_url(asset.getImageUrl());
    ab.setFeed_url(asset.getFeedUrl());
    ab.setRedemption_url(asset.getRedemptionUrl());
    ab.setVisible(asset.isVisible());
    return ab;
}

From source file:tufts.vue.URLResource.java

private URI findRelativeURI(URI root) {
    final URI absURI = toAbsoluteURI();

    if (root.getScheme() == null || !root.getScheme().equals(absURI.getScheme())) {
        //if (DEBUG.Enabled) out("differing schemes: " + root + " - " + absURI + "; can't be relative");
        if (DEBUG.RESOURCE)
            Log.info(this + "; scheme=" + absURI.getScheme() + "; different scheme: " + root
                    + "; can't be relative");
        return null;
    }/*from  w ww .  jav  a 2s .  c om*/

    //         if (DEBUG.Enabled) {
    //             //System.out.println("\n=======================================================");
    //             Log.debug("attempting to relativize [" + this + "] against: " + root);
    //         }

    if (!absURI.isAbsolute())
        Log.warn("findRelativeURI: non-absolute URI: " + absURI);
    //Log.warn("Non absolute URI: " + absURI + "; from URL " + url);

    //         if (absURI == null) {
    //             System.out.println("URL INVALID FOR URI: " + url + "; in " + this);
    //             return null;
    //         }

    if (DEBUG.RESOURCE)
        Resource.dumpURI(absURI, "CURRENT ABSOLUTE:");
    final URI relativeURI = root.relativize(absURI);

    if (relativeURI == absURI) {
        // oldRoot was unable to relativize absURI -- this resource
        // was not relative to it's map in it's previous incarnation.
        return null;
    }

    if (relativeURI != absURI) {
        if (DEBUG.RESOURCE)
            Resource.dumpURI(relativeURI, "RELATIVE FOUND:");
    }

    if (DEBUG.Enabled) {
        out(TERM_GREEN + "FOUND RELATIVE: " + relativeURI + TERM_CLEAR);
    } else {
        Log.info("found relative to " + root + ": " + relativeURI.getPath());
    }

    return relativeURI;

}

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 w w .j  av  a2  s  .c om*/

    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;
}