Repository: syncope Updated Branches: refs/heads/master 73f73f206 -> 7a11bf3b9
http://git-wip-us.apache.org/repos/asf/syncope/blob/7a11bf3b/client/cli/src/main/java/org/apache/syncope/client/cli/view/Messages.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/view/Messages.java b/client/cli/src/main/java/org/apache/syncope/client/cli/view/Messages.java new file mode 100644 index 0000000..4489b9b --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/view/Messages.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.client.cli.view; + +public final class Messages { + + private static final String OPTION_COMMAND_MESSAGE_TEMPLATE = "\n - Usage: %s\n"; + + private static final String CREATED_MESSAGE_TEMPLATE = "%s %s successfully created"; + + private static final String UPDATED_MESSAGE_TEMPLATE = "%s %s successfully updated"; + + private static final String DELETED_MESSAGE_TEMPLATE = "%s %s successfully deleted"; + + private static final String DOESNT_EXIST_MESSAGE_TEMPLATE = "%s %s doesn't exist"; + + private static final String TYPE_NOT_VALID_MESSAGE_TEMPLATE = "%s isn't a valid %s type, try with: %s"; + + private static final String ID_NOT_NUMBER_MESSAGE_TEMPLATE = "Error reading %s. It isn't a valid %s " + + "id because it isn't a long value"; + + private static final String NOT_BOOLEAN_MESSAGE_TEMPLATE = "Error reading %s. It isn't a valid %s " + + "value because it isn't a boolean value"; + + private static final String DEFAULT_MESSAGE_TEMPLATE = "%s is not a valid option. \n\b %s"; + + public static void printCommandOptionMessage(final String message) { + System.out.println(String.format(OPTION_COMMAND_MESSAGE_TEMPLATE, message)); + } + + public static void printMessage(final String... messages) { + final StringBuilder messageBuilder = new StringBuilder("\n"); + for (final String message : messages) { + messageBuilder.append(" - ").append(message).append("\n"); + } + System.out.println(messageBuilder.toString()); + } + + public static void printNofFoundMessage(final String what, final String key) { + printMessage(String.format(DOESNT_EXIST_MESSAGE_TEMPLATE, what, key)); + } + + public static void printCreatedMessage(final String what, final String key) { + printMessage(String.format(CREATED_MESSAGE_TEMPLATE, what, key)); + } + + public static void printUpdatedMessage(final String what, final String key) { + printMessage(String.format(UPDATED_MESSAGE_TEMPLATE, what, key)); + } + + public static void printDeletedMessage(final String what, final String key) { + printMessage(String.format(DELETED_MESSAGE_TEMPLATE, what, key)); + } + + public static void printIdNotNumberDeletedMessage(final String what, final String key) { + printMessage(String.format(ID_NOT_NUMBER_MESSAGE_TEMPLATE, key, what)); + } + + public static void printNotBooleanDeletedMessage(final String what, final String key) { + printMessage(String.format(NOT_BOOLEAN_MESSAGE_TEMPLATE, key, what)); + } + + public static void printTypeNotValidMessage(final String what, final String key, final String[] types) { + final StringBuilder typesBuilder = new StringBuilder(); + for (final String type : types) { + typesBuilder.append("\n *** ").append(type); + } + printMessage(String.format(TYPE_NOT_VALID_MESSAGE_TEMPLATE, key, what, typesBuilder.toString())); + } + + public static void printDefaultMessage(final String option, final String helpMessage) { + printMessage(String.format(DEFAULT_MESSAGE_TEMPLATE, option, helpMessage)); + } + + private Messages() { + + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/7a11bf3b/client/cli/src/main/java/org/apache/syncope/client/cli/view/Table.java ---------------------------------------------------------------------- diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/view/Table.java b/client/cli/src/main/java/org/apache/syncope/client/cli/view/Table.java new file mode 100644 index 0000000..5230f42 --- /dev/null +++ b/client/cli/src/main/java/org/apache/syncope/client/cli/view/Table.java @@ -0,0 +1,194 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.client.cli.view; + +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.Set; +import org.apache.commons.lang3.StringUtils; + +public final class Table { + + private static final String TABLE_TITLE_FORMAT = "# %s #\n"; + + private final String title; + + private final LinkedList<String> headers; + + private final Set<LinkedList<String>> values; + + private int columnsNumber; + + private String[] tmpValuesArray; + + private String tableContentFormat; + + private int[] columnsSize; + + private int tableWidth; + + private String border = ""; + + private Table( + final String title, + final LinkedList<String> headers, + final Set<LinkedList<String>> values) { + this.title = title; + this.headers = headers; + this.values = values; + } + + public void print() { + columnsNumber = headers.size(); + tmpValuesArray = new String[columnsNumber]; + + buildTableContentFormat(); + initializeColumnSize(); + countTableWidth(); + + printBorder(); + printTitle(); + printBorder(); + printHeaders(); + printBorder(); + printeContent(); + printBorder(); + } + + private void buildTableContentFormat() { + final StringBuilder tableContentFormatBuilder = new StringBuilder("#"); + for (int s = 0; s < columnsNumber; s++) { + tableContentFormatBuilder.append(" %s #"); + } + tableContentFormatBuilder.append("\n"); + tableContentFormat = tableContentFormatBuilder.toString(); + } + + private void initializeColumnSize() { + columnsSize = new int[columnsNumber]; + for (int j = 0; j < columnsSize.length; j++) { + columnsSize[j] = 0; + } + + for (int i = 0; i < columnsSize.length; i++) { + if (headers.get(i).length() > columnsSize[i]) { + columnsSize[i] = headers.get(i).length(); + } + } + + for (final LinkedList<String> value : values) { + for (int j = 0; j < columnsSize.length; j++) { + if (value.get(j) != null && value.get(j).length() > columnsSize[j]) { + columnsSize[j] = value.get(j).length(); + } + } + } + } + + private void countTableWidth() { + int maxColumnValueSum = 0; + for (int j = 0; j < columnsSize.length; j++) { + maxColumnValueSum = maxColumnValueSum + columnsSize[j]; + } + + tableWidth = maxColumnValueSum + (columnsNumber * (2 + 2)) + columnsNumber + 1; + } + + private void printBorder() { + if (border.isEmpty()) { + final StringBuilder borderBuilder = new StringBuilder(); + for (int j = 0; j < tableWidth; j++) { + borderBuilder.append("#"); + } + border = borderBuilder.toString(); + } + + System.out.println(border); + } + + private void printTitle() { + System.out.format(TABLE_TITLE_FORMAT, StringUtils.center(" ", tableWidth - 6)); + System.out.format(TABLE_TITLE_FORMAT, StringUtils.center(title.toUpperCase(), tableWidth - 6)); + System.out.format(TABLE_TITLE_FORMAT, StringUtils.center(" ", tableWidth - 6)); + } + + private void printHeaders() { + printColumnSpace(); + + for (int h = 0; h < columnsNumber; h++) { + tmpValuesArray[h] = StringUtils.center(headers.get(h).toUpperCase(), columnsSize[h]); + } + + System.out.format(tableContentFormat, tmpValuesArray); + + printColumnSpace(); + } + + private void printeContent() { + printColumnSpace(); + + for (final LinkedList<String> value : values) { + for (int j = 0; j < columnsNumber; j++) { + if (value.get(j) == null) { + tmpValuesArray[j] = StringUtils.center("null", columnsSize[j]); + } else { + tmpValuesArray[j] = StringUtils.center(value.get(j), columnsSize[j]); + } + } + System.out.format(tableContentFormat, tmpValuesArray); + } + + printColumnSpace(); + } + + private void printColumnSpace() { + for (int h = 0; h < columnsNumber; h++) { + tmpValuesArray[h] = StringUtils.center(" ", columnsSize[h]); + } + + System.out.format(tableContentFormat, tmpValuesArray); + } + + public static class TableBuilder { + + private final LinkedList<String> headers = new LinkedList<>(); + + private final Set<LinkedList<String>> values = new LinkedHashSet<>(); + + private final String title; + + public TableBuilder(final String title) { + this.title = title; + } + + public TableBuilder header(final String header) { + headers.add(header); + return this; + } + + public TableBuilder rowValues(final LinkedList<String> row) { + values.add(row); + return this; + } + + public Table build() { + return new Table(title, headers, values); + } + } +}
