Search in sources :

Example 41 with Range

use of org.apache.commons.lang3.Range in project metron by apache.

the class WindowProcessorTest method testRepeatWithInclusionExclusion.

@Test
public void testRepeatWithInclusionExclusion() throws ParseException {
    Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago including holidays:us excluding weekends");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    Date now = sdf.parse("2017/12/26 12:00");
    List<Range<Long>> intervals = w.toIntervals(now.getTime());
    assertEquals(1, intervals.size());
}
Also used : Range(org.apache.commons.lang3.Range) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 42 with Range

use of org.apache.commons.lang3.Range in project metron by apache.

the class WindowProcessorTest method testRepeatWithWeekendExclusion.

@Test
public void testRepeatWithWeekendExclusion() {
    Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago excluding weekends");
    Date now = new Date();
    // avoid DST impacts if near Midnight
    now.setHours(6);
    List<Range<Long>> intervals = w.toIntervals(now.getTime());
    assertEquals(5, intervals.size());
}
Also used : Range(org.apache.commons.lang3.Range) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 43 with Range

use of org.apache.commons.lang3.Range in project metron by apache.

the class WindowLookbackTest method test.

public State test(String windowSelector, Date now, Optional<Map<String, Object>> config, Assertions... assertions) {
    List<Range<Long>> windowIntervals = WindowProcessor.process(windowSelector).toIntervals(now.getTime());
    String stellarStatement = "PROFILE_WINDOW('" + windowSelector + "', now" + (config.isPresent() ? ", config" : "") + ")";
    Map<String, Object> variables = new HashMap<>();
    variables.put("now", now.getTime());
    if (config.isPresent()) {
        variables.put("config", config.get());
    }
    StellarProcessor stellar = new StellarProcessor();
    List<ProfilePeriod> periods = (List<ProfilePeriod>) stellar.parse(stellarStatement, new DefaultVariableResolver(k -> variables.get(k), k -> variables.containsKey(k)), resolver, context);
    State state = new State(windowIntervals, periods);
    for (Assertions assertion : assertions) {
        assertTrue(assertion.test(state), assertion.name());
    }
    return state;
}
Also used : StellarProcessor(org.apache.metron.stellar.common.StellarProcessor) ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) Range(org.apache.commons.lang3.Range) Assertions(org.junit.jupiter.api.Assertions) DefaultVariableResolver(org.apache.metron.stellar.dsl.DefaultVariableResolver)

Example 44 with Range

use of org.apache.commons.lang3.Range in project cuba by cuba-platform.

the class EntityInspectorBrowse method createEntitiesTable.

protected void createEntitiesTable(MetaClass meta) {
    if (entitiesTable != null)
        tableBox.remove(entitiesTable);
    if (filter != null) {
        filterBox.remove(filter);
    }
    entitiesTable = uiComponents.create(Table.NAME);
    entitiesTable.setFrame(frame);
    if (companion != null) {
        companion.setHorizontalScrollEnabled(entitiesTable, true);
    }
    ClientType clientType = AppConfig.getClientType();
    if (clientType == ClientType.WEB) {
        textSelection.setVisible(true);
        textSelection.addValueChangeListener(e -> changeTableTextSelectionEnabled());
    }
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
    Function<?, String> dateTimeFormatter = value -> {
        if (value == null) {
            return StringUtils.EMPTY;
        }
        return dateTimeFormat.format(value);
    };
    // collect properties in order to add non-system columns first
    List<Table.Column> nonSystemPropertyColumns = new ArrayList<>(10);
    List<Table.Column> systemPropertyColumns = new ArrayList<>(10);
    for (MetaProperty metaProperty : meta.getProperties()) {
        // don't show embedded, transient & multiple referred entities
        if (isEmbedded(metaProperty) || metadata.getTools().isNotPersistent(metaProperty)) {
            continue;
        }
        Range range = metaProperty.getRange();
        if (range.getCardinality().isMany()) {
            continue;
        }
        Table.Column column = new Table.Column(meta.getPropertyPath(metaProperty.getName()));
        if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
            column.setFormatter(dateTimeFormatter);
        }
        if (metaProperty.getJavaType().equals(String.class)) {
            column.setMaxTextLength(MAX_TEXT_LENGTH);
        }
        if (!metadata.getTools().isSystem(metaProperty)) {
            column.setCaption(getPropertyCaption(meta, metaProperty));
            nonSystemPropertyColumns.add(column);
        } else {
            column.setCaption(metaProperty.getName());
            systemPropertyColumns.add(column);
        }
    }
    for (Table.Column column : nonSystemPropertyColumns) {
        entitiesTable.addColumn(column);
    }
    for (Table.Column column : systemPropertyColumns) {
        entitiesTable.addColumn(column);
    }
    if (entitiesDs != null) {
        ((DsContextImplementation) getDsContext()).unregister(entitiesDs);
    }
    entitiesDs = DsBuilder.create(getDsContext()).setId("entitiesDs").setMetaClass(meta).setView(createView(meta)).buildCollectionDatasource();
    entitiesDs.setLoadDynamicAttributes(true);
    entitiesDs.setSoftDeletion(!removedRecords.isChecked());
    entitiesDs.setQuery(String.format("select e from %s e", meta.getName()));
    entitiesTable.setDatasource(entitiesDs);
    tableBox.add(entitiesTable);
    entitiesTable.setSizeFull();
    createButtonsPanel(entitiesTable);
    RowsCount rowsCount = uiComponents.create(RowsCount.class);
    rowsCount.setDatasource(entitiesDs);
    entitiesTable.setRowsCount(rowsCount);
    entitiesTable.setEnterPressAction(entitiesTable.getAction("edit"));
    entitiesTable.setItemClickAction(entitiesTable.getAction("edit"));
    entitiesTable.setMultiSelect(true);
    entitiesTable.addStyleName("table-boolean-text");
    createFilter();
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) java.util(java.util) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) LoggerFactory(org.slf4j.LoggerFactory) ParamsMap(com.haulmont.bali.util.ParamsMap) CubaIcon(com.haulmont.cuba.gui.icons.CubaIcon) SimpleDateFormat(java.text.SimpleDateFormat) JsonParser(com.google.gson.JsonParser) Icons(com.haulmont.cuba.gui.icons.Icons) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) JsonElement(com.google.gson.JsonElement) Inject(javax.inject.Inject) JSON(com.haulmont.cuba.gui.export.ExportFormat.JSON) Files(com.google.common.io.Files) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) com.haulmont.cuba.core.app.importexport(com.haulmont.cuba.core.app.importexport) ExportFormat(com.haulmont.cuba.gui.export.ExportFormat) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) com.haulmont.cuba.gui.components.actions(com.haulmont.cuba.gui.components.actions) Nullable(javax.annotation.Nullable) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Logger(org.slf4j.Logger) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Sets(com.google.common.collect.Sets) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ZIP(com.haulmont.cuba.gui.export.ExportFormat.ZIP) Consumer(java.util.function.Consumer) IOUtils(org.apache.commons.io.IOUtils) EntityOp(com.haulmont.cuba.security.entity.EntityOp) ByteArrayDataProvider(com.haulmont.cuba.gui.export.ByteArrayDataProvider) AppConfig(com.haulmont.cuba.gui.AppConfig) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) ClientConfig(com.haulmont.cuba.client.ClientConfig) UiComponents(com.haulmont.cuba.gui.UiComponents) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Session(com.haulmont.chile.core.model.Session) Entity(com.haulmont.cuba.core.entity.Entity) InputStream(java.io.InputStream) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) SimpleDateFormat(java.text.SimpleDateFormat)

Example 45 with Range

use of org.apache.commons.lang3.Range in project cuba by cuba-platform.

the class EntityRestore method buildLayout.

protected void buildLayout() {
    Object value = entities.getValue();
    if (value != null) {
        MetaClass metaClass = (MetaClass) value;
        MetaProperty deleteTsMetaProperty = metaClass.getProperty("deleteTs");
        if (deleteTsMetaProperty != null) {
            if (entitiesTable != null) {
                tablePanel.remove(entitiesTable);
            }
            if (filter != null) {
                tablePanel.remove(filter);
            }
            entitiesTable = uiComponents.create(Table.NAME);
            entitiesTable.setFrame(frame);
            restoreButton = uiComponents.create(Button.class);
            restoreButton.setId("restore");
            restoreButton.setCaption(getMessage("entityRestore.restore"));
            ButtonsPanel buttonsPanel = uiComponents.create(ButtonsPanel.class);
            buttonsPanel.add(restoreButton);
            entitiesTable.setButtonsPanel(buttonsPanel);
            RowsCount rowsCount = uiComponents.create(RowsCount.class);
            entitiesTable.setRowsCount(rowsCount);
            SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
            Function<Object, String> dateTimeFormatter = propertyValue -> {
                if (propertyValue == null) {
                    return StringUtils.EMPTY;
                }
                return dateTimeFormat.format(propertyValue);
            };
            // collect properties in order to add non-system columns first
            LinkedList<Table.Column<Entity>> nonSystemPropertyColumns = new LinkedList<>();
            LinkedList<Table.Column<Entity>> systemPropertyColumns = new LinkedList<>();
            List<MetaProperty> metaProperties = new ArrayList<>();
            for (MetaProperty metaProperty : metaClass.getProperties()) {
                // don't show embedded, transient & multiple referred entities
                Range range = metaProperty.getRange();
                if (isEmbedded(metaProperty) || metadataTools.isNotPersistent(metaProperty) || range.getCardinality().isMany() || metadataTools.isSystemLevel(metaProperty) || (range.isClass() && metadataTools.isSystemLevel(range.asClass()))) {
                    continue;
                }
                metaProperties.add(metaProperty);
                Table.Column<Entity> column = new Table.Column<>(metaClass.getPropertyPath(metaProperty.getName()));
                if (!metadataTools.isSystem(metaProperty)) {
                    column.setCaption(getPropertyCaption(metaClass, metaProperty));
                    nonSystemPropertyColumns.add(column);
                } else {
                    column.setCaption(metaProperty.getName());
                    column.setDescription(getSystemAttributeDescription(metaClass, metaProperty));
                    systemPropertyColumns.add(column);
                }
                if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
                    column.setFormatter(dateTimeFormatter);
                }
            }
            for (Table.Column<Entity> column : nonSystemPropertyColumns) {
                entitiesTable.addColumn(column);
            }
            for (Table.Column<Entity> column : systemPropertyColumns) {
                entitiesTable.addColumn(column);
            }
            DsContext dsContext = getDsContext();
            if (entitiesDs != null) {
                ((DsContextImplementation) dsContext).unregister(entitiesDs);
            }
            entitiesDs = DsBuilder.create(dsContext).setId("entitiesDs").setMetaClass(metaClass).setView(buildView(metaClass, metaProperties)).buildGroupDatasource();
            entitiesDs.setQuery("select e from " + metaClass.getName() + " e " + "where e.deleteTs is not null order by e.deleteTs");
            entitiesDs.setSoftDeletion(false);
            entitiesDs.refresh();
            entitiesTable.setDatasource(entitiesDs);
            String filterId = metaClass.getName().replace("$", "") + "GenericFilter";
            filter = uiComponents.create(Filter.class);
            filter.setId(filterId);
            filter.setFrame(getFrame());
            StringBuilder sb = new StringBuilder();
            for (MetaProperty property : metaClass.getProperties()) {
                AnnotatedElement annotatedElement = property.getAnnotatedElement();
                if (annotatedElement.getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class) != null) {
                    sb.append(property.getName()).append("|");
                }
            }
            Element filterElement = dom4JTools.readDocument(String.format("<filter id=\"%s\">\n" + "    <properties include=\".*\" exclude=\"\"/>\n" + "</filter>", filterId)).getRootElement();
            String excludedProperties = sb.toString();
            if (StringUtils.isNotEmpty(excludedProperties)) {
                Element properties = filterElement.element("properties");
                properties.attribute("exclude").setValue(excludedProperties.substring(0, excludedProperties.lastIndexOf("|")));
            }
            ((HasXmlDescriptor) filter).setXmlDescriptor(filterElement);
            filter.setUseMaxResults(true);
            filter.setDatasource(entitiesDs);
            entitiesTable.setSizeFull();
            entitiesTable.setMultiSelect(true);
            Action restoreAction = new ItemTrackingAction("restore").withCaption(getMessage("entityRestore.restore")).withPrimary(true).withHandler(event -> showRestoreDialog());
            entitiesTable.addAction(restoreAction);
            restoreButton.setAction(restoreAction);
            tablePanel.add(filter);
            tablePanel.add(entitiesTable);
            tablePanel.expand(entitiesTable, "100%", "100%");
            entitiesTable.refresh();
            ((FilterImplementation) filter).loadFiltersAndApplyDefault();
        }
    }
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) java.util(java.util) Dom4jTools(com.haulmont.cuba.core.sys.xmlparsing.Dom4jTools) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) EntityRestoreService(com.haulmont.cuba.core.app.EntityRestoreService) SimpleDateFormat(java.text.SimpleDateFormat) EnableRestore(com.haulmont.cuba.core.entity.annotation.EnableRestore) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) GroupDatasource(com.haulmont.cuba.gui.data.GroupDatasource) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Status(com.haulmont.cuba.gui.components.Action.Status) Type(com.haulmont.cuba.gui.components.DialogAction.Type) Element(org.dom4j.Element) UiComponents(com.haulmont.cuba.gui.UiComponents) Entity(com.haulmont.cuba.core.entity.Entity) AnnotatedElement(java.lang.reflect.AnnotatedElement) Entity(com.haulmont.cuba.core.entity.Entity) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) AnnotatedElement(java.lang.reflect.AnnotatedElement) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) Range(com.haulmont.chile.core.model.Range) MetaClass(com.haulmont.chile.core.model.MetaClass) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

List (java.util.List)30 HashMap (java.util.HashMap)24 Map (java.util.Map)24 ArrayList (java.util.ArrayList)23 Collectors (java.util.stream.Collectors)21 StringUtils (org.apache.commons.lang3.StringUtils)20 LoggerFactory (org.slf4j.LoggerFactory)17 Pair (org.apache.commons.lang3.tuple.Pair)16 Logger (org.slf4j.Logger)16 Set (java.util.Set)15 IOException (java.io.IOException)14 Optional (java.util.Optional)12 Range (org.apache.commons.lang3.Range)11 Test (org.junit.jupiter.api.Test)11 java.util (java.util)10 Date (java.util.Date)10 Lists (com.google.common.collect.Lists)9 HashSet (java.util.HashSet)9 ExecutorService (java.util.concurrent.ExecutorService)9 Collection (java.util.Collection)8