/*
 * Copyright (c) 2013 Lucas Holt
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

package com.justjournal.ctl.api;

import com.justjournal.model.Journal;
import com.justjournal.model.PrefBool;
import com.justjournal.model.User;
import com.justjournal.repository.UserRepository;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Lucas Holt
 */
@Controller
@RequestMapping("/api/members")
public class MembersController {
    private static final Logger log = Logger.getLogger(MembersController.class);

    @Autowired
    private UserRepository userRepository;

    /**
     * All moods usable by blogs
     *
     * @return mood list
     */
    @Transactional
    @Cacheable("members")
    @RequestMapping(method = RequestMethod.GET, headers = "Accept=*/*", produces = "application/json")
    public
    @ResponseBody
    Collection<User> list() {
        if ((userRepository == null)) throw new AssertionError();

        Collection<User> publicMembers = new ArrayList<User>();

        try {
            Iterable<User> members = userRepository.findAll();

            if ((members == null)) throw new AssertionError();

            for (User member : members) {
                boolean stealth = false;
                for (Journal journal : member.getJournals()) {
                   if (journal.isOwnerViewOnly()) {
                       stealth = true;
                       break;
                   }
                }
                if (!stealth) {
                    publicMembers.add(member);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return publicMembers;
    }
}

@@ -1421,47 +1421,8 @@
 al;%0A
-import com.justjournal.model.PrefBool;%0A
 impo
@@ -1513,31 +1513,33 @@
 ort 
-org.apache.log4j.Logger
+lombok.extern.slf4j.Slf4j
 ;%0Aim
@@ -1685,29 +1685,70 @@
 ork.
-stereotype.Controller
+http.MediaType;%0Aimport org.springframework.http.ResponseEntity
 ;%0Aim
@@ -2000,102 +2000,71 @@
 dy;%0A
-%0A
 import 
-javax.persistence.PersistenceContext;%0Aimport javax.persistence.PersistenceContextType;
+org.springframework.web.bind.annotation.RestController;%0A
 %0Aimp
@@ -2149,16 +2149,27 @@
 lt%0A */%0A@
+Slf4j%0A@Rest
 Controll
@@ -2236,16 +2236,17 @@
 oller %7B%0A
+%0A
     priv
@@ -2253,106 +2253,143 @@
 ate 
-static final Logger log = Logger.getLogger(MembersController.class);%0A%0A    @Autowired%0A    private U
+final UserRepository userRepository;%0A%0A    @Autowired%0A    public MembersController(final UserRepository userRepository) %7B%0A        this.u
 serR
@@ -2389,32 +2389,34 @@
 s.userRepository
+ =
  userRepository;
@@ -2411,24 +2411,30 @@
 rRepository;
+%0A    %7D
 %0A%0A    /**%0A  
@@ -2636,27 +2636,59 @@
 s = 
-%22application/json%22)
+MediaType.APPLICATION_JSON_VALUE)%0A    @ResponseBody
 %0A   
@@ -2694,30 +2694,25 @@
   public
-%0A    @
+ 
 Response
 Body%0A   
@@ -2703,25 +2703,23 @@
 Response
-Body%0A    
+Entity%3C
 Collecti
@@ -2726,16 +2726,17 @@
 on%3CUser%3E
+%3E
  list() 
@@ -2737,24 +2737,25 @@
 ist() %7B%0A
+%0A
         
 if ((use
@@ -2750,74 +2750,13 @@
     
-if ((userRepository == null)) throw new AssertionError();%0A%0A       
+final
  Col
@@ -2835,16 +2835,22 @@
         
+final 
 Iterable
@@ -2909,77 +2909,19 @@
     
-if ((members == null)) throw new AssertionError();%0A%0A            for (
+for (final 
 User
@@ -3003,16 +3003,22 @@
    for (
+final 
 Journal 
@@ -3047,24 +3047,25 @@
 urnals()) %7B%0A
+ 
             
@@ -3127,16 +3127,17 @@
         
+ 
 stealth 
@@ -3171,15 +3171,17 @@
     
+ 
 break;%0A
+ 
     
@@ -3343,16 +3343,22 @@
  catch (
+final 
 Exceptio
@@ -3400,16 +3400,19 @@
 essage()
+, e
 );%0A     
@@ -3408,32 +3408,33 @@
 , e);%0A        %7D%0A
+%0A
         return p
@@ -3432,29 +3432,104 @@
  return 
-publicMembers
+ResponseEntity.ok().eTag(Integer.toString(publicMembers.hashCode())).body(publicMembers)
 ;%0A    %7D%0A

use etags