/*
 * Copyright 2016 Crown Copyright
 *
 * 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 stroom.stats.client.presenter;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.web.bindery.event.shared.EventBus;
import stroom.entity.client.event.DirtyEvent;
import stroom.entity.client.event.DirtyEvent.DirtyHandler;
import stroom.entity.client.presenter.ContentCallback;
import stroom.entity.client.presenter.EntityEditTabPresenter;
import stroom.entity.client.presenter.LinkTabPanelView;
import stroom.entity.client.presenter.TabContentProvider;
import stroom.security.client.ClientSecurityContext;
import stroom.statistics.shared.StatisticStoreEntity;
import stroom.statistics.shared.StatisticsDataSourceData;
import stroom.widget.tab.client.presenter.TabData;
import stroom.widget.tab.client.presenter.TabDataImpl;

public class StroomStatsStorePresenter extends EntityEditTabPresenter<LinkTabPanelView, StatisticStoreEntity> {
    private static final TabData SETTINGS = new TabDataImpl("Settings");
    private static final TabData FIELDS = new TabDataImpl("Fields");
    private static final TabData CUSTOM_ROLLUPS = new TabDataImpl("Custom Roll-ups");

    private final TabContentProvider<StatisticStoreEntity> tabContentProvider = new TabContentProvider<StatisticStoreEntity>();

    @Inject
    public StroomStatsStorePresenter(final EventBus eventBus, final LinkTabPanelView view,
                                     final Provider<StroomStatsStoreSettingsPresenter> statisticsDataSourceSettingsPresenter,
                                     final Provider<StroomStatsStoreFieldListPresenter> statisticsFieldListPresenter,
                                     final Provider<StroomStatsStoreCustomMaskListPresenter> statisticsCustomMaskListPresenter,
                                     final ClientSecurityContext securityContext) {
        super(eventBus, view, securityContext);

        tabContentProvider.setDirtyHandler(new DirtyHandler() {
            @Override
            public void onDirty(final DirtyEvent event) {
                if (event.isDirty()) {
                    setDirty(true);
                }
            }
        });

        tabContentProvider.add(SETTINGS, statisticsDataSourceSettingsPresenter);
        tabContentProvider.add(FIELDS, statisticsFieldListPresenter);
        tabContentProvider.add(CUSTOM_ROLLUPS, statisticsCustomMaskListPresenter);

        addTab(SETTINGS);
        addTab(FIELDS);
        addTab(CUSTOM_ROLLUPS);

        selectTab(SETTINGS);

    }

    @Override
    protected void getContent(final TabData tab, final ContentCallback callback) {
        callback.onReady(tabContentProvider.getPresenter(tab));
    }

    @Override
    public void onRead(final StatisticStoreEntity statisticsDataSource) {
        if (statisticsDataSource != null) {
            if (statisticsDataSource.getStatisticDataSourceDataObject() == null) {
                statisticsDataSource.setStatisticDataSourceDataObject(new StatisticsDataSourceData());
            }
        }

        tabContentProvider.read(statisticsDataSource);

        // the field and rollup presenters need to know about each other as
        // changes in one affect the other
        ((StroomStatsStoreFieldListPresenter) tabContentProvider.getPresenter(FIELDS)).setCustomMaskListPresenter(
                (StroomStatsStoreCustomMaskListPresenter) tabContentProvider.getPresenter(CUSTOM_ROLLUPS));
    }

    @Override
    protected void onWrite(final StatisticStoreEntity statisticsDataSource) {
        tabContentProvider.write(statisticsDataSource);
    }

    @Override
    public String getType() {
        return StatisticStoreEntity.ENTITY_TYPE;
    }
}

@@ -758,113 +758,8 @@
 us;%0A
-import stroom.entity.client.event.DirtyEvent;%0Aimport stroom.entity.client.event.DirtyEvent.DirtyHandler;%0A
 impo
@@ -1048,37 +1048,32 @@
 port stroom.stat
-istic
 s.shared.Statist
@@ -1067,23 +1067,25 @@
 hared.St
-atistic
+roomStats
 StoreEnt
@@ -1107,21 +1107,16 @@
 oom.stat
-istic
 s.shared
@@ -1118,34 +1118,36 @@
 hared.St
-atisticsDataSource
+roomStatsStoreEntity
 Data;%0Aim
@@ -1341,23 +1341,25 @@
 View, St
-atistic
+roomStats
 StoreEnt
@@ -1625,39 +1625,41 @@
 ntentProvider%3CSt
-atistic
+roomStats
 StoreEntity%3E tab
@@ -1701,23 +1701,25 @@
 vider%3CSt
-atistic
+roomStats
 StoreEnt
@@ -1919,33 +1919,29 @@
 nter%3E st
-atisticsDataSourc
+roomStatsStor
 eSetting
@@ -2042,24 +2042,30 @@
 nter%3E st
-atistics
+roomStatsStore
 FieldLis
@@ -2171,24 +2171,30 @@
 nter%3E st
-atistics
+roomStatsStore
 CustomMa
@@ -2390,106 +2390,16 @@
 ler(
-new DirtyHandler() %7B%0A            @Override%0A            public void onDirty(final DirtyEvent event)
+event -%3E
  %7B%0A 
@@ -2405,28 +2405,24 @@
             
- 
-   
 if (event.is
@@ -2448,20 +2448,16 @@
         
-    
 setDirty
@@ -2468,26 +2468,8 @@
 e);%0A
-                %7D%0A
     
@@ -2534,33 +2534,29 @@
 INGS, st
-atisticsDataSourc
+roomStatsStor
 eSetting
@@ -2609,24 +2609,30 @@
 ELDS, st
-atistics
+roomStatsStore
 FieldLis
@@ -2693,24 +2693,30 @@
 LUPS, st
-atistics
+roomStatsStore
 CustomMa
@@ -2842,25 +2842,24 @@
 (SETTINGS);%0A
-%0A
     %7D%0A%0A    @
@@ -3058,39 +3058,41 @@
  onRead(final St
-atistic
+roomStats
 StoreEntity stat
@@ -3093,251 +3093,223 @@
 y st
-atisticsDataSource) %7B%0A        if (statisticsDataSource != null) %7B%0A            if (statisticsDataSource.getStatisticDataSourceDataObject() == null) %7B%0A                statisticsDataSource.setStatisticDataSourceDataObject(new StatisticsDataSource
+roomStatsStoreEntity) %7B%0A        if (stroomStatsStoreEntity != null) %7B%0A            if (stroomStatsStoreEntity.getDataObject() == null) %7B%0A                stroomStatsStoreEntity.setDataObject(new StroomStatsStoreEntity
 Data
@@ -3372,34 +3372,36 @@
 .read(st
-atisticsDataSource
+roomStatsStoreEntity
 );%0A%0A    
@@ -3794,23 +3794,25 @@
 final St
-atistic
+roomStats
 StoreEnt
@@ -3817,34 +3817,36 @@
 ntity st
-atisticsDataSource
+roomStatsStoreEntity
 ) %7B%0A    
@@ -3880,26 +3880,28 @@
 e(st
-atisticsDataSource
+roomStatsStoreEntity
 );%0A 
@@ -3971,15 +3971,17 @@
 n St
-atistic
+roomStats
 Stor

Remove sql stats entity references