3853N/A/*
3853N/A * CDDL HEADER START
3853N/A *
3853N/A * The contents of this file are subject to the terms of the
3853N/A * Common Development and Distribution License, Version 1.0 only
3853N/A * (the "License"). You may not use this file except in compliance
3853N/A * with the License.
3853N/A *
3853N/A * You can obtain a copy of the license at
3853N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
3853N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
3853N/A * See the License for the specific language governing permissions
3853N/A * and limitations under the License.
3853N/A *
3853N/A * When distributing Covered Code, include this CDDL HEADER in each
3853N/A * file and include the License file at
3853N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
3853N/A * add the following below this CDDL HEADER, with the fields enclosed
3853N/A * by brackets "[]" replaced with your own identifying information:
3853N/A * Portions Copyright [yyyy] [name of copyright owner]
3853N/A *
3853N/A * CDDL HEADER END
3853N/A *
3853N/A *
3853N/A * Copyright 2008 Sun Microsystems, Inc.
6176N/A * Portions copyright 2013 ForgeRock, AS.
3853N/A */
3853N/A
3853N/Apackage org.opends.guitools.controlpanel.datamodel;
3853N/A
3853N/Aimport static org.opends.messages.AdminToolMessages.*;
3853N/A
3853N/Aimport java.util.ArrayList;
3853N/Aimport java.util.Comparator;
3853N/Aimport java.util.Date;
3853N/Aimport java.util.HashSet;
3853N/Aimport java.util.Set;
3853N/Aimport java.util.TreeSet;
3853N/A
3853N/Aimport org.opends.guitools.controlpanel.util.Utilities;
3853N/Aimport org.opends.messages.Message;
3853N/A
3853N/A/**
3853N/A * The table model used to display all the base DNs.
3853N/A *
3853N/A */
3853N/Apublic class BaseDNTableModel extends SortableTableModel
3853N/Aimplements Comparator<BaseDNDescriptor>
3853N/A{
3853N/A private static final long serialVersionUID = -5650762484071136983L;
3853N/A private HashSet<BaseDNDescriptor> data = new HashSet<BaseDNDescriptor>();
3853N/A private ServerDescriptor.ServerStatus serverStatus;
3853N/A private boolean isAuthenticated;
3853N/A
3853N/A private ArrayList<String[]> dataArray =
3853N/A new ArrayList<String[]>();
3853N/A private String[] COLUMN_NAMES;
3853N/A private int sortColumn = 0;
3853N/A private boolean sortAscending = true;
3853N/A private boolean displayReplicationInformation;
3853N/A
3853N/A /**
3853N/A * Key value to identify the case of a value not available because the server
3853N/A * is down.
3853N/A */
3853N/A public static String NOT_AVAILABLE_SERVER_DOWN = "NOT_AVAILABLE_SERVER_DOWN";
3853N/A
3853N/A /**
3853N/A * Key value to identify the case of a value not available because
3853N/A * authentication is required.
3853N/A */
3853N/A public static String NOT_AVAILABLE_AUTHENTICATION_REQUIRED =
3853N/A "NOT_AVAILABLE_AUTHENTICATION_REQUIRED";
3853N/A
3853N/A /**
3853N/A * Key value to identify the case of a value not available.
3853N/A */
3853N/A public static String NOT_AVAILABLE = "NOT_AVAILABLE";
3853N/A
3853N/A /**
3853N/A * Constructor for this table model.
3853N/A * @param displayReplicationInformation whether to display replication.
3853N/A * monitoring information or not.
3853N/A */
3853N/A public BaseDNTableModel(boolean displayReplicationInformation)
3853N/A {
3853N/A this(displayReplicationInformation, true);
3853N/A }
3853N/A
3853N/A /**
3853N/A * Constructor for this table model.
3853N/A * @param displayReplicationInformation whether to display replication.
3853N/A * @param wrapHeader whether to wrap the headers or not.
3853N/A * monitoring information or not.
3853N/A */
3853N/A public BaseDNTableModel(boolean displayReplicationInformation,
3853N/A boolean wrapHeader)
3853N/A {
3853N/A this.displayReplicationInformation = displayReplicationInformation;
3853N/A if (wrapHeader)
3853N/A {
3853N/A COLUMN_NAMES = new String[] {
3853N/A getHeader(INFO_BASEDN_COLUMN.get()),
3853N/A getHeader(INFO_BACKENDID_COLUMN.get()),
3853N/A getHeader(INFO_NUMBER_ENTRIES_COLUMN.get()),
3853N/A getHeader(INFO_REPLICATED_COLUMN.get()),
3853N/A getHeader(INFO_MISSING_CHANGES_COLUMN.get()),
3853N/A getHeader(INFO_AGE_OF_OLDEST_MISSING_CHANGE_COLUMN.get())
3853N/A };
3853N/A }
3853N/A else
3853N/A {
3853N/A COLUMN_NAMES = new String[] {
3853N/A INFO_BASEDN_COLUMN.get().toString(),
3853N/A INFO_BACKENDID_COLUMN.get().toString(),
3853N/A INFO_NUMBER_ENTRIES_COLUMN.get().toString(),
3853N/A INFO_REPLICATED_COLUMN.get().toString(),
3853N/A INFO_MISSING_CHANGES_COLUMN.get().toString(),
3853N/A INFO_AGE_OF_OLDEST_MISSING_CHANGE_COLUMN.get().toString()
3853N/A };
3853N/A }
3853N/A }
3853N/A
3853N/A /**
3853N/A * Sets the data for this table model.
3853N/A * @param newData the data for this table model.
3853N/A * @param status the server status.
3853N/A * @param isAuthenticated whether the user provided authentication or not.
3853N/A */
3853N/A public void setData(Set<BaseDNDescriptor> newData,
3853N/A ServerDescriptor.ServerStatus status, boolean isAuthenticated)
3853N/A {
3853N/A if (!newData.equals(data) || (serverStatus != status) ||
3853N/A (this.isAuthenticated != isAuthenticated))
3853N/A {
3853N/A serverStatus = status;
3853N/A this.isAuthenticated = isAuthenticated;
3853N/A data.clear();
3853N/A data.addAll(newData);
3853N/A updateDataArray();
3853N/A fireTableDataChanged();
3853N/A }
3853N/A }
3853N/A
3853N/A /**
3853N/A * Updates the table model contents and sorts its contents depending on the
3853N/A * sort options set by the user.
3853N/A */
3853N/A public void forceResort()
3853N/A {
3853N/A updateDataArray();
3853N/A fireTableDataChanged();
3853N/A }
3853N/A
3853N/A /**
3853N/A * Comparable implementation.
3853N/A * @param desc1 the first replica descriptor to compare.
3853N/A * @param desc2 the second replica descriptor to compare.
3853N/A * @return 1 if according to the sorting options set by the user the first
3853N/A * base DN descriptor must be put before the second descriptor, 0 if they
3853N/A * are equivalent in terms of sorting and -1 if the second descriptor must
3853N/A * be put before the first descriptor.
3853N/A */
3853N/A public int compare(BaseDNDescriptor desc1, BaseDNDescriptor desc2)
3853N/A {
3853N/A int result = 0;
3853N/A if (sortColumn == 0)
3853N/A {
3853N/A result = compareDns(desc1, desc2);
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareBackendIDs(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareEntries(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareRepl(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareMissingChanges(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareAgeOfOldestMissingChange(desc1, desc2);
3853N/A }
3853N/A }
3853N/A
3853N/A if (sortColumn == 1)
3853N/A {
3853N/A result = compareBackendIDs(desc1, desc2);
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareDns(desc1, desc2);
3853N/A
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareEntries(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareRepl(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareMissingChanges(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareAgeOfOldestMissingChange(desc1, desc2);
3853N/A }
3853N/A }
3853N/A else if (sortColumn == 2)
3853N/A {
3853N/A result = compareEntries(desc1, desc2);
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareBackendIDs(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareDns(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareRepl(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareMissingChanges(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareAgeOfOldestMissingChange(desc1, desc2);
3853N/A }
3853N/A }
3853N/A else if (sortColumn == 3)
3853N/A {
3853N/A result = compareRepl(desc1, desc2);
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareBackendIDs(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareDns(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareEntries(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareMissingChanges(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareAgeOfOldestMissingChange(desc1, desc2);
3853N/A }
3853N/A }
3853N/A else if (sortColumn == 4)
3853N/A {
3853N/A result = compareMissingChanges(desc1, desc2);
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareBackendIDs(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareDns(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareEntries(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareRepl(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareAgeOfOldestMissingChange(desc1, desc2);
3853N/A }
3853N/A }
3853N/A else if (sortColumn == 5)
3853N/A {
3853N/A result = compareAgeOfOldestMissingChange(desc1, desc2);
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareBackendIDs(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareDns(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareEntries(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareRepl(desc1, desc2);
3853N/A }
3853N/A
3853N/A if (result == 0)
3853N/A {
3853N/A result = compareMissingChanges(desc1, desc2);
3853N/A }
3853N/A }
3853N/A
3853N/A if (!sortAscending)
3853N/A {
3853N/A result = -result;
3853N/A }
3853N/A
3853N/A return result;
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public int getColumnCount()
3853N/A {
3853N/A return displayReplicationInformation ? 6 : 4;
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public int getRowCount()
3853N/A {
3853N/A return dataArray.size();
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public Object getValueAt(int row, int col)
3853N/A {
3853N/A return dataArray.get(row)[col];
3853N/A }
3853N/A
3853N/A /**
3853N/A * Updates the array data. This includes resorting it.
3853N/A */
3853N/A private void updateDataArray()
3853N/A {
3853N/A TreeSet<BaseDNDescriptor> sortedSet = new TreeSet<BaseDNDescriptor>(this);
3853N/A sortedSet.addAll(data);
3853N/A dataArray.clear();
3853N/A for (BaseDNDescriptor desc : sortedSet)
3853N/A {
3853N/A String[] s = new String[6];
3853N/A
3853N/A s[0] = Utilities.unescapeUtf8(desc.getDn().toString());
3853N/A
3853N/A s[1] = desc.getBackend().getBackendID();
3853N/A
3853N/A s[2] = getValueForEntries(desc);
3853N/A
3853N/A s[3] = getStringForReplState(desc);
3853N/A
3853N/A s[4] = getValueForMissingChanges(desc);
3853N/A
3853N/A s[5] = getValueForOldestMissingChange(desc);
3853N/A
3853N/A dataArray.add(s);
3853N/A }
3853N/A }
3853N/A
3853N/A /**
3853N/A * {@inheritDoc}
3853N/A */
3853N/A public String getColumnName(int col) {
3853N/A return COLUMN_NAMES[col];
3853N/A }
3853N/A
3853N/A /**
3853N/A * Returns whether the sort is ascending or descending.
3853N/A * @return <CODE>true</CODE> if the sort is ascending and <CODE>false</CODE>
3853N/A * otherwise.
3853N/A */
3853N/A public boolean isSortAscending()
3853N/A {
3853N/A return sortAscending;
3853N/A }
3853N/A
3853N/A /**
3853N/A * Sets whether to sort ascending of descending.
3853N/A * @param sortAscending whether to sort ascending or descending.
3853N/A */
3853N/A public void setSortAscending(boolean sortAscending)
3853N/A {
3853N/A this.sortAscending = sortAscending;
3853N/A }
3853N/A
3853N/A /**
3853N/A * Returns the column index used to sort.
3853N/A * @return the column index used to sort.
3853N/A */
3853N/A public int getSortColumn()
3853N/A {
3853N/A return sortColumn;
3853N/A }
3853N/A
3853N/A /**
3853N/A * Sets the column index used to sort.
3853N/A * @param sortColumn column index used to sort..
3853N/A */
3853N/A public void setSortColumn(int sortColumn)
3853N/A {
3853N/A this.sortColumn = sortColumn;
3853N/A }
3853N/A
3853N/A /*
3853N/A * Several comparison methods to be able to sort the table model.
3853N/A */
3853N/A private int compareBackendIDs(BaseDNDescriptor desc1, BaseDNDescriptor desc2)
3853N/A {
3853N/A return desc1.getBackend().getBackendID().compareTo(
3853N/A desc2.getBackend().getBackendID());
3853N/A }
3853N/A
3853N/A private int compareEntries(BaseDNDescriptor desc1, BaseDNDescriptor desc2)
3853N/A {
3853N/A int n1 = desc1.getEntries();
3853N/A int n2 = desc2.getEntries();
3853N/A return compareIntegers(n1, n2);
3853N/A }
3853N/A
3853N/A private int compareIntegers(int n1, int n2)
3853N/A {
3853N/A if (n1 == n2)
3853N/A {
3853N/A return 0;
3853N/A }
3853N/A if (n1 > n2)
3853N/A {
3853N/A return 1;
3853N/A }
3853N/A return -1;
3853N/A }
3853N/A
3853N/A private int compareLongs(long n1, long n2)
3853N/A {
3853N/A if (n1 == n2)
3853N/A {
3853N/A return 0;
3853N/A }
3853N/A if (n1 > n2)
3853N/A {
3853N/A return 1;
3853N/A }
3853N/A return -1;
3853N/A }
3853N/A
3853N/A private int compareDns(BaseDNDescriptor desc1, BaseDNDescriptor desc2)
3853N/A {
3853N/A return Utilities.unescapeUtf8(desc1.getDn().toString()).compareTo(
3853N/A Utilities.unescapeUtf8(desc2.getDn().toString()));
3853N/A }
3853N/A
3853N/A private int compareRepl(BaseDNDescriptor desc1, BaseDNDescriptor desc2)
3853N/A {
3853N/A return (String.valueOf(desc1.getType()).compareTo(
3853N/A String.valueOf(desc2.getType())));
3853N/A }
3853N/A
3853N/A private int compareMissingChanges(BaseDNDescriptor desc1,
3853N/A BaseDNDescriptor desc2)
3853N/A {
3853N/A return compareIntegers(desc1.getMissingChanges(),
3853N/A desc2.getMissingChanges());
3853N/A }
3853N/A
3853N/A private int compareAgeOfOldestMissingChange(BaseDNDescriptor desc1,
3853N/A BaseDNDescriptor desc2)
3853N/A {
3853N/A return compareLongs(desc1.getAgeOfOldestMissingChange(),
3853N/A desc2.getAgeOfOldestMissingChange());
3853N/A }
3853N/A
3853N/A /**
3853N/A * Returns the Object describing the number of entries of a given Base DN.
3853N/A * The Object will be an Integer.
3853N/A * @param rep the Base DN object to handle.
3853N/A * @return the Object describing the number of entries of a given Base DN.
3853N/A */
3853N/A private String getValueForEntries(BaseDNDescriptor rep)
3853N/A {
3853N/A String returnValue;
3853N/A if (serverStatus != ServerDescriptor.ServerStatus.STARTED)
3853N/A {
3853N/A returnValue = NOT_AVAILABLE_SERVER_DOWN;
3853N/A }
3853N/A else if (!isAuthenticated)
3853N/A {
3853N/A returnValue = NOT_AVAILABLE_AUTHENTICATION_REQUIRED;
3853N/A }
3853N/A else
3853N/A {
3853N/A if (rep.getEntries() < 0)
3853N/A {
3853N/A returnValue = NOT_AVAILABLE;
3853N/A }
3853N/A else
3853N/A {
3853N/A returnValue = String.valueOf(rep.getEntries());
3853N/A }
3853N/A }
3853N/A return returnValue;
3853N/A }
3853N/A
3853N/A /**
3853N/A * Returns the Object describing the number of missing changes of a given Base
3853N/A * DN. The Object will be a String unless the base DN is
3853N/A * replicated and we could not find a valid value (in this case we return
3853N/A * an Integer with the invalid value).
3853N/A * @param rep the Base DN object to handle.
3853N/A * @return the Object describing the number of missing changes of
3853N/A * a given Base DN.
3853N/A */
3853N/A private String getValueForMissingChanges(BaseDNDescriptor rep)
3853N/A {
3853N/A String returnValue;
3853N/A if (rep.getType() == BaseDNDescriptor.Type.REPLICATED)
3853N/A {
3853N/A if (serverStatus != ServerDescriptor.ServerStatus.STARTED)
3853N/A {
3853N/A returnValue = NOT_AVAILABLE_SERVER_DOWN;
3853N/A }
3853N/A else if (!isAuthenticated)
3853N/A {
3853N/A returnValue = NOT_AVAILABLE_AUTHENTICATION_REQUIRED;
3853N/A }
3853N/A else
3853N/A {
3853N/A if (rep.getMissingChanges() < 0)
3853N/A {
3853N/A returnValue = NOT_AVAILABLE;
3853N/A }
3853N/A else
3853N/A {
3853N/A returnValue = String.valueOf(rep.getMissingChanges());
3853N/A }
3853N/A }
3853N/A }
3853N/A else
3853N/A {
3853N/A returnValue = INFO_NOT_APPLICABLE_LABEL.get().toString();
3853N/A }
3853N/A return returnValue;
3853N/A }
3853N/A
3853N/A /**
3853N/A * Returns the Object describing the age of oldest missing change of
3853N/A * a given Base DN. The Object will be a String unless the base DN is
3853N/A * replicated and we could not find a valid value (in this case we return
3853N/A * an Integer with the invalid value).
3853N/A * @param rep the Base DN object to handle.
3853N/A * @return the Object describing the age of oldest missing change of
3853N/A * a given Base DN.
3853N/A */
3853N/A private String getValueForOldestMissingChange(BaseDNDescriptor rep)
3853N/A {
3853N/A String returnValue;
3853N/A if (rep.getType() == BaseDNDescriptor.Type.REPLICATED)
3853N/A {
3853N/A if (serverStatus != ServerDescriptor.ServerStatus.STARTED)
3853N/A {
3853N/A returnValue = NOT_AVAILABLE_SERVER_DOWN;
3853N/A }
3853N/A else if (!isAuthenticated)
3853N/A {
3853N/A returnValue = NOT_AVAILABLE_AUTHENTICATION_REQUIRED;
3853N/A }
3853N/A else
3853N/A {
3853N/A long age = rep.getAgeOfOldestMissingChange();
3853N/A if (age > 0)
3853N/A {
3853N/A Date date = new Date(age);
3853N/A returnValue = date.toString();
3853N/A }
3853N/A else
3853N/A {
3853N/A // Not available
3853N/A returnValue = NOT_AVAILABLE;
3853N/A }
3853N/A }
3853N/A }
3853N/A else
3853N/A {
3853N/A returnValue = INFO_NOT_APPLICABLE_LABEL.get().toString();
3853N/A }
3853N/A return returnValue;
3853N/A }
3853N/A
3853N/A /**
3853N/A * Returns the localized String describing the replication state of
3853N/A * a given Base DN.
3853N/A * @param rep the Base DN object to handle.
3853N/A * @return the localized String describing the replication state of
3853N/A * a given Base DN.
3853N/A */
3853N/A private String getStringForReplState(BaseDNDescriptor rep)
3853N/A {
3853N/A Message s;
3853N/A if (rep.getType() == BaseDNDescriptor.Type.REPLICATED)
3853N/A {
3853N/A s = INFO_BASEDN_REPLICATED_LABEL.get();
3853N/A }
6176N/A else if (rep.getType() == BaseDNDescriptor.Type.NOT_REPLICATED)
6176N/A {
6176N/A s = INFO_BASEDN_NOT_REPLICATED_LABEL.get();
6176N/A }
3853N/A else
3853N/A {
6176N/A s = INFO_BASEDN_DISABLED_LABEL.get();
3853N/A }
3853N/A return s.toString();
3853N/A }
3853N/A}