/*
 * 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.feed.impl;

import stroom.docref.DocRef;
import stroom.docstore.api.DocumentResourceHelper;
import stroom.event.logging.rs.api.AutoLogged;
import stroom.feed.api.FeedStore;
import stroom.feed.shared.FeedDoc;
import stroom.feed.shared.FeedResource;
import stroom.util.shared.EntityServiceException;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

@AutoLogged
class FeedResourceImpl implements FeedResource {

    private static final List<String> SUPPORTED_ENCODINGS;

    static {
        final List<String> list = new ArrayList<>();
        list.add("UTF-8");
        list.add("UTF-16LE");
        list.add("UTF-16BE");
        list.add("UTF-32LE");
        list.add("UTF-32BE");
        list.add("ASCII");
        list.add("");

        list.addAll(Charset.availableCharsets().keySet());

        SUPPORTED_ENCODINGS = list;
    }

    private final FeedStore feedStore;
    private final DocumentResourceHelper documentResourceHelper;

    @Inject
    FeedResourceImpl(final FeedStore feedStore,
                     final DocumentResourceHelper documentResourceHelper) {
        this.feedStore = feedStore;
        this.documentResourceHelper = documentResourceHelper;
    }

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

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

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

    @Override
    public List<String> fetchSupportedEncodings() {
        return SUPPORTED_ENCODINGS;
    }
}

@@ -1018,16 +1018,46 @@
 .Inject;
+%0Aimport javax.inject.Provider;
 %0A%0A@AutoL
@@ -1552,24 +1552,33 @@
 ivate final 
+Provider%3C
 FeedStore fe
@@ -1566,32 +1566,33 @@
 ovider%3CFeedStore
+%3E
  feedStore;%0A    
@@ -1581,24 +1581,32 @@
 e%3E feedStore
+Provider
 ;%0A    privat
@@ -1609,24 +1609,33 @@
 ivate final 
+Provider%3C
 DocumentReso
@@ -1636,32 +1636,33 @@
 ntResourceHelper
+%3E
  documentResourc
@@ -1668,16 +1668,24 @@
 ceHelper
+Provider
 ;%0A%0A    @
@@ -1714,24 +1714,33 @@
 eImpl(final 
+Provider%3C
 FeedStore fe
@@ -1736,16 +1736,17 @@
 eedStore
+%3E
  feedSto
@@ -1747,16 +1747,24 @@
 eedStore
+Provider
 ,%0A      
@@ -1784,16 +1784,25 @@
   final 
+Provider%3C
 Document
@@ -1815,16 +1815,17 @@
 ceHelper
+%3E
  documen
@@ -1839,16 +1839,24 @@
 ceHelper
+Provider
 ) %7B%0A    
@@ -1873,16 +1873,24 @@
 eedStore
+Provider
  = feedS
@@ -1893,16 +1893,24 @@
 eedStore
+Provider
 ;%0A      
@@ -1938,16 +1938,24 @@
 ceHelper
+Provider
  = docum
@@ -1971,16 +1971,24 @@
 ceHelper
+Provider
 ;%0A    %7D%0A
@@ -2085,16 +2085,30 @@
 ceHelper
+Provider.get()
 .read(fe
@@ -2114,16 +2114,30 @@
 eedStore
+Provider.get()
 , getDoc
@@ -2444,16 +2444,30 @@
 ceHelper
+Provider.get()
 .update(
@@ -2475,16 +2475,30 @@
 eedStore
+Provider.get()
 , doc);%0A

Apply Stroom REST resource standards to FeedResourceImpl