/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed 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 io.gravitee.repository.api.management;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.model.management.Api;
import io.gravitee.repository.model.management.PolicyConfiguration;
import io.gravitee.repository.model.management.User;
import io.gravitee.repository.model.management.Team;

/**
 * @author David BRASSELY (brasseld at gmail.com)
 */
public interface ApiRepository {

    /**
     * Get an API using its name.
     *
     * @param apiName The name of the API to retrieve.
     * @return An {@link Optional} API.
     */
    Optional<Api> findByName(String apiName) throws TechnicalException;

    /**
     * List all public APIs.
     *
     * @return All public APIs.
     */
    Set<Api> findAll() throws TechnicalException;

    /**
     * List APIs (public and/or private) hold by a {@link Team}.
     *
     * @param teamName The name of the team.
     * @param publicOnly List only public APIs.
     * @return List APIs from a team.
     */
    Set<Api> findByTeam(String teamName, boolean publicOnly) throws TechnicalException;

    /**
     * List APIs (public and private) hold by a {@link User}.
     *
     * @param username The name of the user.
     * @param publicOnly List only public APIs.
     * @return List APIs from a user.
     */
    Set<Api> findByUser(String username, boolean publicOnly) throws TechnicalException;

    /**
     * Create an API
     * 
     * @param api api to create
     * @return api creaded
     */
    Api create(Api api) throws TechnicalException;

    /**
     * Update an API
     * 
     * @param api api to update
     * @return api updated
     */
    Api update(Api api) throws TechnicalException;

    /**
     * Delete an API
     * 
     * @param apiName api name to delete
     */
    void delete(String apiName) throws TechnicalException;

    /**
     * Count all APIs (public and private) owned by a given {@link User}
     * 
     * @param username owner user name 
     * @param publicOnly List only public APIs.
     * @return counted APIs
     */
    int countByUser(String username, boolean publicOnly) throws TechnicalException;
   
    /**
    * Count all APIs (public and private) owned by a given {@link Team}
    * 
    * @param teamName owner team name 
    * @param publicOnly List only public APIs.
    * @return counted APIs
    */
    int countByTeam(String teamName, boolean publicOnly) throws TechnicalException;

    /**
     * Update an API policies
     * 
     * @param apiName API name
     * @param policyConfigurations Ordered list of {@link PolicyConfiguration} to set to the API
     */
    void updatePoliciesConfiguration(String apiName, List<PolicyConfiguration> policyConfigurations) throws TechnicalException;
  
    /**
     * Update a API policy
     * 
     * @param apiName API name
     * @param policyConfiguration {@link PolicyConfiguration} to update
     */
    void updatePolicyConfiguration(String apiName, PolicyConfiguration policyConfiguration) throws TechnicalException;

    /**
     * Give all {@link PolicyConfiguration} for an API
     * 
     * @param apiName API name
     * @return API policies configuration
     */
    List<PolicyConfiguration> findPoliciesByApi(String apiName) throws TechnicalException;
    
    /**
     * Find APIs by creator
     * 
     * @param userName creator {@link User} name
     * @return APIs created by the user
     */
    Set<Api> findByCreator(String userName) throws TechnicalException;
    
    /**
     * Find Apis associated with an application
     * 
     * @param application Application Name
     * @return Apis associated
     */
    Set<Api> findByApplication(String application) throws TechnicalException;

}

@@ -3189,25 +3189,24 @@
 icies%0A     *
- 
 %0A     * @par
@@ -3326,32 +3326,56 @@
 API%0A     */%0A    
+@Deprecated%0A    default 
 void updatePolic
@@ -3480,20 +3480,76 @@
 xception
-;%0A  
+ %7B%0A        throw new UnsupportedOperationException();%0A    %7D%0A
 %0A    /**
@@ -3578,25 +3578,24 @@
 olicy%0A     *
- 
 %0A     * @par
@@ -3694,24 +3694,48 @@
      */%0A    
+@Deprecated%0A    default 
 void updateP
@@ -3839,81 +3839,694 @@
 tion
-;%0A%0A    /**%0A     * Give all %7B@link PolicyConfiguration%7D for an API%0A     * 
+ %7B%0A        throw new UnsupportedOperationException();%0A    %7D%0A%0A    /**%0A     * Update a API policy descriptor%0A     *%0A     * @param apiName API name%0A     * @param jsonDescriptor to update%0A     */%0A    default void updateDescriptor(String apiName, String jsonDescriptor) throws TechnicalException %7B%0A        // TODO remove default%0A    %7D%0A%0A    /**%0A     * Give all %7B@link PolicyConfiguration%7D for an API%0A     *%0A     * @param apiName API name%0A     * @return API JSON descriptor%0A     */%0A    default String findDescriptorByApi(String apiName) throws TechnicalException %7B%0A        // TODO remove default%0A        return null;%0A    %7D%0A%0A    /**%0A     * Give all %7B@link PolicyConfiguration%7D for an API%0A     *
 %0A   
@@ -4607,16 +4607,40 @@
  */%0A    
+@Deprecated%0A    default 
 List%3CPol
@@ -4708,33 +4708,91 @@
 chnicalException
-;
+ %7B%0A        throw new UnsupportedOperationException();%0A    %7D
 %0A    %0A    /**%0A  

Save and update API descriptor as a json String