jan.vdbergh
jan.vdbergh

Reputation: 2119

How to find content older than a specific number of days in Alfresco

I'm developing a number of scheduled tasks in Alfresco. One of these tasks should only run on nodes where a certain date property is older than a fixed number of days. I've looked into range queries: date:[MIN TO NOW], but I don't want to use NOW, but a date that is x days in the past.

Can this be specified in the query?

Upvotes: 0

Views: 1018

Answers (2)

jan.vdbergh
jan.vdbergh

Reputation: 2119

Scheduled tasks are defined in a Spring configuration file. A CronScheduledQueryBasedTemplateActionDefinition performs a query and executes an action on every result. The main problem was adding a dynamic date range to the query.

However, it is not actually a query, but a query template, which is processed with FreeMarker. The model used to process the template is created by another bean, the TemplateActionModelFactory.

Our solution was to subclass this factory to provide a richer model to the template: a function was added to calculate the dynamic date range:

public class CustomTemplateActionModelFactory extends FreeMarkerWithLuceneExtensionsModelFactory {

    /**
      * Function to calculate a date range based on the number of days in the past.
      */
    private static class LuceneDateRangeOlderThanDays implements TemplateMethodModelEx {

        @SuppressWarnings("rawtypes")
        public Object exec(List args) throws TemplateModelException {
            if (args.size() == 1) {
                int days = ((TemplateNumberModel) args.get(0)).getAsNumber().intValue();
                Date date = new Date(System.currentTimeMillis() - days * 24L * 3600 * 1000);

                StringBuilder builder = new StringBuilder();
                builder.append("[2001-01-01 TO ");
                builder.append(DefaultTypeConverter.INSTANCE.convert(String.class, date));
                builder.append("]");

                return builder.toString();
            } else {
                throw new TemplateModelException("Invalid parameters.");
            }
        }
    }

    @Override
    public Map<String, Object> getModel() {
        // Extend the parent model with our custom function
        Map<String, Object> model = super.getModel();
        model.put("luceneDateRangeOlderThanDays", new LuceneDateRangeOlderThanDays());
        return model;
    }
}

In the Spring configuration of the Alfresco repository, define your own TemplateActionModelFactory and use it in the rest of your configuration.

<bean id="customTemplateActionModelFactory" class="com.db.cms.repository.util.DBTemplateActionModelFactory">
    <property name="serviceRegistry">
        <ref bean="ServiceRegistry" />
    </property>
</bean>

Upvotes: 1

Tahir Malik
Tahir Malik

Reputation: 6643

Hi yeah that's possible quite easily. Let the task trigger a Javascript.

Just do some Javascript:

 var days = 7;
 var time = date.getTime();
 var add = 86400000 * days;
 var newDate = new Date(time - add);

 var dateString = utils.toISO8601(newDate);

Now use the dateString into your query, e.g. "date:[MIN - " + dateString + "]"

Upvotes: 2

Related Questions