org.alfresco.util.CachingDateFormat Java Examples

The following examples show how to use org.alfresco.util.CachingDateFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QParserPluginIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void dateTimeRanges()
{
    String fieldName = escape(QName.createQName(TEST_NAMESPACE, "datetime-ista"));
    stream(CachingDateFormat.getLenientFormatters())
            .filter(formatter -> formatter.getResolution() > Calendar.DAY_OF_MONTH)
            .forEach(formatter -> {
                String date = formatter.getSimpleDateFormat().format(FTS_TEST_DATE);

                stream(msecs)
                        .forEach(milliseconds ->
                        {
                            String startDate = formatter.getSimpleDateFormat().format(new Date(FTS_TEST_DATE.getTime() - milliseconds));
                            String endDate = formatter.getSimpleDateFormat().format(new Date(FTS_TEST_DATE.getTime() + milliseconds));

                            assertAQuery("\\@" + fieldName + ":[" + startDate + " TO " + endDate + "]", 1);
                            assertAQuery("\\@" + fieldName + ":[" + date + " TO " + endDate + "]", 1);
                            assertAQuery("\\@" + fieldName + ":[" + startDate + " TO " + date + "]", 1);
                            assertAQuery("\\@" + fieldName + ":{" + date + " TO " + endDate + "}", 0);
                            assertAQuery("\\@" + fieldName + ":{" + startDate + " TO " + date + "}", 0);
                        });
            });
}
 
Example #2
Source File: M2XML.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Convert XML date (of the form yyyy-MM-dd) to Date 
 * 
 * @param date  the xml representation of the date
 * @return  the date
 * @throws ParseException
 */
public static Date deserialiseDate(String date)
    throws ParseException
{
    Date xmlDate = null;
    if (date != null)
    {
        SimpleDateFormat df = CachingDateFormat.getDateOnlyFormat();
        xmlDate = df.parse(date);
    }
    return xmlDate;
}
 
Example #3
Source File: M2XML.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Convert date to XML date (of the form yyyy-MM-dd)
 * 
 * @param date  the date
 * @return  the xml representation of the date
 */
public static String serialiseDate(Date date)
{
    String xmlDate = null;
    if (date != null)
    {
        SimpleDateFormat df = CachingDateFormat.getDateOnlyFormat();
        xmlDate = df.format(date);
    }
    return xmlDate;
}
 
Example #4
Source File: QParserPluginIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void dateLiterals()
{
    stream(CachingDateFormat.getLenientFormatters())
        .filter(formatter -> formatter.getResolution() < Calendar.DAY_OF_MONTH)
        .map(SimpleDateFormatAndResolution::getSimpleDateFormat)
        .peek(simpleDateFormat -> simpleDateFormat.setTimeZone(UTC))
        .map(simpleDateFormat -> simpleDateFormat.format(FTS_TEST_DATE))
        .filter(date -> date.length() >= 9)
        .forEach(date -> assertAQuery("\\@" + escape(QName.createQName(TEST_NAMESPACE, "date-ista")) + ":\"" + date + "\"", 1));
}
 
Example #5
Source File: QParserPluginIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void dateTimeLiterals()
{
    stream(CachingDateFormat.getLenientFormatters())
            .filter(formatter -> formatter.getResolution() > Calendar.DAY_OF_MONTH)
            .map(formatter -> formatter.getSimpleDateFormat().format(FTS_TEST_DATE))
            .forEach(date -> assertAQuery("\\@" + escape(QName.createQName(TEST_NAMESPACE, "datetime-ista")) + ":\"" + date + "\"", 1));
}
 
Example #6
Source File: QParserPluginIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void fieldNamesAreCaseInsensitive()
{
    Date now = new Date();
    stream(CachingDateFormat.getLenientFormatters())
            .filter(formatter -> formatter.getResolution() > Calendar.DAY_OF_MONTH)
            .map(formatter -> formatter.getSimpleDateFormat().format(now))
            .forEach(date -> {
                assertAQuery("\\@cM\\:CrEaTeD:[MIN TO " + date + "]", 1);
                assertAQuery("\\@cM\\:CrEaTeD:[MIN TO " + date + "]", 1);
                assertAQuery("\\@CM\\:CrEaTeD:[MIN TO " + date + "]", 1);
                assertAQuery("\\@cm\\:created:[MIN TO NOW]", 1);
                assertAQuery("\\@" + escape(PROP_CREATED) + ":[MIN TO " + date + "]", 1);
            });
}
 
Example #7
Source File: QParserPluginIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void dateRanges()
{
    String fieldName = escape(QName.createQName(TEST_NAMESPACE, "date-ista"));
    stream(CachingDateFormat.getLenientFormatters())
            .filter(formatter -> formatter.getResolution() > Calendar.DAY_OF_MONTH)
            .map(formatter -> formatter.getSimpleDateFormat().format(FTS_TEST_DATE))
            .filter(date -> date.length() >= 9)
            .forEach(date -> {
                assertAQuery("\\@" + fieldName + ":[" + date + " TO " + date + "]", 1);
                assertAQuery("\\@" + fieldName + ":[MIN  TO " + date + "]", 1);
                assertAQuery("\\@" + fieldName + ":[" + date + " TO MAX]", 1);
            });
}
 
Example #8
Source File: PeriodTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * End of FQ
 * 
 * @throws ParseException
 */
public void test12EndOfDefaultFinancialQuarter() throws ParseException
{
    PeriodProvider provider = Period.getProvider(EndOfFinancialQuarter.PERIOD_TYPE);
    assertEquals(provider.getDefaultExpression(), "1");
    assertEquals(provider.getExpressionMutiplicity(), PeriodProvider.ExpressionMutiplicity.OPTIONAL);
    assertEquals(provider.getPeriodType(), EndOfFinancialQuarter.PERIOD_TYPE);
    assertEquals(DataTypeDefinition.INT, provider.getExpressionDataType());
    assertEquals("End Of Financial Quarter", provider.getDisplayLabel());
    
    AbstractEndOfCalendarPeriodProvider instance = (AbstractEndOfCalendarPeriodProvider) provider;
    instance.setStartDayOfMonth(1);
    instance.setStartMonth(Calendar.JANUARY);

    SimpleDateFormat df = CachingDateFormat.getDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", true);
    Period period = new Period(EndOfFinancialQuarter.PERIOD_TYPE);
    Date date;
    date = df.parse("2008-10-01T00:00:00.000");
    assertEquals("2008-12-31T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2008-12-31T23:59:59.998");
    assertEquals("2008-12-31T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2008-12-31T23:59:59.999");
    assertEquals("2009-03-31T23:59:59.999", df.format(period.getNextDate(date)));

    date = df.parse("2009-01-01T00:00:00.000");
    assertEquals("2009-03-31T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-03-31T23:59:59.998");
    assertEquals("2009-03-31T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-03-31T23:59:59.999");
    assertEquals("2009-06-30T23:59:59.999", df.format(period.getNextDate(date)));

    date = df.parse("2009-04-01T00:00:00.000");
    assertEquals("2009-06-30T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-06-30T23:59:59.998");
    assertEquals("2009-06-30T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-06-30T23:59:59.999");
    assertEquals("2009-09-30T23:59:59.999", df.format(period.getNextDate(date)));

    date = df.parse("2009-07-01T00:00:00.000");
    assertEquals("2009-09-30T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-09-30T23:59:59.998");
    assertEquals("2009-09-30T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-09-30T23:59:59.999");
    assertEquals("2009-12-31T23:59:59.999", df.format(period.getNextDate(date)));

    date = df.parse("2009-10-01T00:00:00.000");
    assertEquals("2009-12-31T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-12-31T23:59:59.998");
    assertEquals("2009-12-31T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-12-31T23:59:59.999");
    assertEquals("2010-03-31T23:59:59.999", df.format(period.getNextDate(date)));
}
 
Example #9
Source File: PeriodTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * End of FQ
 * 
 * @throws ParseException
 */
public void test14EndOfUKTaxQuarter() throws ParseException
{
    PeriodProvider provider = Period.getProvider(EndOfFinancialQuarter.PERIOD_TYPE);
    assertEquals(provider.getDefaultExpression(), "1");
    assertEquals(provider.getExpressionMutiplicity(), PeriodProvider.ExpressionMutiplicity.OPTIONAL);
    assertEquals(provider.getPeriodType(), EndOfFinancialQuarter.PERIOD_TYPE);
    assertEquals(DataTypeDefinition.INT, provider.getExpressionDataType());

    AbstractEndOfCalendarPeriodProvider instance = (AbstractEndOfCalendarPeriodProvider) provider;
    instance.setStartDayOfMonth(6);
    instance.setStartMonth(Calendar.APRIL);
    
    SimpleDateFormat df = CachingDateFormat.getDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", true);
    Period period = new Period(EndOfFinancialQuarter.PERIOD_TYPE);
    Date date;
    date = df.parse("2008-04-06T00:00:00.000");
    assertEquals("2008-07-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2008-07-05T23:59:59.998");
    assertEquals("2008-07-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2008-07-05T23:59:59.999");
    assertEquals("2008-10-05T23:59:59.999", df.format(period.getNextDate(date)));

    date = df.parse("2008-07-06T00:00:00.000");
    assertEquals("2008-10-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2008-10-05T23:59:59.998");
    assertEquals("2008-10-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2008-10-05T23:59:59.999");
    assertEquals("2009-01-05T23:59:59.999", df.format(period.getNextDate(date)));

    date = df.parse("2008-10-06T00:00:00.000");
    assertEquals("2009-01-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-01-05T23:59:59.998");
    assertEquals("2009-01-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-01-05T23:59:59.999");
    assertEquals("2009-04-05T23:59:59.999", df.format(period.getNextDate(date)));

    date = df.parse("2009-01-06T00:00:00.000");
    assertEquals("2009-04-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-04-05T23:59:59.998");
    assertEquals("2009-04-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-04-05T23:59:59.999");
    assertEquals("2009-07-05T23:59:59.999", df.format(period.getNextDate(date)));

    date = df.parse("2009-04-06T00:00:00.000");
    assertEquals("2009-07-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-07-05T23:59:59.998");
    assertEquals("2009-07-05T23:59:59.999", df.format(period.getNextDate(date)));
    date = df.parse("2009-07-05T23:59:59.999");
    assertEquals("2009-10-05T23:59:59.999", df.format(period.getNextDate(date)));
}
 
Example #10
Source File: HandlerReportHelper.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
static NamedList<Object> buildTrackerReport(TrackerRegistry trackerRegistry, InformationServer srv, String coreName, Long fromTx, Long toTx, Long fromAclTx, Long toAclTx,
                                            Long fromTime, Long toTime) throws JSONException
{
    try
    {
        // ACL
        AclTracker aclTracker = trackerRegistry.getTrackerForCore(coreName, AclTracker.class);
        IndexHealthReport aclReport = aclTracker.checkIndex(toAclTx, fromTime, toTime);
        NamedList<Object> ihr = new SimpleOrderedMap<>();
        ihr.add("DB acl transaction count", aclReport.getDbAclTransactionCount());
        ihr.add("Count of duplicated acl transactions in the index", aclReport.getDuplicatedAclTxInIndex()
                .cardinality());
        if (aclReport.getDuplicatedAclTxInIndex().cardinality() > 0) {
            ihr.add("First duplicate acl tx", aclReport.getDuplicatedAclTxInIndex().nextSetBit(0L));
        }
        ihr.add("Count of acl transactions in the index but not the DB", aclReport.getAclTxInIndexButNotInDb()
                .cardinality());
        if (aclReport.getAclTxInIndexButNotInDb().cardinality() > 0) {
            ihr.add("First acl transaction in the index but not the DB", aclReport.getAclTxInIndexButNotInDb()
                    .nextSetBit(0L));
        }
        ihr.add("Count of missing acl transactions from the Index", aclReport.getMissingAclTxFromIndex()
                .cardinality());
        if (aclReport.getMissingAclTxFromIndex().cardinality() > 0) {
            ihr.add("First acl transaction missing from the Index", aclReport.getMissingAclTxFromIndex()
                    .nextSetBit(0L));
        }
        ihr.add("Index acl transaction count", aclReport.getAclTransactionDocsInIndex());
        ihr.add("Index unique acl transaction count", aclReport.getAclTransactionDocsInIndex());
        TrackerState aclState = aclTracker.getTrackerState();
        ihr.add("Last indexed change set commit time", aclState.getLastIndexedChangeSetCommitTime());
        Date lastChangeSetDate = new Date(aclState.getLastIndexedChangeSetCommitTime());
        ihr.add("Last indexed change set commit date", CachingDateFormat.getDateFormat().format(lastChangeSetDate));
        ihr.add("Last changeset id before holes", aclState.getLastIndexedChangeSetIdBeforeHoles());

        // Metadata
        MetadataTracker metadataTracker = trackerRegistry.getTrackerForCore(coreName, MetadataTracker.class);
        IndexHealthReport metaReport = metadataTracker.checkIndex(toTx, fromTime, toTime);
        ihr.add("DB transaction count", metaReport.getDbTransactionCount());
        ihr.add("Count of duplicated transactions in the index", metaReport.getDuplicatedTxInIndex()
                .cardinality());
        if (metaReport.getDuplicatedTxInIndex().cardinality() > 0) {
            ihr.add("First duplicate", metaReport.getDuplicatedTxInIndex().nextSetBit(0L));
        }
        ihr.add("Count of transactions in the index but not the DB", metaReport.getTxInIndexButNotInDb()
                .cardinality());
        if (metaReport.getTxInIndexButNotInDb().cardinality() > 0) {
            ihr.add("First transaction in the index but not the DB", metaReport.getTxInIndexButNotInDb()
                    .nextSetBit(0L));
        }
        ihr.add("Count of missing transactions from the Index", metaReport.getMissingTxFromIndex().cardinality());
        if (metaReport.getMissingTxFromIndex().cardinality() > 0) {
            ihr.add("First transaction missing from the Index", metaReport.getMissingTxFromIndex()
                    .nextSetBit(0L));
        }
        ihr.add("Index transaction count", metaReport.getTransactionDocsInIndex());
        ihr.add("Index unique transaction count", metaReport.getTransactionDocsInIndex());
        ihr.add("Index node count", metaReport.getLeafDocCountInIndex());
        ihr.add("Count of duplicate nodes in the index", metaReport.getDuplicatedLeafInIndex().cardinality());
        if (metaReport.getDuplicatedLeafInIndex().cardinality() > 0) {
            ihr.add("First duplicate node id in the index", metaReport.getDuplicatedLeafInIndex().nextSetBit(0L));
        }
        ihr.add("Index error count", metaReport.getErrorDocCountInIndex());
        ihr.add("Count of duplicate error docs in the index", metaReport.getDuplicatedErrorInIndex()
                .cardinality());
        if (metaReport.getDuplicatedErrorInIndex().cardinality() > 0) {
            ihr.add("First duplicate error in the index", SolrInformationServer.PREFIX_ERROR
                    + metaReport.getDuplicatedErrorInIndex().nextSetBit(0L));
        }
        ihr.add("Index unindexed count", metaReport.getUnindexedDocCountInIndex());
        ihr.add("Count of duplicate unindexed docs in the index", metaReport.getDuplicatedUnindexedInIndex()
                .cardinality());
        if (metaReport.getDuplicatedUnindexedInIndex().cardinality() > 0) {
            ihr.add("First duplicate unindexed in the index",
                    metaReport.getDuplicatedUnindexedInIndex().nextSetBit(0L));
        }
        TrackerState metaState = metadataTracker.getTrackerState();
        ihr.add("Last indexed transaction commit time", metaState.getLastIndexedTxCommitTime());
        Date lastTxDate = new Date(metaState.getLastIndexedTxCommitTime());
        ihr.add("Last indexed transaction commit date", CachingDateFormat.getDateFormat().format(lastTxDate));
        ihr.add("Last TX id before holes", metaState.getLastIndexedTxIdBeforeHoles());

        srv.addContentOutdatedAndUpdatedCounts(ihr);

        return ihr;
    }
    catch (Exception exception)
    {
        throw new AlfrescoRuntimeException("", exception);
    }
}
 
Example #11
Source File: DistributedDateAbstractSolrTrackerIT.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testDateMonth() throws Exception
{
    putHandleDefaults();

    int numAcls = 25;
    AclChangeSet bulkAclChangeSet = getAclChangeSet(numAcls);

    List<Acl> bulkAcls = new ArrayList<>();
    List<AclReaders> bulkAclReaders = new ArrayList<>();

    for (int i = 0; i < numAcls; i++)
    {
        Acl bulkAcl = getAcl(bulkAclChangeSet);
        bulkAcls.add(bulkAcl);
        bulkAclReaders.add(
                getAclReaders(bulkAclChangeSet,
                    bulkAcl,
                    singletonList("joel" + bulkAcl.getId()),
                    singletonList("phil" + bulkAcl.getId()),
                    null));
    }

    indexAclChangeSet(bulkAclChangeSet,
            bulkAcls,
            bulkAclReaders);

    int numNodes = 1000;
    List<Node> nodes = new ArrayList<>();
    List<NodeMetaData> nodeMetaDatas = new ArrayList<>();

    Transaction bigTxn = getTransaction(0, numNodes);

    Date[] dates = setupDates();

    int[] counts = new int[dates.length];

    for (int i = 0; i < numNodes; i++)
    {
        int aclIndex = i % numAcls;
        int dateIndex = i % dates.length;
        String dateString = DefaultTypeConverter.INSTANCE.convert(String.class, dates[dateIndex]);

        counts[dateIndex]++;

        Node node = getNode(bigTxn, bulkAcls.get(aclIndex), Node.SolrApiNodeStatus.UPDATED);
        node.setShardPropertyValue(dateString);
        nodes.add(node);
        NodeMetaData nodeMetaData = getNodeMetaData(node, bigTxn, bulkAcls.get(aclIndex), "mike", null, false);
        nodeMetaData.getProperties().put(ContentModel.PROP_CREATED, new StringPropertyValue(dateString));

        nodeMetaDatas.add(nodeMetaData);
    }

    indexTransaction(bigTxn, nodes, nodeMetaDatas);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), numNodes, 100000);

    Optional<QName> shardProperty = MetadataTracker.getShardProperty("created");
    assertTrue("'created' field is expected to be found in data model", shardProperty.isPresent());

    List<AlfrescoSolrDataModel.FieldInstance> fieldInstanceList =
            AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(shardProperty.get()).getFields();

    AlfrescoSolrDataModel.FieldInstance fieldInstance = fieldInstanceList.get(0);
    String fieldName = fieldInstance.getField();

    SimpleDateFormat formatter = CachingDateFormat.getSolrDatetimeFormatWithoutMsecs();
    for (int i = 0; i < dates.length; i++)
    {
        String startDate = formatter.format(dates[i]);

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(dates[i]);
        calendar.add(Calendar.SECOND, 1);
        String endDate = formatter.format(calendar.getTime());

        SolrQuery query =
                new SolrQuery(
                        "{!lucene}" +
                                escapeQueryChars(fieldName) +
                                ":[" + escapeQueryChars(startDate) + " TO " + escapeQueryChars(endDate) + " } " );
        assertCountAndColocation(query, counts[i]);
        assertCorrect(numNodes);
    }
}