2014-03-31 2 views
0

미러 API를 사용하여 타임 라인에 삽입 한 표지가있는 번들이 있습니다. 이제 내가 원하는 것은 사용자가 번들을 클릭하면 사용자 정의 메뉴를 클릭하여 백엔드를 호출하여 동일한 번들에 카드 세트를 다시 삽입하는 것입니다.번들에 카드를 삽입하기 위해 사용자 정의 메뉴에서 사용자 작업 감지

public class newsfeedbliss { 
    static String bundleId = "lunchRoulette" + UUID.randomUUID(); 
    private static ArrayList<String> newstext = new ArrayList<String>(); 

    static final String PROD_BASE_URL = "https://newsfeedbliss.appspot.com"; 
     private static final String PROD_CALLBACK = PROD_BASE_URL + "/newsfeedcallback"; 
     private static final String TEST_CALLBACK = "https://newsfeedbliss.appspot.com/newsfeedcallback"; 

    public static void subscribe(HttpServletRequest req, String userId) 
       throws IOException 
      { 
      Mirror mirror = MirrorUtils.getMirror(req); 

      // START:subscribe 

      final String callbackUrl = "https://newsfeedbliss.appspot.com/newsfeedcallback"; 
      Subscription tliSubscription = new Subscription() 
       .setCallbackUrl(callbackUrl) 
       .setVerifyToken("a_secret_to_everybody") 
       .setUserToken(userId) 
       .setCollection("timeline") 
       .setOperation(Collections.singletonList("UPDATE")); 

      mirror.subscriptions().insert(tliSubscription).execute(); 
      // END:subscribe 

      // TODO: check if this user has subscribed, skip if already has 
      SubscriptionsListResponse subscriptions = mirror.subscriptions().list().execute(); 
      for (Subscription sub : subscriptions.getItems()) { 
       System.out.println(sub); 
      } 
      } 



      public static TimelineItem buildarticlestimeline(
        ServletContext ctx, String userId) 
throws IOException, ServletException, ParserConfigurationException, SAXException 
{ 
       Mirror mirror = MirrorUtils.getMirror(userId); 
       Timeline timeline1 = mirror.timeline(); 
       TimelineItem timelineItem1 = new TimelineItem() 
       .setText("Hello"); 
       timeline1.insert(timelineItem1).executeAndDownloadTo(System.out); 

      return timelineItem1; 
      } 


    public static void insertSimpleTextTimelineItem(HttpServletRequest req) 
      throws IOException, ParserConfigurationException, SAXException 
      { 
      Mirror mirror = MirrorUtils.getMirror(req); 
      Timeline timeline = mirror.timeline(); 
      TimelineItem timelineItem = new TimelineItem() 
      .setHtml("<article>\n <section>\n <p class=\"text-auto-size\">This <em class=\"yellow\">paragraph</em> auto-resizes according to the <strong class=\"blue\">HTML</strong> content length.\n </p>\n </section>\n</article>\n") 
      .setBundleId(bundleId) 
      .setIsBundleCover(true); 
      setSimpleMenuItems(timelineItem,true); 
      timeline.insert(timelineItem).executeAndDownloadTo(System.out); 
      System.out.println("Hello hello"); 

      } 


    public static void setSimpleMenuItems(TimelineItem ti, boolean hasRestaurant) { 
     // Add blank menu list 
     ti.setMenuItems(new LinkedList<MenuItem>()); 
     ti.getMenuItems().add(new MenuItem().setAction("READ_ALOUD")); 
     ti.getMenuItems().add(new MenuItem().setAction("DELETE")); 
     List<MenuValue> menuValues = new ArrayList<MenuValue>(2); 
      menuValues.add(new MenuValue() 
      .setState("DEFAULT") 
      .setDisplayName("Alternative") 
      // .setIconUrl("") 
     ); 
      menuValues.add(new MenuValue() 
      .setState("PENDING") 
      .setDisplayName("Generating Alternative")); 

      ti.getMenuItems().add(new MenuItem() 
      .setAction("CUSTOM") 
       .setId("ALT") 
       .setPayload("ALT") 
       .setValues(menuValues) 
     ); 
     } 
} 

이 내 서블릿 파일

public class NewsfeedblissServlet extends HttpServlet 
{ 
    private static final Logger log = Logger.getLogger(NewsfeedblissServlet.class.getName()); 
/** Accept an HTTP GET request, and write a random lunch type. */ 

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
throws IOException, ServletException 
{ 

    log.info("in do get"); 
    try { 
     newsfeedbliss.insertSimpleTextTimelineItem(req); 
    } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    log.info("called insert text"); 
    resp.setContentType("text/html"); 
    resp.getWriter().append("Inserted Timeline Item"); 
} 
} 

입니다 그리고 이것은 내가 사용자 정의 메뉴를 클릭을 감지하고 카드를 삽입 콜백에서 실행하고자하는 코드가 그 쓴 클래스입니다.

public class TimelineUpdateServlet extends HttpServlet 
{ 
protected void doPost(HttpServletRequest req, HttpServletResponse res) 
throws IOException, ServletException 
{ 
    System.out.println("Hey, Hello"); 
    res.getWriter().append("Inside update servlet"); 
// Generate Notification from request body 
JsonFactory jsonFactory = new JacksonFactory(); 
Notification notification = 
jsonFactory.fromInputStream(req.getInputStream(), Notification.class); 
// Get this user action's type 
String userActionType = null; 
if(!notification.getUserActions().isEmpty()) 
userActionType = notification.getUserActions().get(0).getType(); 
//If this is a pinned timeline item, log who and which timeline item 

if("timeline".equals(notification.getCollection()) 
&& "UPDATE".equals(notification.getOperation()) 
&& "CUSTOM".equals(userActionType)) 
{ 
    UserAction userAction = notification.getUserActions().get(0); 
    if("ALT".equals(userAction.getPayload())) 
    { 
    // Add a new timeline item, and bundle it to the previous one 
    String userId = notification.getUserToken(); 
    String itemId = notification.getItemId(); 
    Mirror mirror = MirrorUtils.getMirror(userId); 
    Timeline timeline = mirror.timeline(); 
    // Get the timeline item that owns the tapped menu 
    TimelineItem current = timeline.get(itemId).execute(); 
    String bundleId = current.getBundleId(); 
    // If not a bundle, update this item as a bundle 
    if(bundleId == null) { 
    bundleId = "lunchRoulette" + UUID.randomUUID(); 
    current.setBundleId(bundleId); 
    timeline.update(itemId, current).execute(); 
    } 
    // Create a new random restaurant suggestion 
    TimelineItem newTi=null; 
    try { 
     newTi = newsfeedbliss.buildarticlestimeline(getServletContext(), userId); 
    } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    newTi.setBundleId(bundleId); 
    timeline.insert(newTi).execute(); 

} 
} 
} 
} 

답변

2

TimelineUpdateServlet 코드에서 원래 항목을 setIsBundleCover (true)로 수정해야합니다. 묶음 표지에는 하위 항목의 상위 역할 만하기 때문에 메뉴 항목이 없습니다. 새 카드가 제대로 번들에 표시 할 수 있도록해야한다

timeline.update(itemId, current).execute(); 

이를 호출하기 전에 따라서

current.setIsBundleCover(true); 

를 호출합니다.