/*
 * Copyright (C) 2013  Christian Autermann, Jan Alexander Wirwahn,
 *                     Arne De Wall, Dustin Demuth, Saqib Rasheed
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package io.car.server.rest.encoding;

import java.net.URI;

import javax.ws.rs.core.MediaType;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.inject.Inject;

import io.car.server.core.entities.Group;
import io.car.server.core.entities.User;
import io.car.server.rest.JSONConstants;
import io.car.server.rest.MediaTypes;
import io.car.server.rest.resources.GroupResource;

/**
 * @author Christian Autermann <autermann@uni-muenster.de>
 */
public class GroupEncoder extends AbstractEntityEncoder<Group> {
    private EntityEncoder<User> userEncoder;

    @Inject
    public GroupEncoder(EntityEncoder<User> userProvider) {
        this.userEncoder = userProvider;
    }

    @Override
    public ObjectNode encode(Group t, MediaType mediaType) {
        ObjectNode group = getJsonFactory().objectNode();
        if (t.hasName()) {
            group.put(JSONConstants.NAME_KEY, t.getName());
        }
        if (t.hasDescription()) {
            group.put(JSONConstants.DESCRIPTION_KEY, t.getDescription());
        }
        if (t.getOwner() != null) {
            group.put(JSONConstants.OWNER_KEY,
                      userEncoder.encode(t.getOwner(), mediaType));
        }
        if (mediaType.equals(MediaTypes.GROUP_TYPE)) {
            if (t.hasCreationTime()) {
                group.put(JSONConstants.CREATED_KEY,
                          getDateTimeFormat().print(t.getCreationTime()));
            }
            if (t.hasModificationTime()) {
                group.put(JSONConstants.MODIFIED_KEY,
                          getDateTimeFormat().print(t.getModificationTime()));
            }
            URI members = getUriInfo().getAbsolutePathBuilder()
                    .path(GroupResource.MEMBERS).build();
            group.put(JSONConstants.MEMBERS_KEY, members.toString());
            URI activities = getUriInfo().getAbsolutePathBuilder()
                    .path(GroupResource.ACTIVITIES).build();
            group.put(JSONConstants.ACTIVITIES_KEY, activities.toString());
        }
        return group;
    }
}

@@ -845,30 +845,8 @@
 g;%0A%0A
-import java.net.URI;%0A%0A
 impo
@@ -876,16 +876,16 @@
 iaType;%0A
+
 %0Aimport 
@@ -1630,24 +1630,55 @@
 (t.hasName()
+ && getRights().canSeeNameOf(t)
 ) %7B%0A        
@@ -1769,16 +1769,54 @@
 iption()
+ && getRights().canSeeDescriptionOf(t)
 ) %7B%0A    
@@ -1913,19 +1913,19 @@
  (t.
-get
+has
 Owner() 
 != n
@@ -1924,15 +1924,39 @@
 r() 
-!= null
+&& getRights().canSeeOwnerOf(t)
 ) %7B%0A
@@ -2162,32 +2162,71 @@
 asCreationTime()
+ && getRights().canSeeCreationTimeOf(t)
 ) %7B%0A            
@@ -2394,16 +2394,80 @@
 onTime()
+ && getRights()%0A                    .canSeeModificationTimeOf(t)
 ) %7B%0A    
@@ -2625,34 +2625,136 @@
     
-URI members = getUriInfo()
+if (getRights().canSeeMembersOf(t)) %7B%0A                group.put(JSONConstants.MEMBERS_KEY, getUriInfo()%0A                        
 .get
@@ -2767,32 +2767,36 @@
 tePathBuilder()%0A
+    
                 
@@ -2827,32 +2827,44 @@
 MEMBERS).build()
+.toString())
 ;%0A            gr
@@ -2865,107 +2865,156 @@
     
-group.put(JSONConstants.MEMBERS_KEY, members.toString());%0A            URI activities = getUriInfo()
+%7D%0A            if (getRights().canSeeActivitiesOf(t)) %7B%0A                group.put(JSONConstants.ACTIVITIES_KEY, getUriInfo()%0A                        
 .get
@@ -3027,32 +3027,36 @@
 tePathBuilder()%0A
+    
                 
@@ -3098,16 +3098,28 @@
 .build()
+.toString())
 ;%0A      
@@ -3128,71 +3128,9 @@
     
-group.put(JSONConstants.ACTIVITIES_KEY, activities.toString());
+%7D
 %0A   

enforce rights while encoding groups