diff options
| author | Jesse Morgan <jesse@jesterpm.net> | 2016-04-09 09:51:01 -0700 | 
|---|---|---|
| committer | Jesse Morgan <jesse@jesterpm.net> | 2016-04-09 09:51:01 -0700 | 
| commit | 9d7cd517e8a00049357ce6ec4b65cb5a12dc2f24 (patch) | |
| tree | 0d7e0f421f5b75b043106e519ed7b3319b9cb67f /src/com | |
| parent | 10c5fd17b603f125ae2c0ef14b1a65341dbdf961 (diff) | |
Implementing CCBProgressReporter
This reporter updates two pairs of user-defined fields on an Individual
Profile. The first pair are pulldown & date fields used to track the
assessment results. The second pair are updated after each chapter is
completed.
Diffstat (limited to 'src/com')
| -rw-r--r-- | src/com/p4square/grow/ccb/CCBProgressReporter.java | 82 | ||||
| -rw-r--r-- | src/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java | 3 | ||||
| -rw-r--r-- | src/com/p4square/grow/model/Score.java | 2 | 
3 files changed, 81 insertions, 6 deletions
diff --git a/src/com/p4square/grow/ccb/CCBProgressReporter.java b/src/com/p4square/grow/ccb/CCBProgressReporter.java index e2304fe..d2826eb 100644 --- a/src/com/p4square/grow/ccb/CCBProgressReporter.java +++ b/src/com/p4square/grow/ccb/CCBProgressReporter.java @@ -1,9 +1,15 @@  package com.p4square.grow.ccb;  import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.model.*;  import com.p4square.grow.frontend.ProgressReporter; +import com.p4square.grow.model.Score; +import org.apache.log4j.Logger;  import org.restlet.security.User; +import java.io.IOException; +import java.time.LocalDate; +import java.time.ZoneId;  import java.util.Date;  /** @@ -14,19 +20,85 @@ import java.util.Date;   */  public class CCBProgressReporter implements ProgressReporter { +    private static final Logger LOG = Logger.getLogger(CCBProgressReporter.class); + +    private static final String GROW_LEVEL = "GrowLevelTrain"; +    private static final String GROW_ASSESSMENT = "GrowLevelAsmnt"; +      private final CCBAPI mAPI; +    private final CustomFieldCache mCache; -    public CCBProgressReporter(final CCBAPI api) { +    public CCBProgressReporter(final CCBAPI api, final CustomFieldCache cache) {          mAPI = api; +        mCache = cache;      }      @Override -    public void reportAssessmentComplete(User user, String level, Date date, String results) { -        // TODO +    public void reportAssessmentComplete(final User user, final String level, final Date date, final String results) { +        if (!(user instanceof CCBUser)) { +            throw new IllegalArgumentException("Expected CCBUser but got " + user.getClass().getCanonicalName()); +        } +        final CCBUser ccbuser = (CCBUser) user; + +        updateLevelAndDate(ccbuser, GROW_ASSESSMENT, level, date);      }      @Override -    public void reportChapterComplete(User user, String chapter, Date date) { -        // TODO +    public void reportChapterComplete(final User user, final String chapter, final Date date) { +        if (!(user instanceof CCBUser)) { +            throw new IllegalArgumentException("Expected CCBUser but got " + user.getClass().getCanonicalName()); +        } +        final CCBUser ccbuser = (CCBUser) user; + +        // Only update the level if it is increasing. +        final CustomPulldownFieldValue currentLevel = ccbuser.getProfile() +                .getCustomPulldownFields().getByLabel(GROW_LEVEL); + +        if (currentLevel != null) { +            if (Score.numericScore(chapter) <= Score.numericScore(currentLevel.getSelection().getLabel())) { +                LOG.info("Not updating level for " + user.getIdentifier() +                        + " because current level (" + currentLevel.getSelection().getLabel() +                        + ") is greater than new level (" + chapter + ")"); +                return; +            } +        } + +        updateLevelAndDate(ccbuser, GROW_LEVEL, chapter, date); +    } + +    private void updateLevelAndDate(final CCBUser user, final String field, final String level, final Date date) { +        boolean modified = false; + +        final UpdateIndividualProfileRequest req = new UpdateIndividualProfileRequest() +                .withIndividualId(user.getProfile().getId()); + +        final CustomField pulldownField = mCache.getIndividualPulldownByLabel(field); +        if (pulldownField != null) { +            final LookupTableType type = LookupTableType.valueOf(pulldownField.getName().toUpperCase()); +            final LookupTableItem item = mCache.getPulldownItemByName(type, level); +            if (item != null) { +                req.withCustomPulldownField(pulldownField.getName(), item.getId()); +                modified = true; +            } +        } + +        final CustomField dateField = mCache.getDateFieldByLabel(field); +        if (dateField != null) { +            req.withCustomDateField(dateField.getName(), date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()); +            modified = true; +        } + +        try { +            // Only update if a field exists. +            if (modified) { +                mAPI.updateIndividualProfile(req); +            } + +        } catch (IOException e) { +            LOG.error("updateIndividual failed for " + user.getIdentifier() +                    + ", field " + field +                    + ", level " + level +                    + ", date " + date.toString()); +        }      }  } diff --git a/src/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java b/src/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java index 48d143c..fc6148f 100644 --- a/src/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java +++ b/src/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java @@ -41,7 +41,8 @@ public class ChurchCommunityBuilderIntegrationDriver implements IntegrationDrive              mAPI = api; -            mProgressReporter = new CCBProgressReporter(mAPI); +            final CustomFieldCache cache = new CustomFieldCache(mAPI); +            mProgressReporter = new CCBProgressReporter(mAPI, cache);          } catch (URISyntaxException e) {              throw new RuntimeException(e); diff --git a/src/com/p4square/grow/model/Score.java b/src/com/p4square/grow/model/Score.java index 82f26c9..031c309 100644 --- a/src/com/p4square/grow/model/Score.java +++ b/src/com/p4square/grow/model/Score.java @@ -17,6 +17,8 @@ public class Score {       *  numericScore(x.toString()) <= x.getScore()       */      public static double numericScore(String score) { +        score = score.toLowerCase(); +          if ("teacher".equals(score)) {              return 3.5;          } else if ("disciple".equals(score)) {  | 
