/*
 * Copyright 2017 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.statistics.impl.hbase.entity;

import stroom.docref.DocRef;
import stroom.docstore.api.DocumentResourceHelper;
import stroom.event.logging.rs.api.AutoLogged;
import stroom.statistics.impl.hbase.shared.StatsStoreResource;
import stroom.statistics.impl.hbase.shared.StroomStatsStoreDoc;
import stroom.util.shared.EntityServiceException;

import javax.inject.Inject;

@AutoLogged
class StatsStoreResourceImpl implements StatsStoreResource {

    private final StroomStatsStoreStore stroomStatsStoreStore;
    private final DocumentResourceHelper documentResourceHelper;

    @Inject
    StatsStoreResourceImpl(final StroomStatsStoreStore stroomStatsStoreStore,
                           final DocumentResourceHelper documentResourceHelper) {
        this.stroomStatsStoreStore = stroomStatsStoreStore;
        this.documentResourceHelper = documentResourceHelper;
    }

    @Override
    public StroomStatsStoreDoc fetch(final String uuid) {
        return documentResourceHelper.read(stroomStatsStoreStore, getDocRef(uuid));
    }

    @Override
    public StroomStatsStoreDoc update(final String uuid, final StroomStatsStoreDoc doc) {
        if (doc.getUuid() == null || !doc.getUuid().equals(uuid)) {
            throw new EntityServiceException("The document UUID must match the update UUID");
        }
        return documentResourceHelper.update(stroomStatsStoreStore, doc);
    }

    private DocRef getDocRef(final String uuid) {
        return DocRef.builder()
                .uuid(uuid)
                .type(StroomStatsStoreDoc.DOCUMENT_TYPE)
                .build();
    }
}

@@ -971,16 +971,77 @@
 .Inject;
+%0Aimport javax.inject.Provider;%0Aimport javax.inject.Singleton;
 %0A%0A@AutoL
@@ -1046,16 +1046,27 @@
 oLogged%0A
+@Singleton%0A
 class St
@@ -1133,24 +1133,33 @@
 ivate final 
+Provider%3C
 StroomStatsS
@@ -1159,32 +1159,33 @@
 mStatsStoreStore
+%3E
  stroomStatsStor
@@ -1186,24 +1186,32 @@
 tsStoreStore
+Provider
 ;%0A    privat
@@ -1214,24 +1214,33 @@
 ivate final 
+Provider%3C
 DocumentReso
@@ -1241,32 +1241,33 @@
 ntResourceHelper
+%3E
  documentResourc
@@ -1273,16 +1273,24 @@
 ceHelper
+Provider
 ;%0A%0A    @
@@ -1325,24 +1325,33 @@
 eImpl(final 
+Provider%3C
 StroomStatsS
@@ -1359,16 +1359,17 @@
 oreStore
+%3E
  stroomS
@@ -1382,16 +1382,24 @@
 oreStore
+Provider
 ,%0A      
@@ -1425,16 +1425,25 @@
   final 
+Provider%3C
 Document
@@ -1456,16 +1456,17 @@
 ceHelper
+%3E
  documen
@@ -1480,16 +1480,24 @@
 ceHelper
+Provider
 ) %7B%0A    
@@ -1526,16 +1526,24 @@
 oreStore
+Provider
  = stroo
@@ -1558,16 +1558,24 @@
 oreStore
+Provider
 ;%0A      
@@ -1603,16 +1603,24 @@
 ceHelper
+Provider
  = docum
@@ -1636,16 +1636,24 @@
 ceHelper
+Provider
 ;%0A    %7D%0A
@@ -1762,16 +1762,30 @@
 ceHelper
+Provider.get()
 .read(st
@@ -1803,16 +1803,30 @@
 oreStore
+Provider.get()
 , getDoc
@@ -2157,16 +2157,30 @@
 ceHelper
+Provider.get()
 .update(
@@ -2200,16 +2200,30 @@
 oreStore
+Provider.get()
 , doc);%0A

Apply REST stds to StatsStoreResourceImpl