/*
 * (C) Copyright Blackboard Inc. 2013 - All Rights Reserved
 * 
 * See the NOTICE file distributed with this work for additional information regarding copyright ownership. Blackboard
 * Inc. licenses this file to you 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:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * BLACKBOARD MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-
 * INFRINGEMENT. BLACKBOARD SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package blackboard.plugin.hayabusa.provider;

import static com.google.common.base.Preconditions.checkNotNull;

import blackboard.data.navigation.NavigationItem;
import blackboard.persist.PersistenceException;
import blackboard.persist.PersistenceRuntimeException;
import blackboard.persist.navigation.NavigationItemDbLoader;
import blackboard.plugin.hayabusa.command.Command;
import blackboard.plugin.hayabusa.command.SimpleCommand;

import java.util.*;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;

/**
 * A {@link Provider} for navigation item links.
 * 
 * @author Danny Thomas
 * @since 1.0
 */
public class NavigationItemProvider implements Provider
{
  private static final Function<NavigationItem, Command> TRANSFORM = new Function<NavigationItem, Command>()
    {
      public Command apply( NavigationItem ni )
      {
        checkNotNull( ni );
        return new SimpleCommand( ni.getLabel(), ni.getHref() );
      }
    };

  @Override
  public Iterable<Command> getCommands()
  {
    try
    {
      NavigationItemDbLoader loader = NavigationItemDbLoader.Default.getInstance();
      List<NavigationItem> items = loader.loadByFamily( "admin_main" );
      return Iterables.transform( items, TRANSFORM );
    }
    catch ( PersistenceException e )
    {
      throw new PersistenceRuntimeException( e );
    }
  }

}

@@ -1002,16 +1002,23 @@
 tionItem
+Control
 ;%0Aimport
@@ -1301,17 +1301,20 @@
 va.util.
-*
+List
 ;%0A%0Aimpor
@@ -1344,24 +1344,65 @@
 e.Function;%0A
+import com.google.common.base.Predicate;%0A
 import com.g
@@ -1422,24 +1422,30 @@
 collect.
+Fluent
 Iterable
 s;%0A%0A/**%0A
@@ -1436,17 +1436,16 @@
 Iterable
-s
 ;%0A%0A/**%0A 
@@ -1634,32 +1634,39 @@
 n%3CNavigationItem
+Control
 , Command%3E TRANS
@@ -1660,16 +1660,24 @@
 ommand%3E 
+COMMAND_
 TRANSFOR
@@ -1707,16 +1707,23 @@
 tionItem
+Control
 , Comman
@@ -1775,19 +1775,27 @@
 tionItem
+Control
  ni
+c
  )%0A     
@@ -1821,16 +1821,17 @@
 Null( ni
+c
  );%0A    
@@ -1862,16 +1862,17 @@
 mand( ni
+c
 .getLabe
@@ -1882,19 +1882,262 @@
 , ni
+c
 .get
-Href() 
+Url() );%0A      %7D%0A    %7D;%0A%0A  private static final Predicate%3CNavigationItemControl%3E PERMISSION_PREDICATE = new Predicate%3CNavigationItemControl%3E()%0A    %7B%0A      public boolean apply( NavigationItemControl nic )%0A      %7B%0A        return nic.getUserHasAccess(
 );%0A 
@@ -2331,16 +2331,23 @@
 tionItem
+Control
 %3E items 
@@ -2347,16 +2347,50 @@
  items =
+ NavigationItemControl.createList(
  loader.
@@ -2415,16 +2415,18 @@
 in_main%22
+ )
  );%0A    
@@ -2434,24 +2434,30 @@
  return 
+Fluent
 Iterable
 s.transf
@@ -2448,17 +2448,61 @@
 Iterable
-s
+.from( items ).filter( PERMISSION_PREDICATE )
 .transfo
@@ -2509,15 +2509,16 @@
 rm( 
-items, 
+COMMAND_
 TRAN

Change to NIC