public static Collection<ParseObject> toParseObjects(String classname, Collection<String> ids) {
   List<ParseObject> objects = new ArrayList<ParseObject>();
   if (ids != null) {
     for (String id : ids) {
       objects.add(ParseObject.createWithoutData(classname, id));
     }
   }
   return objects;
 }
 private void loadUsersWithSkills() {
   initializeUserRecyclerView();
   VolunteerUser.findUsersWithSkill(
       ParseObject.createWithoutData(Skill.class, mResourceObjectId),
       new FindCallback<VolunteerUser>() {
         @Override
         public void done(List<VolunteerUser> objects, ParseException e) {
           populateUserList(objects);
         }
       });
 }
 private void loadRecognizedUsers() {
   initializeUserRecyclerView();
   UserRecognition.findUsersWithRecognition(
       ParseObject.createWithoutData(Recognition.class, mResourceObjectId),
       new FindCallback<UserRecognition>() {
         @Override
         public void done(List<UserRecognition> objects, ParseException e) {
           if (objects.isEmpty()) {
             showEmptyMessage();
           } else {
             for (UserRecognition recognizedUsers : objects) {
               mResourceList.add(
                   VolunteerUser.convertToResourceModel(recognizedUsers.getAchievedBy()));
             }
           }
           mUserResourceAdapter.notifyDataSetChanged();
         }
       });
 }
Example #4
0
 public void setUserId(String userId) {
   put("userId", ParseObject.createWithoutData("_User", userId));
 }
  public void onSubmitClick(View v) {
    // Validate and Save the resource
    mProgressBar.setVisibility(View.VISIBLE);
    String errorMessage = validateResource();

    if (!Strings.isNullOrEmpty(errorMessage)) {
      showError(errorMessage);
      mProgressBar.setVisibility(View.GONE);
      return;
    }

    mSubmitButton.setEnabled(false);
    switch (mResourceType) {
      case Constants.ANNOUNCEMENT_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new Announcement()
                : ParseObject.createWithoutData(Announcement.class, mResourceObjectId);
        ((Announcement) mResourceObject).setAnnouncementText(mDescriptionText.getText().toString());
        ((Announcement) mResourceObject).setIsActive(mActiveStatus.isChecked());
        Announcement.saveAnnouncement((Announcement) mResourceObject, onResourceSave());
        break;

      case Constants.CATEGORY_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new Category()
                : ParseObject.createWithoutData(Category.class, mResourceObjectId);
        ((Category) mResourceObject).setCategoryName(mTitleText.getText().toString());
        ((Category) mResourceObject).setDescription(mDescriptionText.getText().toString());
        ((Category) mResourceObject).setCategoryColor(mColorHexText.getText().toString());
        ((Category) mResourceObject).setIsActive(mActiveStatus.isChecked());

        try {
          Category.saveCategory((Category) mResourceObject, onResourceSave());
        } catch (ParseException ex) {

        }

        break;

      case Constants.RECOGNITION_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new Recognition()
                : ParseObject.createWithoutData(Recognition.class, mResourceObjectId);
        ((Recognition) mResourceObject).setTitle(mTitleText.getText().toString());
        ((Recognition) mResourceObject).setDescription(mDescriptionText.getText().toString());
        ((Recognition) mResourceObject)
            .setPointsRequired(Integer.parseInt(mRecogReqPtsText.getText().toString()));
        ((Recognition) mResourceObject)
            .setMaxUnits(Integer.parseInt(mRecogMaxUnitsText.getText().toString()));
        ((Recognition) mResourceObject).setIsHoursBased(mRecognitionUnit.isChecked());
        ((Recognition) mResourceObject)
            .setRecognitionType(String.valueOf(mRecognitionTypeSpinner.getSelectedItem()));
        // TODO: Set logo
        ////// ((Recognition)mResourceObject).getLogo();
        ((Recognition) mResourceObject).setIsActive(mActiveStatus.isChecked());

        Recognition.saveRecognition((Recognition) mResourceObject, onResourceSave());
        break;

      case Constants.INTEREST_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new Interest()
                : ParseObject.createWithoutData(Interest.class, mResourceObjectId);
        ((Interest) mResourceObject).setInterestTitle(mTitleText.getText().toString());
        ((Interest) mResourceObject).setDescription(mDescriptionText.getText().toString());
        // TODO: Set logo
        ////// ((Interest)mResourceObject).getLogo();
        ((Interest) mResourceObject).setIsActive(mActiveStatus.isChecked());

        Interest.saveInterest((Interest) mResourceObject, onResourceSave());
        break;

      case Constants.SKILL_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new Skill()
                : ParseObject.createWithoutData(Skill.class, mResourceObjectId);
        ((Skill) mResourceObject).setSkillTitle(mTitleText.getText().toString());
        ((Skill) mResourceObject).setDescription(mDescriptionText.getText().toString());
        // TODO: Set logo
        // ((Skill)mResourceObject).setLogo();
        ((Skill) mResourceObject).setIsActive(mActiveStatus.isChecked());

        Skill.saveSkill((Skill) mResourceObject, onResourceSave());
        break;

      case Constants.USER_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new SpecialUser()
                : ParseObject.createWithoutData(SpecialUser.class, mResourceObjectId);
        ((SpecialUser) mResourceObject).setEmailId(mTitleText.getText().toString());
        ((SpecialUser) mResourceObject).setRole(String.valueOf(mUserRoleSpinner.getSelectedItem()));
        ((SpecialUser) mResourceObject)
            .setOrganizationName(String.valueOf(mUserOrgSpinner.getSelectedItem()));
        ((SpecialUser) mResourceObject).setIsActive(mActiveStatus.isChecked());
        SpecialUser.saveSpecialUser((SpecialUser) mResourceObject, onResourceSave());
        break;

      case Constants.SCHOOL_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new School()
                : ParseObject.createWithoutData(School.class, mResourceObjectId);
        ((School) mResourceObject).setSchoolName(mTitleText.getText().toString());
        ((School) mResourceObject).setDescription(mDescriptionText.getText().toString());
        // TODO: Set logo
        // ((School)mResourceObject).setLogo();
        ((School) mResourceObject).setIsActive(mActiveStatus.isChecked());
        School.saveSchool((School) mResourceObject, onResourceSave());
        break;

      case Constants.GROUP_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new InterestGroup()
                : ParseObject.createWithoutData(InterestGroup.class, mResourceObjectId);
        ((InterestGroup) mResourceObject).setGroupName(mTitleText.getText().toString());
        ((InterestGroup) mResourceObject)
            .setGroupDescription(mDescriptionText.getText().toString());
        // TODO: Set logo
        // ((InterestGroup)mResourceObject).setLogo();
        ((InterestGroup) mResourceObject).setIsActive(mActiveStatus.isChecked());
        InterestGroup.saveGroup((InterestGroup) mResourceObject, onResourceSave());
        // TODO: Add members
        break;

      case Constants.ORGANIZATION_RESOURCE:
        mResourceObject =
            Strings.isNullOrEmpty(mResourceObjectId)
                ? new Organization()
                : ParseObject.createWithoutData(Organization.class, mResourceObjectId);
        ((Organization) mResourceObject).setOrganizationName(mTitleText.getText().toString());
        ((Organization) mResourceObject).setDescription(mDescriptionText.getText().toString());
        // TODO: Set logo
        // ((Organization)mResourceObject).setLogo();
        ((Organization) mResourceObject).setIsActive(mActiveStatus.isChecked());

        Organization.saveOrganization((Organization) mResourceObject, onResourceSave());
        break;

      default:
        mResourceObject.saveInBackground(
            new SaveCallback() {
              @Override
              public void done(ParseException e) {
                Snackbar snackbar =
                    Snackbar.make(
                        mSaveResourceForm, R.string.msg_data_success, Snackbar.LENGTH_LONG);
                snackbar.show();
              }
            });
    }
    mProgressBar.setVisibility(View.GONE);
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mContentView = inflater.inflate(R.layout.fragment_read_reply, container, false);

    post = ParseObject.createWithoutData(Post.class, postId);
    post.fetchFromLocalDatastoreInBackground(
        new GetCallback<ParseObject>() {
          @Override
          public void done(ParseObject parseObject, ParseException e) {
            Log.e("", "post fetch from local done");
            if (e == null) {
              refresh("");
              if (adapter != null) {
                adapter.setPostId(post.getObjectId());
              } else if (data != null) {
                adapter = new ReplyAdapter(getActivity(), data);
                adapter.setPostId(post.getObjectId());
              } else {
                data = new ArrayList<>();
                adapter = new ReplyAdapter(getActivity(), data);
                adapter.setPostId(post.getObjectId());
              }
            }
          }
        });

    data = new ArrayList<>();
    adapter = new ReplyAdapter(getActivity(), data);
    holder = new ViewHolder();
    holder.lv_replies = (ListView) mContentView.findViewById(R.id.replies);
    holder.lv_replies.setAdapter(adapter);
    setListViewHeightBasedOnChildren(holder.lv_replies);

    LinearLayout ll = new LinearLayout(getActivity());
    LinearLayout.LayoutParams lp =
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            Gravity.CENTER);
    lp.setMargins(
        0, Util.convertDpToPixel(10, getActivity()), 0, Util.convertDpToPixel(10, getActivity()));
    ll.setLayoutParams(lp);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    TextView tv = new TextView(getActivity());
    tv.setLayoutParams(
        new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    tv.setMinimumHeight(Util.convertDpToPixel(120, getActivity()));
    tv.setHint("  There is no reply!");
    tv.setGravity(Gravity.CENTER);

    ImageView imageView = new ImageView(getActivity());
    LinearLayout.LayoutParams lp1 =
        new LinearLayout.LayoutParams(
            Util.convertDpToPixel(135, getActivity()), Util.convertDpToPixel(100, getActivity()));
    imageView.setLayoutParams(lp1);
    imageView.setBackgroundResource(R.drawable.empty_box);
    imageView.setAdjustViewBounds(true);
    ll.addView(imageView);
    ll.addView(tv);

    holder.empty_Overlay = (FrameLayout) mContentView.findViewById(R.id.empty_layout);
    holder.empty_Overlay.addView(
        ll,
        new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            Gravity.CENTER));
    holder.empty_Overlay.setVisibility(View.INVISIBLE);
    holder.lv_replies.setEmptyView(holder.empty_Overlay);
    //        holder.lv_replies.setOnItemClickListener(new OnItemClickListener() {
    //
    //            @Override
    //            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    //                                    long arg3) {
    //
    //            }
    //        });

    return mContentView;
  }
Example #7
0
  public <E extends SyncEntity> void synObjects(
      final Class<E> entityClass, boolean userAware, final SyncCallback<E> callback)
      throws SyncException {
    try {
      final String parseClass = extractParseClass(entityClass);

      // get updated remote objects
      ParseQuery query = ParseQuery.getQuery(parseClass);
      query.whereGreaterThan(PARSE_UPDATED_AT_FIELD, lastSyncDate);
      if (userAware && userId != null) query.whereEqualTo(PARSE_USER_ID_FIELD, userId);
      // query.orderByAscending(PARSE_OBJECT_ID_FIELD);
      final List<ParseObject> remoteObjects = ParseTools.findAllParseObjects(query);

      // get updated local objects
      final SyncDAO<E, ?> syncDao = dbHelper.getDao(entityClass);
      QueryBuilder<E, ?> dbQuery = syncDao.queryBuilder();
      // dbQuery.orderBy(DB_OBJECT_ID_FIELD, true);
      Where<E, ?> where = dbQuery.where().gt(DB_UPDATED_AT_FIELD, lastSyncDate);
      if (userAware && userId != null) where.and().eq(DB_USER_ID_FIELD, userId);
      final List<E> localObjects = where.query();

      // create local object map
      Map<String, E> localObjectMap = new HashMap<String, E>(localObjects.size());
      for (E localObject : localObjects) {
        localObjectMap.put(localObject.getSyncId(), localObject);
      }

      List<Pair<E, ParseObject>> toSaveLocally = new ArrayList<>();
      List<Pair<E, ParseObject>> toSaveRemotely = new ArrayList<>();

      for (ParseObject remoteObject : remoteObjects) {
        String syncId = remoteObject.getObjectId();
        E localObject = localObjectMap.get(syncId);

        if (localObject == null) {
          localObject = findBySyncId(syncDao, syncId);
          if (localObject == null) {
            // this object was created on the server but doesn't exist locally
            localObject = SyncEntity.fromParseObject(remoteObject, entityClass);
          } else {
            // the object exists locally but out-of-date
            SyncEntity.fromParseObject(remoteObject, localObject);
          }
          toSaveLocally.add(new Pair<>(localObject, remoteObject));
          continue;
        }

        if (localObject != null) {
          long localTime =
              (localObject.getSyncDate() == null) ? 0L : localObject.getSyncDate().getTime();
          long remoteTime =
              (remoteObject.getUpdatedAt() == null) ? 0L : remoteObject.getUpdatedAt().getTime();

          if (remoteTime > localTime) {
            // the remote object is newer
            SyncEntity.fromParseObject(remoteObject, localObject);
            toSaveLocally.add(new Pair<>(localObject, remoteObject));
          } else if (remoteTime < localTime) {
            // the local objects is newer
            SyncEntity.toParseObject(localObject, remoteObject);
            toSaveRemotely.add(new Pair<>(localObject, remoteObject));
          }
        }
      }
      localObjectMap = null;

      // create remote object map
      Map<String, ParseObject> remoteObjectMap =
          new HashMap<String, ParseObject>(remoteObjects.size());
      for (ParseObject remoteObject : remoteObjects) {
        remoteObjectMap.put(remoteObject.getObjectId(), remoteObject);
      }

      for (E localObject : localObjects) {
        String syncId = localObject.getSyncId();
        if (syncId == null) {
          // a brand new object!
          ParseObject remoteObject = SyncEntity.toParseObject(localObject);
          toSaveRemotely.add(new Pair<>(localObject, remoteObject));
          continue;
        }

        ParseObject remoteObject = remoteObjectMap.get(syncId);
        if (remoteObject == null && !localObject.isDeleted()) {
          // object was created locally but doesn't exist or too old on the server
          // this is weird because syncId is not null

          // try to get it from server
          remoteObject = ParseObject.createWithoutData(parseClass, syncId).fetch();
          toSaveRemotely.add(new Pair<>(localObject, remoteObject));
          continue;
        }

        if (remoteObject != null) {
          long localTime =
              (localObject.getSyncDate() == null) ? 0L : localObject.getSyncDate().getTime();
          long remoteTime =
              (remoteObject.getUpdatedAt() == null) ? 0L : remoteObject.getUpdatedAt().getTime();

          if (remoteTime > localTime) {
            // the remote object is newer
            SyncEntity.fromParseObject(remoteObject, localObject);
            toSaveLocally.add(new Pair<>(localObject, remoteObject));
          } else if (remoteTime < localTime) {
            // the local objects is newer
            SyncEntity.toParseObject(localObject, remoteObject);
            toSaveRemotely.add(new Pair<>(localObject, remoteObject));
          }
        }
      }

      if (callback != null) {
        callback.beforeSync(toSaveLocally, toSaveRemotely);
      }

      for (Pair<E, ParseObject> p : toSaveLocally) {
        final E localObject = p.first;
        final ParseObject remoteObject = p.second;
        TransactionManager.callInTransaction(
            dbHelper.getConnectionSource(),
            new Callable<Object>() {
              @Override
              public Object call() throws Exception {
                if (callback != null) {
                  callback.onSaveLocally(localObject, remoteObject);
                }
                syncDao.createOrUpdate(localObject);
                return null;
              }
            });
      }

      for (Pair<E, ParseObject> p : toSaveRemotely) {
        final E localObject = p.first;
        final ParseObject remoteObject = p.second;
        TransactionManager.callInTransaction(
            dbHelper.getConnectionSource(),
            new Callable<Object>() {
              @Override
              public Object call() throws Exception {
                if (callback != null) {
                  callback.onSaveRemotely(localObject, remoteObject);
                }
                remoteObject.save();
                if (localObject.getSyncId() == null) {
                  SyncEntity.fromParseObject(remoteObject, localObject);
                  syncDao.createOrUpdate(localObject);
                }
                return null;
              }
            });
      }

      if (callback != null) {
        callback.afterSync();
      }
    } catch (Exception ex) {
      throw new SyncException("Synchronization failed", ex);
    }
  }
Example #8
0
 public T wrap(String objectId) {
   ParseObject po = ParseObject.createWithoutData(clazz.getSimpleName(), objectId);
   return wrap(po);
 }