package org.kaaproject.kaa.server.admin.client.mvp.activity;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.Collections;
import java.util.List;
import org.kaaproject.avro.ui.gwt.client.util.BusyAsyncCallback;
import org.kaaproject.kaa.common.dto.EndpointUserConfigurationDto;
import org.kaaproject.kaa.server.admin.client.KaaAdmin;
import org.kaaproject.kaa.server.admin.client.mvp.ClientFactory;
import org.kaaproject.kaa.server.admin.client.mvp.place.GetUserConfigPlace;
import org.kaaproject.kaa.server.admin.client.mvp.view.GetUserConfigView;
import org.kaaproject.kaa.server.admin.client.servlet.ServletHelper;
import org.kaaproject.kaa.server.admin.client.util.Utils;
import org.kaaproject.kaa.server.admin.shared.schema.SchemaInfoDto;


public class GetUserConfigActivity extends AbstractDetailsActivity<EndpointUserConfigurationDto, GetUserConfigView, GetUserConfigPlace> {

    private String applicationId;

    public GetUserConfigActivity(GetUserConfigPlace place, ClientFactory clientFactory) {
        super(place, clientFactory);
        this.applicationId = place.getApplicationId();
    }

    @Override
    protected String getEntityId(GetUserConfigPlace place) {
        return null;
    }

    @Override
    protected GetUserConfigView getView(boolean create) {
        return clientFactory.getUserConfigView();
    }

    @Override
    protected EndpointUserConfigurationDto newEntity() {
        return new EndpointUserConfigurationDto();
    }

    @Override
    protected void onEntityRetrieved() {
        KaaAdmin.getDataSource().getUserConfigurationSchemaInfosByApplicationId(applicationId,
                new BusyAsyncCallback<List<SchemaInfoDto>>() {
                    @Override
                    public void onSuccessImpl(List<SchemaInfoDto> result) {
                        Collections.sort(result);
                        SchemaInfoDto schemaInfo = result.get(result.size()-1);
                        detailsView.getConfigurationSchemaInfo().setValue(schemaInfo);
                        detailsView.getConfigurationSchemaInfo().setAcceptableValues(result);

                    }
                    @Override
                    public void onFailureImpl(Throwable caught) {
                        Utils.handleException(caught, detailsView);
                    }
                });
        registrations.add(detailsView.getDownloadUserCongigurationButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {

                KaaAdmin.getDataSource().findUserConfigurationByExternalUIdAndAppIdAndSchemaVersion(
                        detailsView.getExternalUserId().getValue(),
                        applicationId,
                        detailsView.getConfigurationSchemaInfo().getValue().getVersion(),
                        new BusyAsyncCallback<EndpointUserConfigurationDto>() {
                            @Override
                            public void onSuccessImpl(EndpointUserConfigurationDto result) {
                                ServletHelper.downloadUserConfiguration(
                                        detailsView.getExternalUserId().getValue(),
                                        String.valueOf(detailsView.getConfigurationSchemaInfo().getValue().getVersion()),
                                        applicationId
                                );
                            }
                            @Override
                            public void onFailureImpl(Throwable caught) {
                                Utils.handleException(caught, detailsView);
                            }
                        });
            }
        }));
    }
    @Override
    protected void onSave() {
    }

    @Override
    protected void getEntity(String id, AsyncCallback<EndpointUserConfigurationDto> callback) {
    }

    @Override
    protected void editEntity(EndpointUserConfigurationDto entity, AsyncCallback<EndpointUserConfigurationDto> callback) {
    }
    @Override
    protected void bind(EventBus eventBus) {
        super.bind(eventBus);
    }
}

@@ -1,8 +1,616 @@
+/*%0A * Copyright 2014-2016 CyberVision, Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 

add ability to retrieve user configuration using Web UI,add licence