org.bubblecloud.ilves.module.audit.AuditLogFlowlet.java Source code

Java tutorial

Introduction

Here is the source code for org.bubblecloud.ilves.module.audit.AuditLogFlowlet.java

Source

/**
 * Copyright 2013 Tommi S.E. Laukkanen
 *
 * 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.bubblecloud.ilves.module.audit;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import org.bubblecloud.ilves.component.field.TimestampField;
import org.bubblecloud.ilves.component.flow.AbstractFlowlet;
import org.bubblecloud.ilves.component.grid.*;
import org.bubblecloud.ilves.model.AuditLogEntry;
import org.bubblecloud.ilves.util.ContainerUtil;
import org.joda.time.DateTime;
import org.vaadin.addons.lazyquerycontainer.EntityContainer;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * AuditLogEntry list flow.
 *
 * @author Tommi S.E. Laukkanen
 */
public final class AuditLogFlowlet extends AbstractFlowlet {

    /** Serial version UID. */
    private static final long serialVersionUID = 1L;
    /** The entity container. */
    private EntityContainer<AuditLogEntry> entityContainer;
    /** The content grid. */
    private Grid entityGrid;

    @Override
    public String getFlowletKey() {
        return "audit";
    }

    @Override
    public boolean isDirty() {
        return false;
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public void initialize() {
        // Get entity manager from site context and prepare container.
        final EntityManager entityManager = getSite().getSiteContext().getObject(EntityManager.class);
        entityContainer = new EntityContainer<AuditLogEntry>(entityManager, true, false, false, AuditLogEntry.class,
                1000, new String[] { "created" }, new boolean[] { false }, "auditLogEntryId");

        // Get descriptors and set container properties.
        final List<FilterDescriptor> filterDescriptors = new ArrayList<FilterDescriptor>();
        filterDescriptors.add(new FilterDescriptor("startTime", "created", getSite().localize("filter-start-time"),
                new TimestampField(), 200, ">=", Date.class, new DateTime().withTimeAtStartOfDay().toDate()));
        filterDescriptors.add(new FilterDescriptor("endTime", "created", getSite().localize("filter-end-time"),
                new TimestampField(), 200, "<=", Date.class,
                new DateTime().withTimeAtStartOfDay().plusDays(1).toDate()));
        final List<FieldDescriptor> fieldDescriptors = FieldSetDescriptorRegister
                .getFieldSetDescriptor(AuditLogEntry.class).getFieldDescriptors();
        ContainerUtil.addContainerProperties(entityContainer, fieldDescriptors);

        // Initialize layout
        final GridLayout gridLayout = new GridLayout(1, 2);
        gridLayout.setSizeFull();
        gridLayout.setMargin(false);
        gridLayout.setSpacing(true);
        gridLayout.setRowExpandRatio(1, 1f);
        setViewContent(gridLayout);
        final HorizontalLayout buttonLayout = new HorizontalLayout();
        buttonLayout.setSpacing(true);
        buttonLayout.setSizeUndefined();
        gridLayout.addComponent(buttonLayout, 0, 0);

        final Table table = new FormattingTable();
        table.setPageLength(13);

        // Initialize grid
        entityGrid = new Grid(table, entityContainer);
        entityGrid.setFields(fieldDescriptors);
        entityGrid.setFilters(filterDescriptors);
        gridLayout.addComponent(entityGrid, 0, 1);

        final Button viewButton = getSite().getButton("view");
        buttonLayout.addComponent(viewButton);
        viewButton.addClickListener(new ClickListener() {
            /** Serial version UID. */
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(final ClickEvent event) {
                if (entityGrid.getSelectedItemId() == null) {
                    return;
                }
                final AuditLogEntry entity = entityContainer.getEntity(entityGrid.getSelectedItemId());
                final AuditLogEntryFlowlet contentView = getFlow().forward(AuditLogEntryFlowlet.class);
                contentView.edit(entity, false);
            }
        });

    }

    @Override
    public void enter() {
        //final Company company = getSite().getSiteContext().getObject(Company.class);
        entityContainer.removeDefaultFilters();
        //entityContainer.addDefaultFilter(new Compare.Equal("owner.companyId", company.getCompanyId()));
        entityGrid.refresh();
    }

}