// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps.servlets;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.io.*;



//Class to check if a user follows a tag.
//Includes functionality to get followed tags from datastore.
public final class LoadFollowedTags {
  public static final String TAG_QUERY = "followedTag";
  public static List<String> getFollowedTags (String email) {
    List<String> followedTags = new ArrayList<String>();
    PreparedQuery results = getTagsFromDatastore(email);

    for (Entity entity : results.asIterable()) {
      String tag = (String) entity.getProperty("tag");
      followedTags.add(tag);
    }
    return followedTags;
  }

  public static Boolean hasFollowedTags (String email) {
    return !getFollowedTags(email).isEmpty();
  }

  private static PreparedQuery getTagsFromDatastore (String email) {
    Filter tagFilter = new FilterPredicate("email", FilterOperator.EQUAL, email);
    Query query = new Query(TAG_QUERY).setFilter(tagFilter);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    PreparedQuery results = datastore.prepare(query);
    return results;
  }
}
@@ -1210,12 +1210,11 @@
 *;%0A%0A
-%0A%0A
 //
+ 
 Clas
@@ -1251,16 +1251,17 @@
  tag.%0A//
+ 
 Includes
@@ -1350,16 +1350,17 @@
 dTags %7B%0A
+%0A
   public
@@ -1371,74 +1371,23 @@
 tic 
-final String TAG_QUERY = %22followedTag%22;%0A  public static List%3CStrin
+List%3CFollowedTa
 g%3E g
@@ -1392,33 +1392,32 @@
  getFollowedTags
- 
 (String email) %7B
@@ -1426,21 +1426,26 @@
    List%3C
-Strin
+FollowedTa
 g%3E follo
@@ -1468,21 +1468,26 @@
 rayList%3C
-Strin
+FollowedTa
 g%3E();%0A  
@@ -1587,24 +1587,68 @@
 erable()) %7B%0A
+      long tagId = entity.getKey().getId();%0A
       String
@@ -1690,16 +1690,77 @@
 %22tag%22);%0A
+      FollowedTag followedTag = new FollowedTag(tag, tagId);%0A
       fo
@@ -1774,17 +1774,25 @@
 ags.add(
-t
+followedT
 ag);%0A   
@@ -1863,17 +1863,16 @@
 owedTags
- 
 (String 
@@ -1982,17 +1982,16 @@
 atastore
- 
 (String 
@@ -2109,16 +2109,30 @@
 w Query(
+BlogConstants.
 TAG_QUER
@@ -2315,8 +2315,27 @@
 s;%0A  %7D%0A%7D
+%0A/* End Of File */%0A

Update LoadFollowedTags