/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.usergrid.services.notifiers;

import org.apache.usergrid.persistence.Entity;
import org.apache.usergrid.persistence.entities.Notifier;
import org.apache.usergrid.services.notifications.ProviderAdapterFactory;
import org.apache.usergrid.services.notifications.TestAdapter;
import org.apache.usergrid.services.notifications.apns.APNsAdapter;
import org.apache.usergrid.services.notifications.gcm.GCMAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.usergrid.services.*;
import org.apache.usergrid.services.notifications.NotificationsService;
import org.apache.usergrid.services.notifications.ProviderAdapter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class NotifiersService extends AbstractCollectionService {

    private static final Logger logger = LoggerFactory
            .getLogger(NotifiersService.class);

    public NotifiersService() {
        super();
        logger.info("/notifiers");
    }

    @Override
    public ServiceResults postCollection(ServiceContext context)
            throws Exception {

        ServicePayload payload = context.getPayload();

        NotificationsService ns = (NotificationsService) sm
                .getService("notifications");

        String provider = payload.getStringProperty("provider");


        ServiceResults results = super.postCollection(context);
        Notifier notifier = (Notifier) results.getEntity();
        if (notifier != null) {
            try {
                ProviderAdapter providerAdapter = ProviderAdapterFactory.getProviderAdapter(notifier, em);

                if (providerAdapter==null) {
                    throw new IllegalArgumentException("provider must be one of: "
                            + Arrays.toString(ProviderAdapterFactory.getValidProviders()));
                }
                providerAdapter.validateCreateNotifier(payload);
                ns.testConnection(notifier);
            } catch (Exception e) {
                logger.info("notifier testConnection() failed", e);
                em.delete(notifier);
                throw e;
            }
        }

        return results;
    }
}

@@ -849,55 +849,8 @@
 s;%0A%0A
-import org.apache.usergrid.persistence.Entity;%0A
 impo
@@ -899,24 +899,24 @@
 s.Notifier;%0A
+
 import org.a
@@ -981,205 +981,8 @@
 ry;%0A
-import org.apache.usergrid.services.notifications.TestAdapter;%0Aimport org.apache.usergrid.services.notifications.apns.APNsAdapter;%0Aimport org.apache.usergrid.services.notifications.gcm.GCMAdapter;%0A
 impo
@@ -1212,17 +1212,16 @@
 dapter;%0A
-%0A
 import j
@@ -1240,78 +1240,8 @@
 ays;
-%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Set;
 %0A%0Apu
@@ -1610,17 +1610,16 @@
 ption %7B%0A
-%0A
         
@@ -1668,183 +1668,8 @@
 d();
-%0A%0A        NotificationsService ns = (NotificationsService) sm%0A                .getService(%22notifications%22);%0A%0A        String provider = payload.getStringProperty(%22provider%22);%0A%0A
 %0A   
@@ -2177,32 +2177,32 @@
               %7D%0A
-
                 
@@ -2250,16 +2250,113 @@
 yload);%0A
+                NotificationsService ns = (NotificationsService) sm.getService(%22notifications%22);%0A
         

remove unneeded refs