org.opennms.features.pluginmgr.vaadin.pluginmanager.LicenceDescriptorPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.opennms.features.pluginmgr.vaadin.pluginmanager.LicenceDescriptorPanel.java

Source

/*
 * Copyright 2014 OpenNMS Group Inc., Entimoss ltd.
 * Licensed 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.opennms.features.pluginmgr.vaadin.pluginmanager;

import java.text.Format;
import java.text.SimpleDateFormat;

import org.opennms.karaf.licencemgr.metadata.Licence;
import org.opennms.karaf.licencemgr.metadata.jaxb.LicenceMetadata;
import org.opennms.karaf.licencemgr.metadata.jaxb.OptionMetadata;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class LicenceDescriptorPanel extends CustomComponent {

    /*- VaadinEditorProperties={"grid":"RegularGrid,2","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    @AutoGenerated
    private VerticalLayout mainLayout;

    @AutoGenerated
    private VerticalLayout licenceMetadataVerticalLayout;

    @AutoGenerated
    private VerticalLayout licenceOptionsVerticalLayout;

    @AutoGenerated
    private VerticalLayout systemIdsVerticalLayout;

    @AutoGenerated
    private TextField maxSizeSystemIdsTextField;

    @AutoGenerated
    private TextField startDateTextField;

    @AutoGenerated
    private TextField expiryDateTextField;

    @AutoGenerated
    private TextField durationTextField;

    @AutoGenerated
    private TextField licensorTextField;

    @AutoGenerated
    private TextField licenseeTextField;

    @AutoGenerated
    private TextField featureRepositoryTextField;

    @AutoGenerated
    private TextField productIdTextField;

    @AutoGenerated
    private TextArea licenceTextArea;

    private static final long serialVersionUID = 1L;

    private boolean noUpdate = true; // sets fields to read only

    /**
     * if true sets all fields to read only
     * @return
     */
    public boolean getNoUpdate() {
        return noUpdate;
    }

    /**
     * if true sets all fields to read only
     * @param noUpdate
     */
    public void setNoUpdate(boolean noUpdate) {
        this.noUpdate = noUpdate;
    }

    /**
     * The constructor should first build the main layout, set the
     * composition root and then do any custom initialization.
     *
     * The constructor will not be automatically regenerated by the
     * visual editor.
     */
    public LicenceDescriptorPanel() {
        buildMainLayout();
        setCompositionRoot(mainLayout);

        // manually add user code here

        // set all fields default no update
        maxSizeSystemIdsTextField.setReadOnly(true);
        startDateTextField.setReadOnly(true);
        expiryDateTextField.setReadOnly(true);
        durationTextField.setReadOnly(true);
        licensorTextField.setReadOnly(true);
        licenseeTextField.setReadOnly(true);
        featureRepositoryTextField.setReadOnly(true);
        productIdTextField.setReadOnly(true);
        licenceTextArea.setReadOnly(true);

    }

    /**
     * updates the metadata fields from the licence string
     * if unable to read the metadata then returns false
     * @param licenceStr
     * @return true if metadata read, false if not
     */
    public boolean updateMetadata(String licenceStr) {

        boolean success = false;

        if (licenceStr != null) {

            LicenceMetadata licenceMetadata = null;

            try {
                licenceMetadata = Licence.getUnverifiedMetadata(licenceStr);
            } catch (Exception e) {
                // can't decode string licenceMetadata will be null
            }

            if (licenceMetadata == null) {
                //create empty licenceMetadata panel
                licenceMetadata = new LicenceMetadata();
                licenceMetadata.setStartDate(null);
                licenceMetadata.setProductId("cannot decode licence string");
                success = false;
            } else
                success = true;

            productIdTextField.setReadOnly(false);
            productIdTextField
                    .setValue((licenceMetadata.getProductId() == null) ? "" : licenceMetadata.getProductId());
            productIdTextField.setReadOnly(noUpdate);

            featureRepositoryTextField.setReadOnly(false);
            featureRepositoryTextField.setValue(
                    (licenceMetadata.getFeatureRepository() == null) ? "" : licenceMetadata.getFeatureRepository());
            featureRepositoryTextField.setReadOnly(noUpdate);

            Format formatter = new SimpleDateFormat("yyyy-MM-dd");

            startDateTextField.setReadOnly(false);
            startDateTextField.setValue((licenceMetadata.getStartDate() == null) ? ""
                    : formatter.format(licenceMetadata.getStartDate()));
            startDateTextField.setReadOnly(noUpdate);

            expiryDateTextField.setReadOnly(false);
            expiryDateTextField.setValue((licenceMetadata.getExpiryDate() == null) ? ""
                    : formatter.format(licenceMetadata.getExpiryDate()));
            expiryDateTextField.setReadOnly(noUpdate);

            durationTextField.setReadOnly(false);
            durationTextField
                    .setValue((licenceMetadata.getDuration() == null) ? "" : licenceMetadata.getDuration());
            durationTextField.setReadOnly(noUpdate);

            licenseeTextField.setReadOnly(false);
            licenseeTextField
                    .setValue((licenceMetadata.getLicensee() == null) ? "" : licenceMetadata.getLicensee());
            licenseeTextField.setReadOnly(noUpdate);

            licensorTextField.setReadOnly(false);
            licensorTextField
                    .setValue((licenceMetadata.getLicensor() == null) ? "" : licenceMetadata.getLicensor());
            licensorTextField.setReadOnly(noUpdate);

            maxSizeSystemIdsTextField.setReadOnly(false);
            maxSizeSystemIdsTextField.setValue(
                    (licenceMetadata.getMaxSizeSystemIds() == null) ? "" : licenceMetadata.getMaxSizeSystemIds());
            maxSizeSystemIdsTextField.setReadOnly(noUpdate);

            // display systemIds if present
            systemIdsVerticalLayout.removeAllComponents();

            // add error messages if system ids defined when not expected
            Label l1 = new Label();
            l1.setContentMode(ContentMode.HTML);
            Integer licenceMetadataMaxSizeSystemIds = null;
            try {
                licenceMetadataMaxSizeSystemIds = Integer.parseInt(licenceMetadata.getMaxSizeSystemIds());
            } catch (Exception e) {
            }
            if (licenceMetadataMaxSizeSystemIds == null || licenceMetadataMaxSizeSystemIds == 0) {
                // if maxSizeSystemIds=0 check if systemId's list has entries (an error)
                if (licenceMetadata.getSystemIds() == null || licenceMetadata.getSystemIds().isEmpty()) {
                    l1.setValue("<div style=\"color: green;\">" + "All SystemId's Accepted" + "</div>");
                } else {
                    l1.setValue("<div style=\"color: red;\">" + "Licence Error: MaxSizeSystemIds=0 but "
                            + licenceMetadata.getSystemIds().size() + " systemId's are defined" + "</div>");
                }
                systemIdsVerticalLayout.addComponent(l1);
            } else if (licenceMetadata.getSystemIds() == null || licenceMetadata.getSystemIds().isEmpty()) {
                l1.setValue("<div style=\"color: green;\">" + "No SystemId's Defined" + "</div>");
                systemIdsVerticalLayout.addComponent(l1);
            } else if (licenceMetadata.getSystemIds().size() > licenceMetadataMaxSizeSystemIds) {
                l1.setValue("<div style=\"color: red;\">Licence Error: more system id's defined  in licence ("
                        + licenceMetadata.getSystemIds().size() + ") than allowed in maxSizeSystemIds="
                        + licenceMetadata.getMaxSizeSystemIds() + "</div>");
                systemIdsVerticalLayout.addComponent(l1);
            }

            // add system id strings if present
            if (licenceMetadata.getSystemIds() != null) {
                for (String systemId : licenceMetadata.getSystemIds()) {
                    Label systemIdField = new Label();
                    systemIdField.setImmediate(true);
                    systemIdField.setWidth("400px");
                    systemIdField.setHeight("-1px");
                    systemIdField.setValue(systemId);
                    systemIdsVerticalLayout.addComponent(systemIdField);
                }
            }

            // display licence options if present
            licenceOptionsVerticalLayout.removeAllComponents();
            if (licenceMetadata.getOptions() == null || licenceMetadata.getOptions().isEmpty()) {
                Label l = new Label("No Licence Options Defined");
                licenceOptionsVerticalLayout.addComponent(l);
            } else {
                for (OptionMetadata option : licenceMetadata.getOptions()) {
                    TextField optionField = new TextField();
                    optionField.setImmediate(true);
                    optionField.setWidth("400px");
                    optionField.setHeight("-1px");
                    optionField.setCaption(option.getName());
                    optionField.setValue(option.getValue());
                    optionField.setDescription(option.getDescription());
                    optionField.setReadOnly(noUpdate);
                    licenceOptionsVerticalLayout.addComponent(optionField);
                }
            }

            mainLayout.markAsDirty();

        }
        return success;
    }

    public boolean setLicenceString(String licenceStr) {
        if (licenceStr == null) {
            licenceTextArea.setReadOnly(false);
            licenceTextArea.setValue("");
            licenceTextArea.setReadOnly(noUpdate);
        } else {
            licenceTextArea.setReadOnly(false);
            licenceTextArea.setValue(licenceStr);
            licenceTextArea.setReadOnly(noUpdate);
        }
        return updateMetadata(licenceStr);
    }

    public String getLicenceString() {
        String licenceStr = licenceTextArea.getValue();
        return licenceStr;
    }

    @AutoGenerated
    private VerticalLayout buildMainLayout() {
        // common part: create layout
        mainLayout = new VerticalLayout();
        mainLayout.setImmediate(true);
        mainLayout.setWidth("100%");
        mainLayout.setHeight("100%");
        mainLayout.setMargin(false);
        mainLayout.setSpacing(true);

        // top-level component properties
        setWidth("100.0%");
        setHeight("100.0%");

        // licenceTextArea
        licenceTextArea = new TextArea();
        licenceTextArea.setCaption("Licence String");
        licenceTextArea.setImmediate(true);
        licenceTextArea.setWidth("400px");
        licenceTextArea.setHeight("200px");
        mainLayout.addComponent(licenceTextArea);

        // licenceMetadataVerticalLayout
        licenceMetadataVerticalLayout = buildLicenceMetadataVerticalLayout();
        mainLayout.addComponent(licenceMetadataVerticalLayout);

        return mainLayout;
    }

    @AutoGenerated
    private VerticalLayout buildLicenceMetadataVerticalLayout() {
        // common part: create layout
        licenceMetadataVerticalLayout = new VerticalLayout();
        licenceMetadataVerticalLayout.setCaption("Licence Metadata");
        licenceMetadataVerticalLayout.setImmediate(true);
        licenceMetadataVerticalLayout.setWidth("-1px");
        licenceMetadataVerticalLayout.setHeight("-1px");
        licenceMetadataVerticalLayout.setMargin(false);
        licenceMetadataVerticalLayout.setSpacing(true);

        // productIdTextField
        productIdTextField = new TextField();
        productIdTextField.setCaption("Product Id");
        productIdTextField.setImmediate(true);
        productIdTextField.setWidth("400px");
        productIdTextField.setHeight("-1px");
        licenceMetadataVerticalLayout.addComponent(productIdTextField);

        // featureRepositoryTextField
        featureRepositoryTextField = new TextField();
        featureRepositoryTextField.setCaption("Feature Repository");
        featureRepositoryTextField.setImmediate(true);
        featureRepositoryTextField.setWidth("400px");
        featureRepositoryTextField.setHeight("-1px");
        licenceMetadataVerticalLayout.addComponent(featureRepositoryTextField);

        // licenseeTextField
        licenseeTextField = new TextField();
        licenseeTextField.setCaption("Licensee");
        licenseeTextField.setImmediate(true);
        licenseeTextField.setDescription("The person or organisation to whom the licence applies.");
        licenseeTextField.setWidth("400px");
        licenseeTextField.setHeight("-1px");
        licenceMetadataVerticalLayout.addComponent(licenseeTextField);

        // licensorTextField
        licensorTextField = new TextField();
        licensorTextField.setCaption("Licensor");
        licensorTextField.setImmediate(true);
        licensorTextField.setDescription("The person or organisation issuing the licence");
        licensorTextField.setWidth("400px");
        licensorTextField.setHeight("-1px");
        licenceMetadataVerticalLayout.addComponent(licensorTextField);

        // durationTextField
        durationTextField = new TextField();
        durationTextField.setCaption("Duration");
        durationTextField.setImmediate(true);
        durationTextField.setDescription("The duration in days from the start date.");
        durationTextField.setWidth("400px");
        durationTextField.setHeight("-1px");
        licenceMetadataVerticalLayout.addComponent(durationTextField);

        // expiryDateTextField
        expiryDateTextField = new TextField();
        expiryDateTextField.setCaption("Expiry Date");
        expiryDateTextField.setImmediate(true);
        expiryDateTextField.setDescription(
                "The date after which the licence expires. Note if present, Duration takes priority over this date.");
        expiryDateTextField.setWidth("400px");
        expiryDateTextField.setHeight("-1px");
        licenceMetadataVerticalLayout.addComponent(expiryDateTextField);

        // startDateTextField
        startDateTextField = new TextField();
        startDateTextField.setCaption("Start Date");
        startDateTextField.setImmediate(true);
        startDateTextField.setDescription("The date on which the licence becomes valid");
        startDateTextField.setWidth("400px");
        startDateTextField.setHeight("-1px");
        licenceMetadataVerticalLayout.addComponent(startDateTextField);

        // maxSizeSystemIdsTextField
        maxSizeSystemIdsTextField = new TextField();
        maxSizeSystemIdsTextField.setCaption("Maximum number of systemId's in licence");
        maxSizeSystemIdsTextField.setImmediate(true);
        maxSizeSystemIdsTextField.setDescription(
                "maxSizeSystemIds is the maximum number of systemId's which can be included in the systemIds list.This is used by the shopping cart to limit the number of systems to which licences can be applied.the actual number of systemId entries in systemId's must be <= maxSizeSystemIds for the licence to authenticate.If maxSizeSystemIds==0 then no systemId's should be included in the list and the licence will authenticate with any and all systemIds.");
        maxSizeSystemIdsTextField.setWidth("400px");
        maxSizeSystemIdsTextField.setHeight("-1px");
        licenceMetadataVerticalLayout.addComponent(maxSizeSystemIdsTextField);

        // systemIdsVerticalLayout
        systemIdsVerticalLayout = new VerticalLayout();
        systemIdsVerticalLayout.setCaption("Licenced System Id's");
        systemIdsVerticalLayout.setImmediate(false);
        systemIdsVerticalLayout.setDescription("System Id's which can be authenticated by this licence");
        systemIdsVerticalLayout.setWidth("-1px");
        systemIdsVerticalLayout.setHeight("-1px");
        systemIdsVerticalLayout.setMargin(false);
        licenceMetadataVerticalLayout.addComponent(systemIdsVerticalLayout);

        // licenceOptionsVerticalLayout
        licenceOptionsVerticalLayout = new VerticalLayout();
        licenceOptionsVerticalLayout.setCaption("Licence Options");
        licenceOptionsVerticalLayout.setImmediate(true);
        licenceOptionsVerticalLayout.setWidth("-1px");
        licenceOptionsVerticalLayout.setHeight("-1px");
        licenceOptionsVerticalLayout.setMargin(false);
        licenceMetadataVerticalLayout.addComponent(licenceOptionsVerticalLayout);

        return licenceMetadataVerticalLayout;
    }

}