content: /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF 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
   *
   * 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 org.apache.ambari.server.controller.internal;
  
  import java.util.Collections;
  import java.util.EnumSet;
  import java.util.HashSet;
  import java.util.Map;
  import java.util.Set;
  
  import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
  import org.apache.ambari.server.controller.spi.NoSuchResourceException;
  import org.apache.ambari.server.controller.spi.Predicate;
  import org.apache.ambari.server.controller.spi.Request;
  import org.apache.ambari.server.controller.spi.RequestStatus;
  import org.apache.ambari.server.controller.spi.Resource;
  import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
  import org.apache.ambari.server.controller.spi.SystemException;
  import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
  import org.apache.ambari.server.controller.utilities.PropertyHelper;
  import org.apache.ambari.server.orm.entities.ViewEntity;
  import org.apache.ambari.server.security.authorization.RoleAuthorization;
  import org.apache.ambari.server.view.ViewRegistry;
  
  import com.google.common.collect.ImmutableMap;
  import com.google.common.collect.Sets;
  
  /**
   * Resource provider for view instances.
   */
  public class ViewResourceProvider extends AbstractAuthorizedResourceProvider {
  
    public static final String VIEW_INFO = "ViewInfo";
    public static final String VIEW_NAME_PROPERTY_ID = "view_name";
  
    public static final String VIEW_NAME = VIEW_INFO + PropertyHelper.EXTERNAL_PATH_SEP + VIEW_NAME_PROPERTY_ID;
  
  
    /**
     * The key property ids for a view resource.
     */
    private static final Map<Resource.Type, String> keyPropertyIds = ImmutableMap.<Resource.Type, String>builder()
      .put(Resource.Type.View, VIEW_NAME)
      .build();
  
    /**
     * The property ids for a view resource.
     */
    private static final Set<String> propertyIds = Sets.newHashSet(VIEW_NAME);
  
  
    // ----- Constructors ------------------------------------------------------
  
    /**
     * Construct a view resource provider.
     */
    public ViewResourceProvider() {
      super(Resource.Type.View, propertyIds, keyPropertyIds);
  
      EnumSet<RoleAuthorization> requiredAuthorizations = EnumSet.of(RoleAuthorization.AMBARI_MANAGE_VIEWS);
      setRequiredCreateAuthorizations(requiredAuthorizations);
      setRequiredDeleteAuthorizations(requiredAuthorizations);
      setRequiredUpdateAuthorizations(requiredAuthorizations);
    }
  
  
    // ----- ResourceProvider --------------------------------------------------
  
    @Override
    protected RequestStatus createResourcesAuthorized(Request request)
        throws SystemException, UnsupportedPropertyException,
               ResourceAlreadyExistsException, NoSuchParentResourceException {
      throw new UnsupportedOperationException("Not yet supported.");
    }
  
    @Override
    public Set<Resource> getResources(Request request, Predicate predicate)
        throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
      Set<Resource> resources    = new HashSet<>();
      ViewRegistry  viewRegistry = ViewRegistry.getInstance();
      Set<String>   requestedIds = getRequestPropertyIds(request, predicate);
  
      Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate);
  
      if (propertyMaps.isEmpty()) {
        propertyMaps.add(Collections.emptyMap());
      }
  
      for (Map<String, Object> propertyMap : propertyMaps) {
  
        String viewName    = (String) propertyMap.get(VIEW_NAME);
  
        for (ViewEntity viewDefinition : viewRegistry.getDefinitions()){
          if (viewName == null || viewName.equals(viewDefinition.getCommonName())) {
            if (viewRegistry.includeDefinition(viewDefinition)) {
              Resource resource = new ResourceImpl(Resource.Type.View);
  
              setResourceProperty(resource, VIEW_NAME, viewDefinition.getCommonName(), requestedIds);
  
              resources.add(resource);
            }
          }
        }
      }
  
      return resources;
    }
  
    @Override
    protected RequestStatus updateResourcesAuthorized(Request request, Predicate predicate)
        throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
      throw new UnsupportedOperationException("Not yet supported.");
    }
  
    @Override
    protected RequestStatus deleteResourcesAuthorized(Request request, Predicate predicate)
        throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {
      throw new UnsupportedOperationException("Not yet supported.");
    }
  
    @Override
    public Map<Resource.Type, String> getKeyPropertyIds() {
      return keyPropertyIds;
    }
  
  
    // ----- AbstractResourceProvider ------------------------------------------
  
    @Override
    protected Set<String> getPKPropertyIds() {
      return new HashSet<>(keyPropertyIds.values());
    }
  }
  