private void displayAndUpdate()

in source/com.microsoft.tfs.client.clc/src/com/microsoft/tfs/client/clc/vc/commands/CommandWorkspaces.java [198:434]


    private void displayAndUpdate() throws ArgumentException, MalformedURLException, CLCException, LicenseException {
        if (getFreeArguments().length > 1) {
            final String messageFormat =
                Messages.getString("CommandWorkspaces.CommandRequiresAtMostOneWorkspaceFormat"); //$NON-NLS-1$
            final String message = MessageFormat.format(messageFormat, getCanonicalName());

            throw new InvalidFreeArgumentException(message);
        }

        String computer = null;
        OptionFormat formatOption = null;
        String workspaceName = null;
        String workspaceOwner = null;
        URI serverURI = null;
        String updateUserName = null;
        String updateComputerName = null;

        Option o = null;

        /*
         * Parse these up front.
         */

        if ((o = findOptionType(OptionComputer.class)) != null) {
            computer = ((OptionComputer) o).getValue();
        }

        if ((o = findOptionType(OptionFormat.class)) != null) {
            formatOption = (OptionFormat) o;
        }

        if ((o = findOptionType(OptionUpdateUserName.class)) != null) {
            updateUserName = ((OptionUpdateUserName) o).getValue();
        }

        if ((o = findOptionType(OptionUpdateComputerName.class)) != null) {
            updateComputerName = ((OptionUpdateComputerName) o).getValue();
        }

        final OptionCollection collectionOption = getCollectionOption();
        if (collectionOption != null) {
            serverURI = collectionOption.getURI();
        }

        final String format = (formatOption == null) ? OptionFormat.BRIEF : formatOption.getValue();

        if (getFreeArguments().length != 0) {
            final WorkspaceSpec spec = WorkspaceSpec.parse(getFreeArguments()[0], null);
            workspaceName = spec.getName();
            workspaceOwner = spec.getOwner();
        }

        /*
         * If they provided an owner option, let it override the one parsed from
         * the workspace spec.
         */
        if ((o = findOptionType(OptionOwner.class)) != null) {
            workspaceOwner = ((OptionOwner) o).getValue();
        }

        /*
         * Only set the exit code if the user was searching for something.
         */
        final boolean partialSuccessOnFailedQuery =
            (computer != null || workspaceOwner != null || getFreeArguments().length != 0);

        /*
         * Smash wildcards down to null values for easier testing.
         */
        if (workspaceName == null || workspaceName.equals("*")) //$NON-NLS-1$
        {
            workspaceName = null;
        }

        /*
         * Since we support "*" to mean "all" for both computer and owner, we
         * don't allow wildcards inside the strings.
         */
        if (computer != null && computer.equalsIgnoreCase("*") == false && Wildcard.isWildcard(computer)) //$NON-NLS-1$
        {
            throw new InvalidOptionException(Messages.getString("CommandWorkspaces.WildcardsNotAllowedInComputer")); //$NON-NLS-1$
        }

        if (workspaceOwner != null
            && workspaceOwner.equalsIgnoreCase("*") == false //$NON-NLS-1$
            && Wildcard.isWildcard(workspaceOwner)) {
            throw new InvalidOptionException(Messages.getString("CommandWorkspaces.WildcardsNotAllowedInOwner")); //$NON-NLS-1$
        }

        TFSTeamProjectCollection connection = null;
        VersionControlClient client = null;

        if (serverURI != null) {
            /*
             * We need a connection.
             */
            connection = createConnection(true, true);
            client = connection.getVersionControlClient();
            initializeClient(client);

            /*
             * Update the data on the server (and the local caches).
             */
            updateWorkspaces(connection, client, serverURI, workspaceOwner, updateUserName, updateComputerName);
        } else if (updateUserName != null || updateComputerName != null) {
            throw new InvalidOptionException(
                Messages.getString("CommandWorkspaces.ServerRequiredWithUpdatecomputernameUpdateusername")); //$NON-NLS-1$
        }

        Workspace[] workspaces = null;
        WorkspaceInfo[] workspaceInfos = null;

        /*
         * No filters, so add all workspaces to the list.
         */
        if (serverURI == null && computer == null && workspaceName == null && workspaceOwner == null) {
            if (OptionFormat.DETAILED.equalsIgnoreCase(format) || OptionFormat.XML.equalsIgnoreCase(format)) {
                throw new InvalidOptionValueException(
                    Messages.getString("CommandWorkspaces.DetailedAndXMLFormatsRequireCollectionOption")); //$NON-NLS-1$
            }

            workspaceInfos = Workstation.getCurrent(CLC_PERSISTENCE_PROVIDER).getAllLocalWorkspaceInfo();

            if (workspaceInfos.length == 0) {
                getDisplay().printLine(Messages.getString("CommandWorkspaces.NoLocalWorkspacesFound")); //$NON-NLS-1$
                setExitCode(ExitCode.PARTIAL_SUCCESS);
                return;
            }
        } else {
            /*
             * Filter based on the criteria the user supplied.
             */

            if (computer == null && workspaceOwner == null) {
                computer = LocalHost.getShortName();
            } else if (computer != null && computer.equalsIgnoreCase("*")) //$NON-NLS-1$
            {
                computer = null;
            }

            if (workspaceOwner == null) {
                workspaceOwner = VersionControlConstants.AUTHENTICATED_USER;
            } else if (workspaceOwner != null && workspaceOwner.equalsIgnoreCase("*")) //$NON-NLS-1$
            {
                workspaceOwner = null;
            }

            if (connection == null || client == null) {
                /*
                 * No connection as of yet. We pretty much require the server
                 * option at this point, because we need to connect to some
                 * server for the query.
                 *
                 * The default "cannot find workspace" exception doesn't make a
                 * lot of sense here, so we rethrow.
                 */
                try {
                    connection = createConnection();
                    client = connection.getVersionControlClient();
                    initializeClient(client);
                } catch (final CannotFindWorkspaceException e) {
                    throw new CannotFindWorkspaceException(
                        Messages.getString("CommandWorkspaces.CouldNotDetermineTheServerToQuery")); //$NON-NLS-1$
                }
            }

            if (workspaceOwner != null && workspaceOwner.equalsIgnoreCase(VersionControlConstants.AUTHENTICATED_USER)) {
                final WorkspacePermissions publicLimitedPermissions =
                    WorkspacePermissions.USE.combine(WorkspacePermissions.READ);
                workspaces = client.queryWorkspaces(workspaceName, workspaceOwner, computer, publicLimitedPermissions);
            } else {
                workspaces = client.queryWorkspaces(workspaceName, workspaceOwner, computer);
            }

            if (workspaces.length == 0) {
                /*
                 * Resolve our TFS names back into meaningful strings for the
                 * error.
                 */

                final String authenticatedUserName = connection.getAuthorizedIdentity().getDisplayName();
                if (workspaceOwner != null && workspaceOwner.equals(VersionControlConstants.AUTHENTICATED_USER)) {
                    workspaceOwner = authenticatedUserName;
                } else if (workspaceOwner == null) {
                    workspaceOwner = "*"; //$NON-NLS-1$
                }

                workspaceName =
                    new WorkspaceSpec((workspaceName == null) ? "*" : workspaceName, workspaceOwner).toString(); //$NON-NLS-1$

                final String messageFormat =
                    Messages.getString("CommandWorkspaces.NoWorkspaceMatchingNameOnComputerFormat"); //$NON-NLS-1$
                final String message = MessageFormat.format(messageFormat, workspaceName, (computer == null ? "*" //$NON-NLS-1$
                    : computer), (serverURI == null) ? "*" : URIUtils.decodeForDisplay(serverURI)); //$NON-NLS-1$

                getDisplay().printLine(message);

                if (partialSuccessOnFailedQuery) {
                    setExitCode(ExitCode.PARTIAL_SUCCESS);
                    return;
                }
            }
        }

        /*
         * Time to display what we found.
         */

        if (OptionFormat.BRIEF.equalsIgnoreCase(format)) {
            // If Workspaces were queried, convert to WorkspaceInfo
            if (workspaces != null) {
                workspaceInfos = new WorkspaceInfo[workspaces.length];

                for (int i = 0; i < workspaces.length; i++) {
                    workspaceInfos[i] = new WorkspaceInfo(
                        new InternalServerInfo(workspaces[i].getServerURI(), workspaces[i].getServerGUID()),
                        workspaces[i]);
                }
            }

            Arrays.sort(workspaceInfos);
            printBrief(workspaceInfos);
        } else if (OptionFormat.DETAILED.equalsIgnoreCase(format)) {
            Arrays.sort(workspaces);
            printDetailed(workspaces);
        } else if (OptionFormat.XML.equalsIgnoreCase(format)) {
            Arrays.sort(workspaces);

            try {
                printXML(workspaces);
            } catch (final TransformerConfigurationException e) {
                throw new CLCException(e);
            } catch (final SAXException e) {
                throw new CLCException(e);
            }
        }
    }