Search in sources :

Example 1 with Reading

use of facebook4j.Reading in project camel by apache.

the class ReadingBuilder method copy.

public static Reading copy(Reading reading, boolean skipSinceUtil) throws NoSuchFieldException, IllegalAccessException {
    // use private field access to make a copy
    Field field = Reading.class.getDeclaredField("parameterMap");
    field.setAccessible(true);
    final LinkedHashMap<String, String> source = (LinkedHashMap<String, String>) field.get(reading);
    // create another reading, and add all fields from source
    Reading copy = new Reading();
    final LinkedHashMap<String, String> copyMap = new LinkedHashMap<String, String>();
    copyMap.putAll(source);
    if (skipSinceUtil) {
        copyMap.remove("since");
        copyMap.remove("until");
    }
    field.set(copy, copyMap);
    field.setAccessible(false);
    return copy;
}
Also used : Field(java.lang.reflect.Field) Reading(facebook4j.Reading) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Reading

use of facebook4j.Reading in project camel by apache.

the class ReadingBuilderTest method testCopy.

@Test
public void testCopy() throws Exception {
    final Reading source = new Reading();
    source.fields("field1", "field2");
    source.filter("testFilter");
    source.limit(100);
    source.locale(Locale.US);
    source.metadata();
    source.offset(1000);
    source.since(new Date());
    source.until(new Date());
    source.withLocation();
    Reading copy = ReadingBuilder.copy(source, false);
    assertNotNull("Null copy", copy);
    assertEquals("Copy not equal", source.toString(), copy.toString());
    // skip since and until
    copy = ReadingBuilder.copy(source, true);
    assertNotEquals("Copy equal", source.toString(), copy.toString());
    assertFalse("since", copy.toString().contains("since="));
    assertFalse("until", copy.toString().contains("until="));
}
Also used : Reading(facebook4j.Reading) Date(java.util.Date) Test(org.junit.Test)

Example 3 with Reading

use of facebook4j.Reading in project camel by apache.

the class ReadingBuilderTest method testSetProperties.

@Test
public void testSetProperties() throws Exception {
    final Reading reading = new Reading();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("fields", "field1,field2");
    properties.put("filter", "testFilter");
    properties.put("limit", "100");
    properties.put("metadata", "");
    properties.put("offset", "1000");
    final String facebookDate = new SimpleDateFormat(FacebookConstants.FACEBOOK_DATE_FORMAT).format(new Date());
    properties.put("since", facebookDate);
    properties.put("until", "arbitrary date, to be validated by Facebook call");
    properties.put("withLocation", "");
    // set properties on Reading
    ReadingBuilder.setProperties(reading, properties);
}
Also used : Reading(facebook4j.Reading) HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 4 with Reading

use of facebook4j.Reading in project camel by apache.

the class FacebookConsumer method getMethodArguments.

private Map<String, Object> getMethodArguments() {
    // start by setting the Reading since and until fields,
    // these are used to avoid reading duplicate results across polls
    Map<String, Object> arguments = new HashMap<String, Object>();
    arguments.putAll(endpointProperties);
    Reading reading = (Reading) arguments.remove(READING_PROPERTY);
    if (reading == null) {
        reading = new Reading();
    } else {
        try {
            reading = ReadingBuilder.copy(reading, true);
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException(String.format("Error creating property [%s]: %s", READING_PROPERTY, e.getMessage()), e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(String.format("Error creating property [%s]: %s", READING_PROPERTY, e.getMessage()), e);
        }
    }
    // now set since and until for this poll
    final SimpleDateFormat dateFormat = new SimpleDateFormat(FACEBOOK_DATE_FORMAT);
    final long currentMillis = System.currentTimeMillis();
    if (this.sinceTime == null) {
        // first poll, set this to (current time - initial poll delay)
        final Date startTime = new Date(currentMillis - TimeUnit.MILLISECONDS.convert(getInitialDelay(), getTimeUnit()));
        this.sinceTime = dateFormat.format(startTime);
    } else if (this.untilTime != null) {
        // use the last 'until' time
        this.sinceTime = this.untilTime;
    }
    this.untilTime = dateFormat.format(new Date(currentMillis));
    reading.since(this.sinceTime);
    reading.until(this.untilTime);
    arguments.put(READING_PROPERTY, reading);
    return arguments;
}
Also used : Reading(facebook4j.Reading) HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with Reading

use of facebook4j.Reading in project camel by apache.

the class FacebookPropertiesHelper method configureReadingProperties.

/**
     * Apply properties for {@link Reading} type to the supplied {@link FacebookEndpointConfiguration}.
     * @param configuration endpoint configuration to update
     * @param options properties to apply to the reading field in configuration
     */
public static void configureReadingProperties(FacebookEndpointConfiguration configuration, Map<String, Object> options) {
    final Map<String, Object> readingProperties = IntrospectionSupport.extractProperties(options, FacebookConstants.READING_PREFIX);
    if (!readingProperties.isEmpty()) {
        try {
            // add to an existing reading reference?
            // NOTE Reading class does not support overwriting properties!!!
            Reading reading = configuration.getReading();
            if (reading != null) {
                Reading readingUpdate = new Reading();
                ReadingBuilder.setProperties(readingUpdate, readingProperties);
                reading = ReadingBuilder.merge(reading, readingUpdate);
            } else {
                reading = new Reading();
                ReadingBuilder.setProperties(reading, readingProperties);
            }
            // set properties
            ReadingBuilder.setProperties(reading, readingProperties);
            // update reading in configuration
            configuration.setReading(reading);
        } catch (Exception e) {
            throw new IllegalArgumentException(readingProperties.toString(), e);
        }
        // add any unknown properties back to options to throw an error later
        for (Map.Entry<String, Object> entry : readingProperties.entrySet()) {
            options.put(FacebookConstants.READING_PREFIX + entry.getKey(), entry.getValue());
        }
    }
}
Also used : Reading(facebook4j.Reading) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Reading (facebook4j.Reading)6 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Field (java.lang.reflect.Field)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LinkedHashMap (java.util.LinkedHashMap)2 Test (org.junit.Test)2 Map (java.util.Map)1