CertificatesView.java
/*
** Module : CertificateView.java
** Abstract : CertificateView represents a view to manage certificates.
**
** Copyright (c) 2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------Description----------------------
** 001 SBI 20170601 Created initial version.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.admin.client.application.home.accounts.certificates;
import java.util.Collection;
import java.util.Set;
import org.gwtbootstrap3.client.ui.Button;
import org.gwtbootstrap3.client.ui.CheckBox;
import org.gwtbootstrap3.client.ui.Heading;
import org.gwtbootstrap3.client.ui.gwt.DataGrid;
import com.goldencode.p2j.admin.TaggedName;
import com.goldencode.p2j.admin.client.application.home.BaseViewWithUiHandlers;
import com.goldencode.p2j.admin.client.application.home.accounts.CertificatesOptions;
import com.goldencode.p2j.admin.client.widget.EnumRadioGroup;
import com.goldencode.p2j.admin.client.widget.GridHandle;
import com.goldencode.p2j.admin.client.widget.GridHelper;
import com.goldencode.p2j.admin.client.widget.dialog.InputDialog;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* CertificateView represents a view to manage certificates.
*/
public class CertificatesView
extends BaseViewWithUiHandlers<CertificatesUIHandlers>
implements CertificatesPresenter.MyView
{
/**
* GWT UI creator
*/
interface Binder
extends UiBinder<Widget, CertificatesView>
{}
/** The certificate role selector widget */
@UiField
EnumRadioGroup<CertificatesOptions> certsSelector;
/** The certificates table view */
@UiField(provided = true)
DataGrid<TaggedName> certsGrid = GridHelper.<TaggedName>create();
/** The certificates grid handle */
private final GridHandle<TaggedName> certsGridHandle;
/** The certificates selection model */
private final MultiSelectionModel<TaggedName> certsSelectionModel;
/** The accounts table view */
@UiField(provided = true)
DataGrid<TaggedName> accountsGrid = GridHelper.<TaggedName>create();
/** The accounts grid handle */
private final GridHandle<TaggedName> accountsGridHandle;
/** The accounts header widget */
@UiField
Heading accountsHeader;
/** The refresh button */
@UiField
Button refreshCerts;
/** Add certificate button */
@UiField
Button addCert;
/** Delete certificate button*/
@UiField
Button deleteCerts;
/** Change certificate alias button */
@UiField
Button changeAlias;
/** View details certificate button */
@UiField
Button viewCert;
/** The unused certificate checkbox */
@UiField
CheckBox unassignedOnly;
/** modal container */
@UiField
HasWidgets modalFragment;
/** The certificate definition view */
private final CertificateDefinitionView certDefView;
/** The common input dialog provider */
private final Provider<InputDialog> inputDialogProvider;
/** The certificate details view */
private final CertificateDetailsView certDetailsView;
/** The flag indicating that the select action has been invoked */
private boolean requestToselect = false;
/**
* Constructs this view used by MPV gwtplatform of ArcBees Inc.
*
* @param binder
* GWT UI Binder
* @param inputDialogProvider
* The dialog provider
* @param certDefView
* The certificate definition view
* @param certDetailsView
* The certificate details view
*/
@Inject
public CertificatesView(Binder binder,
Provider<InputDialog> inputDialogProvider,
CertificateDefinitionView certDefView,
CertificateDetailsView certDetailsView)
{
initWidget(binder.createAndBindUi(this));
setupModalSlot(CertificatesPresenter.MODAL_CONTENT, modalFragment);
this.inputDialogProvider = inputDialogProvider;
this.certDefView = certDefView;
addDialog(certDefView);
this.certDetailsView = certDetailsView;
addDialog(certDetailsView);
this.certsGridHandle = GridHelper.<TaggedName>initListGrid(
certsGrid,
true,
new GridHelper.ColumnInfo<TaggedName>(
"Alias",
(tn) -> tn.getName(),
null),
new GridHelper.ColumnInfo<TaggedName>(
"Description",
(tn) -> tn.getTag(),
null));
this.certsSelectionModel =
(MultiSelectionModel<TaggedName>) this.certsGridHandle.getSelectionModel();
this.certsSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler()
{
@Override
public void onSelectionChange(SelectionChangeEvent event)
{
Set<TaggedName> selected = CertificatesView.this.certsSelectionModel.getSelectedSet();
if (!selected.isEmpty() && !requestToselect)
{
getUiHandlers().onCertsSelected(selected.toArray(new TaggedName[selected.size()]),
certsSelector.getValue(), unassignedOnly.getValue());
}
}
});
this.certsSelector.addValueChangeHandler(new ValueChangeHandler<CertificatesOptions>()
{
@Override
public void onValueChange(ValueChangeEvent<CertificatesOptions> event)
{
logger.info("certsSelector changed " + event.getValue());
getUiHandlers().certificatesOptionsChanged(event.getValue(),
unassignedOnly.getValue());
}
});
this.unassignedOnly.addValueChangeHandler(new ValueChangeHandler<Boolean>()
{
@Override
public void onValueChange(ValueChangeEvent<Boolean> event)
{
logger.info("unassignedOnly changed");
getUiHandlers().certificatesOptionsChanged(certsSelector.getValue(), event.getValue());
}
});
this.accountsGridHandle = GridHelper.<TaggedName>initListGrid(
accountsGrid,
false,
new GridHelper.ColumnInfo<TaggedName>(
"Account",
(tn) -> tn.getName(),
null),
new GridHelper.ColumnInfo<TaggedName>(
"Description",
(tn) -> tn.getTag(),
null));
}
/**
* Returns the widgets that are managed by its view state machine.
*
* @return The widgets that are managed by its view state machine
*/
@Override
public HasEnabled[] get()
{
return new HasEnabled[]
{
refreshCerts,
addCert,
deleteCerts,
changeAlias,
viewCert,
unassignedOnly
};
}
/**
* Changes accounts table header depending on the selected certificate role.
*
* @param option
* The selected certificate role
*/
@Override
public void toggleAccountsHeader(CertificatesOptions option)
{
if (CertificatesOptions.PEER == option)
{
CertificatesView.this.accountsHeader.setText("Associated Accounts");
}
else
{
CertificatesView.this.accountsHeader.setText("Certificate Hierarchy");
}
if (CertificatesView.this.certsSelector.getValue() != option)
{
CertificatesView.this.certsSelector.setValue(option, false);
}
}
/**
* Sets certificates
*
* @param certs
* The certificates data (model)
*/
@Override
public void setCerts(TaggedName[] certs)
{
this.certsGridHandle.clearGrid();
if (certs != null)
{
this.certsGridHandle.addRows(certs);
this.certsGridHandle.getDataProvider().flush();
}
}
/**
* The refresh button click handler.
*
* @param event
* Click event
*/
@UiHandler("refreshCerts")
public void onRefreshCerts(ClickEvent event)
{
getUiHandlers().onRefreshCertificatesList(certsSelector.getValue(),
unassignedOnly.getValue());
}
/**
* The add button click handler.
*
* @param event
* Click event
*/
@UiHandler("addCert")
public void onAddCert(ClickEvent event)
{
// trigger for auto opened "add certificate" dialog
this.certDefView.setCertificateOption(certsSelector.getValue());
}
/**
* The change button click handler.
*
* @param event
* Click event
*/
@UiHandler("changeAlias")
public void onChangeAlias(ClickEvent event)
{
TaggedName selectedCert = this.certsSelectionModel.getSelectedSet().toArray(
new TaggedName[1])[0];
InputDialog.Field fAlias;
// trigger for auto opened "Change Alias" dialog
InputDialog inputDialog = this.inputDialogProvider.get();
inputDialog.build("Change Certificate Alias", "", new InputDialog.Field[]
{
fAlias = new InputDialog.Field("New Alias", selectedCert.getName(), false)
});
inputDialog.show(modalFragment, true, values ->
{
if (values != null)
{
String newAlias = (String) values.get(fAlias);
TaggedName selected = certsSelectionModel.getSelectedSet().toArray(
new TaggedName[1])[0];
getUiHandlers().changeAlias(selected, newAlias);
logger.info("inputDialog -> " + newAlias);
}
});
}
/**
* The view button click handler.
*
* @param event
* Click event
*/
@UiHandler("viewCert")
public void onViewCert(ClickEvent event)
{
// trigger for auto opened "view certificate" dialog
Set<TaggedName> selected = CertificatesView.this.certsSelectionModel.getSelectedSet();
getUiHandlers().viewCertificate(selected.toArray(new TaggedName[1])[0]);
}
/**
* The delete button click handler.
*
* @param event
* Click event
*/
@UiHandler("deleteCerts")
public void onDeleteCerts(ClickEvent event)
{
// trigger "Delete certificate" dialog
Set<TaggedName> selected = CertificatesView.this.certsSelectionModel.getSelectedSet();
getUiHandlers().onDeleteSelectedCertificates(selected);
}
/**
* Returns the certificate definition view.
*
* @return The certificate definition view
*/
@Override
public CertificateDefinitionView getCertificateDefinitionView()
{
return this.certDefView;
}
/**
* Sets accounts
*
* @param accounts
* The accounts data (model)
*/
@Override
public void setAccounts(TaggedName[] accounts)
{
this.accountsGridHandle.clearGrid();
if (accounts != null)
{
this.accountsGridHandle.addRows(accounts);
this.accountsGridHandle.getDataProvider().flush();
}
}
/**
* Tests if this set of certificates is selected
*
* @param selected
* The set of certificates
*
* @return True iff theses certificates have been selected, otherwise false.
*/
@Override
public boolean isSelectedCertificates(Set<TaggedName> selected)
{
Set<TaggedName> selectedCerts = certsSelectionModel.getSelectedSet();
return (selected != null) && selectedCerts.containsAll(selected) &&
selected.containsAll(selectedCerts);
}
/**
* Tests if this certificate is selected
*
* @param selected
* The target certificate
*
* @return True iff this target certificate has been selected, otherwise false.
*/
@Override
public boolean isSelectedCertificate(TaggedName selected)
{
Set<TaggedName> selectedCerts = certsSelectionModel.getSelectedSet();
return (selected != null) && selectedCerts.contains(selected) &&
(selectedCerts.size() == 1);
}
/**
* Select the given certificate by its tagged name.
*
* @param cert
* The certificate tagged name.
*/
@Override
public void selectCertificate(TaggedName cert)
{
requestToselect = true;
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
@Override
public void execute()
{
certsSelectionModel.setSelected(cert, true);
requestToselect = false;
}
});
}
/**
* Returns the certificate details view.
*
* @return The certificate details view
*/
@Override
public CertificateDetailsView getCertificateDetailsView()
{
return this.certDetailsView;
}
/**
* Select the given certificate by its alias.
*
* @param alias
* The certificate alias
*/
@Override
public void selectCertificate(String alias)
{
Predicate<TaggedName> searchFilter = record ->
{
if (record.getName().equals(alias))
{
return true;
}
return false;
};
Collection<TaggedName> results = Collections2.filter(
certsGridHandle.getDataProvider().getList(), searchFilter);
results.forEach(cert -> {
selectCertificate(cert); logger.info("select certificate " + cert.getName()); });
}
}