/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.service.MetricsSummaryBin
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.Entity
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityManager$UniqueType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.UpdateException
 *  com.l7tech.util.Pair
 *  org.apache.commons.lang3.StringUtils
 *  org.springframework.dao.DataAccessException
 *  org.springframework.orm.hibernate5.HibernateCallback
 */
package com.l7tech.server.wsdm.subscription;

import com.l7tech.gateway.common.service.MetricsSummaryBin;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.Entity;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityManager;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.wsdm.Aggregator;
import com.l7tech.server.wsdm.faults.ResourceUnknownFault;
import com.l7tech.server.wsdm.faults.UnacceptableTerminationTimeFault;
import com.l7tech.server.wsdm.subscription.Subscription;
import com.l7tech.server.wsdm.subscription.SubscriptionKey;
import com.l7tech.server.wsdm.subscription.SubscriptionManager;
import com.l7tech.server.wsdm.subscription.a;
import com.l7tech.server.wsdm.subscription.b;
import com.l7tech.server.wsdm.subscription.c;
import com.l7tech.server.wsdm.subscription.d;
import com.l7tech.server.wsdm.subscription.e;
import com.l7tech.server.wsdm.subscription.f;
import com.l7tech.server.wsdm.subscription.g;
import com.l7tech.util.Pair;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateCallback;

public class SubscriptionManagerImpl
extends HibernateEntityManager<Subscription, EntityHeader>
implements SubscriptionManager {
    private static final Logger m;
    private static final String n;
    private static final String o;
    private static final String p;
    private static final String q;
    private static final String r;
    private static final String s;
    private static final String t;
    private static final String u;
    private static final String v;
    private Aggregator w;
    private static final String[] x;

    @Override
    public void setAggregator(Aggregator aggregator) {
        this.w = aggregator;
    }

    @Override
    public Subscription findByUuid(String string) throws FindException {
        try {
            return this.a(string);
        }
        catch (DataAccessException dataAccessException) {
            throw new FindException(x[1] + string, (Throwable)dataAccessException);
        }
    }

    @Override
    public void deleteByUuid(String string) throws FindException, DeleteException, ResourceUnknownFault {
        Subscription subscription = this.findByUuid(string);
        try {
            if (subscription == null) {
                throw new ResourceUnknownFault(x[4] + string);
            }
        }
        catch (DataAccessException dataAccessException) {
            throw SubscriptionManagerImpl.a((Exception)((Object)dataAccessException));
        }
        try {
            this.getHibernateTemplate().delete((Object)subscription);
        }
        catch (DataAccessException dataAccessException) {
            throw new DeleteException(x[3] + string, (Throwable)dataAccessException);
        }
    }

    private Subscription a(String string) {
        List list = (List)this.getHibernateTemplate().execute((HibernateCallback)new a(this, string));
        try {
            if (list.isEmpty()) {
                return null;
            }
        }
        catch (DataAccessException dataAccessException) {
            throw SubscriptionManagerImpl.a((Exception)((Object)dataAccessException));
        }
        try {
            if (list.size() > 1) {
                throw new IllegalStateException(x[24] + string);
            }
        }
        catch (DataAccessException dataAccessException) {
            throw SubscriptionManagerImpl.a((Exception)((Object)dataAccessException));
        }
        return (Subscription)((Object)list.iterator().next());
    }

    @Override
    public void deleteExpiredSubscriptions() throws DeleteException {
        try {
            this.getHibernateTemplate().execute((HibernateCallback)new f(this));
        }
        catch (DataAccessException dataAccessException) {
            throw new DeleteException(x[5], (Throwable)dataAccessException);
        }
    }

    @Override
    public void renewSubscription(String string, long l2, String string2) throws FindException, UpdateException, ResourceUnknownFault, UnacceptableTerminationTimeFault {
        String string3;
        Subscription subscription = this.findByUuid(string);
        try {
            if (subscription == null) {
                throw new ResourceUnknownFault(x[23] + string);
            }
        }
        catch (FindException findException) {
            throw SubscriptionManagerImpl.a((Exception)((Object)findException));
        }
        long l3 = System.currentTimeMillis();
        try {
            if (l2 < l3) {
                m.warning(x[0]);
                throw new UnacceptableTerminationTimeFault(x[13]);
            }
        }
        catch (FindException findException) {
            throw SubscriptionManagerImpl.a((Exception)((Object)findException));
        }
        boolean bl2 = false;
        long l4 = subscription.getTermination();
        if (l2 != l4) {
            String[] stringArray = x;
            m.info(MessageFormat.format(stringArray[10], string, l2));
            subscription.setTermination(l2);
            bl2 = true;
        }
        if (!StringUtils.equals((CharSequence)(string3 = subscription.getNotificationPolicyGuid()), (CharSequence)string2)) {
            m.info(MessageFormat.format(x[22], string, string2));
            subscription.setNotificationPolicyGuid(string2);
            bl2 = true;
        }
        try {
            if (bl2) {
                this.update(subscription);
            }
        }
        catch (FindException findException) {
            throw SubscriptionManagerImpl.a((Exception)((Object)findException));
        }
    }

    @Override
    public Collection<Subscription> findByNodeAndServiceGoid(String string, Goid goid) throws FindException {
        return (Collection)this.getHibernateTemplate().execute((HibernateCallback)new b(this, string, goid));
    }

    @Override
    public Collection<Subscription> findBySubscriptionKey(SubscriptionKey subscriptionKey) throws FindException {
        return (Collection)this.getHibernateTemplate().execute((HibernateCallback)new c(this, subscriptionKey));
    }

    @Override
    public void notified(String string) throws UpdateException {
        long l2 = System.currentTimeMillis();
        this.getHibernateTemplate().execute((HibernateCallback)new g(this, l2, string));
    }

    @Override
    public Map<SubscriptionKey, Pair<Subscription, MetricsSummaryBin>> findNotifiableMetricsForNode(String string, long l2) throws FindException {
        Map<Goid, MetricsSummaryBin> map;
        List list;
        block8: {
            long l3 = System.currentTimeMillis();
            try {
                list = (List)this.getHibernateTemplate().execute((HibernateCallback)new d(this, string, l3));
            }
            catch (DataAccessException dataAccessException) {
                String[] stringArray = x;
                throw new FindException(stringArray[11], (Throwable)dataAccessException);
            }
            map = this.w.getMetricsForServices();
            try {
                try {
                    if (list != null && !list.isEmpty()) break block8;
                }
                catch (DataAccessException dataAccessException) {
                    throw SubscriptionManagerImpl.a((Exception)((Object)dataAccessException));
                }
                return Collections.emptyMap();
            }
            catch (DataAccessException dataAccessException) {
                throw SubscriptionManagerImpl.a((Exception)((Object)dataAccessException));
            }
        }
        HashMap<SubscriptionKey, Pair<Subscription, MetricsSummaryBin>> hashMap = new HashMap<SubscriptionKey, Pair<Subscription, MetricsSummaryBin>>();
        for (Subscription subscription : list) {
            Goid goid = subscription.getPublishedServiceGoid();
            MetricsSummaryBin metricsSummaryBin = map.get(goid);
            if (metricsSummaryBin != null) {
                SubscriptionKey subscriptionKey = new SubscriptionKey(subscription);
                hashMap.put(subscriptionKey, (Pair<Subscription, MetricsSummaryBin>)new Pair((Object)subscription, (Object)metricsSummaryBin));
                continue;
            }
            m.fine(x[16] + String.valueOf(goid));
        }
        return hashMap;
    }

    @Override
    public void stealSubscriptionsFromDeadNodes(String string, long l2) {
        List list;
        long l3 = System.currentTimeMillis();
        try {
            list = (List)this.getHibernateTemplate().execute((HibernateCallback)new e(this, string, l2, l3));
        }
        catch (DataAccessException dataAccessException) {
            String[] stringArray = x;
            m.log(Level.WARNING, stringArray[7], dataAccessException);
            return;
        }
        for (Subscription subscription : list) {
            String[] stringArray = x;
            m.log(Level.INFO, MessageFormat.format(stringArray[8], subscription.getUuid(), subscription.getOwnerNodeId()));
            subscription.setOwnerNodeId(string);
            subscription.setLastNotificationTime(l3);
            try {
                this.update(subscription);
            }
            catch (UpdateException updateException) {
                m.log(Level.WARNING, x[2] + subscription.getUuid());
            }
        }
    }

    @Override
    protected EntityManager.UniqueType getUniqueType() {
        return EntityManager.UniqueType.OTHER;
    }

    @Override
    protected Collection<Map<String, Object>> getUniqueConstraints(final Subscription subscription) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>(){
            static final long serialVersionUID = 5979641669591249536L;
            private static final String a = 8.a(89, 8.a("Bm\u0001h"));
            {
                this.put(a, subscription.getUuid());
            }

            private static char[] a(String string) {
                char[] cArray = string.toCharArray();
                if (cArray.length < 2) {
                    cArray = cArray;
                    cArray[0] = (char)(cArray[0] ^ 0x50);
                }
                return cArray;
            }

            /*
             * Handled impossible loop by duplicating code
             * Enabled aggressive block sorting
             */
            private static String a(int n2, char[] cArray) {
                char[] cArray2;
                block11: {
                    int n3;
                    int n4;
                    int n5;
                    block10: {
                        n5 = n2;
                        cArray2 = cArray;
                        n4 = cArray.length;
                        n3 = 0;
                        if (!true) break block10;
                        n4 = n4;
                        if (n4 <= n3) break block11;
                    }
                    do {
                        n5 = n5;
                        cArray2 = cArray2;
                        int n6 = n3;
                        cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                            case 0 -> 110;
                            case 1 -> 65;
                            case 2 -> 49;
                            case 3 -> 85;
                            case 4 -> 106;
                            case 5 -> 4;
                            default -> 80;
                        })));
                        ++n3;
                        n4 = n4;
                    } while (n4 > n3);
                }
                return new String(cArray2).intern();
            }
        };
        return Arrays.asList(hashMap);
    }

    public Class<? extends Entity> getImpClass() {
        return Subscription.class;
    }

    @Override
    public Class<? extends Entity> getInterfaceClass() {
        return Subscription.class;
    }

    @Override
    public String getTableName() {
        return x[17];
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[27];
                var3_1 = 0;
                var2_2 = "d}F\u001e\u0019;M\u0005}][\u0006.W@~\u0012\u001a\u001ak|vD\u0012\b\u0001)JF{[\u000b\u0000\"VK)E\u0012\u0000#\u0019D)F\u001e\u0006&PKhF\u0012\u001b%\u0019Q`_\u001eT\"W\u0005}Z\u001eT;XV}\u001c[&.MP{\\\u0012\u001a,\u0019vFs+T-XPeF&pgS\u0019\u0018.\u0019Qf\u0012\u001d\u001d%]\u0005ZG\u0019\u0007(KLyF\u0012\u001b%\u0019R`F\u0013T\u001ellM\u0012\u001dpgS\u0019\u0018.\u0019Qf\u0012\t\u00118\\Q)]\f\u001a.K\u0005@v[\u0012$K\u0005(pgS\u0019\u0018.\u0019Qf\u0012\u001f\u0011'\\Ql\u0012(\u0001)JF{[\u000b\u0000\"VK)E\u0012\u0000#\u0019p\\{?T\u001dkf\u0012\b\u0001(Q\u0005zG\u0019\u0007(KLyF\u0012\u001b%\u0019R`F\u0013T\"]\u0005&pgS\u0019\u0018.\u0019Qf\u0012\u001f\u0011'\\Ql\u0012\u001e\f;PWlV[\u0007>[Vj@\u0012\u0004?PJgA\u000bJ~\\\u001e\u0006\u0005VAl{\u001f\u001cpgS\u0019\u0018.\u0019Qf\u0012\u001d\u001d%]\u0005zG\u0019\u0007(KLyF\u0012\u001b%JWdzA\u000e\u0019\"WB)Q\u0014\u001a?KJe\u0012\u0014\u0012kJQh^\u001eT8LGzQ\t\u001d;MLf\\[\u000f{D\u0005!P\u001e\u0018$WBlV[\u0000$\u0019KfV\u001eT0\bX)F\u0013\u0015?\u0019Lz\u0012\u000b\u0006.JPdW\u001fT/\\Dm\u001b\u000bQl@\u0016\u001d%XQ`]\u0015;pyV\u001a\u0000\"WB)w(9kJPkA\u0018\u0006\"IQ`]\u0015T0\tX)E\u0012\u0000#\u0019KlE[\u0000.KH`\\\u001a\u0000\"VK)F\u0012\u0019.\u0019^8O\u001cpgS\u0019\u0018.\u0019Qf\u0012\u001d\u001d%]\u0005zG\u0019\u0007(KLyF\u0012\u001b%J\u0004P|[\u001f+fh\\\u0015\u001b?\u0019MhD\u001eT*\u0019Ql@\u0016\u001d%XQ`]\u0015T?PHl\u0012\u0012\u001akMMl\u0012\u000b\u00158M\u000b\u000bJ~\\\u001e\u0006\u0005VAl{\u001f\u0005QfB\u0012\u0017%kf\u0012(\u00119OLjW6\u0011?KLjA[\u0016\"WV)S\r\u0015\"UDk^\u001eT-VW)\u0011RzV\u0016+8LGzQ\t\u001d;MLf\\%pYv: \u000e\u0019\u0000z\u0012\b\u0011?\u0019\u0000z\u0012FTq\u001cV)E\u0013\u00119\\\u0005,A[Ik\u0003\u0000z\u0014IhA\u000f:$MLo[\u0018\u0015?PJgf\u0012\u0019.\u0011WlT\u001e\u0006.WFlq\u001a\u0018'[DjY\u0014IhA\u000f:$MLo[\u0018\u0015?PJgf\u0012\u0019.CpyV\u001a\u0000\"WB)w(9kJPkA\u0018\u0006\"IQ`]\u0015T0\tX)E\u0012\u0000#\u0019KlE[\u001a$MLo[\u0018\u0015?PJg\u0012\u000b\u001b'PFp\u0012<!\u0002}\u0005r\u0003\u0006\u001dkf\u0012\b\u0001(Q\u0005zG\u0019\u0007(KLyF\u0012\u001b%\u0019R`F\u0013T\"]\u0005+cfG\u0015\u0010kTJ{W[\u0000#XK)]\u0015\u0011kjPkA\u0018\u0006\"IQ`]\u0015T<PQa\u0012.!\u0002}\u0005";
                var4_3 = "d}F\u001e\u0019;M\u0005}][\u0006.W@~\u0012\u001a\u001ak|vD\u0012\b\u0001)JF{[\u000b\u0000\"VK)E\u0012\u0000#\u0019D)F\u001e\u0006&PKhF\u0012\u001b%\u0019Q`_\u001eT\"W\u0005}Z\u001eT;XV}\u001c[&.MP{\\\u0012\u001a,\u0019vFs+T-XPeF&pgS\u0019\u0018.\u0019Qf\u0012\u001d\u001d%]\u0005ZG\u0019\u0007(KLyF\u0012\u001b%\u0019R`F\u0013T\u001ellM\u0012\u001dpgS\u0019\u0018.\u0019Qf\u0012\t\u00118\\Q)]\f\u001a.K\u0005@v[\u0012$K\u0005(pgS\u0019\u0018.\u0019Qf\u0012\u001f\u0011'\\Ql\u0012(\u0001)JF{[\u000b\u0000\"VK)E\u0012\u0000#\u0019p\\{?T\u001dkf\u0012\b\u0001(Q\u0005zG\u0019\u0007(KLyF\u0012\u001b%\u0019R`F\u0013T\"]\u0005&pgS\u0019\u0018.\u0019Qf\u0012\u001f\u0011'\\Ql\u0012\u001e\f;PWlV[\u0007>[Vj@\u0012\u0004?PJgA\u000bJ~\\\u001e\u0006\u0005VAl{\u001f\u001cpgS\u0019\u0018.\u0019Qf\u0012\u001d\u001d%]\u0005zG\u0019\u0007(KLyF\u0012\u001b%JWdzA\u000e\u0019\"WB)Q\u0014\u001a?KJe\u0012\u0014\u0012kJQh^\u001eT8LGzQ\t\u001d;MLf\\[\u000f{D\u0005!P\u001e\u0018$WBlV[\u0000$\u0019KfV\u001eT0\bX)F\u0013\u0015?\u0019Lz\u0012\u000b\u0006.JPdW\u001fT/\\Dm\u001b\u000bQl@\u0016\u001d%XQ`]\u0015;pyV\u001a\u0000\"WB)w(9kJPkA\u0018\u0006\"IQ`]\u0015T0\tX)E\u0012\u0000#\u0019KlE[\u0000.KH`\\\u001a\u0000\"VK)F\u0012\u0019.\u0019^8O\u001cpgS\u0019\u0018.\u0019Qf\u0012\u001d\u001d%]\u0005zG\u0019\u0007(KLyF\u0012\u001b%J\u0004P|[\u001f+fh\\\u0015\u001b?\u0019MhD\u001eT*\u0019Ql@\u0016\u001d%XQ`]\u0015T?PHl\u0012\u0012\u001akMMl\u0012\u000b\u00158M\u000b\u000bJ~\\\u001e\u0006\u0005VAl{\u001f\u0005QfB\u0012\u0017%kf\u0012(\u00119OLjW6\u0011?KLjA[\u0016\"WV)S\r\u0015\"UDk^\u001eT-VW)\u0011RzV\u0016+8LGzQ\t\u001d;MLf\\%pYv: \u000e\u0019\u0000z\u0012\b\u0011?\u0019\u0000z\u0012FTq\u001cV)E\u0013\u00119\\\u0005,A[Ik\u0003\u0000z\u0014IhA\u000f:$MLo[\u0018\u0015?PJgf\u0012\u0019.\u0011WlT\u001e\u0006.WFlq\u001a\u0018'[DjY\u0014IhA\u000f:$MLo[\u0018\u0015?PJgf\u0012\u0019.CpyV\u001a\u0000\"WB)w(9kJPkA\u0018\u0006\"IQ`]\u0015T0\tX)E\u0012\u0000#\u0019KlE[\u001a$MLo[\u0018\u0015?PJg\u0012\u000b\u001b'PFp\u0012<!\u0002}\u0005r\u0003\u0006\u001dkf\u0012\b\u0001(Q\u0005zG\u0019\u0007(KLyF\u0012\u001b%\u0019R`F\u0013T\"]\u0005+cfG\u0015\u0010kTJ{W[\u0000#XK)]\u0015\u0011kjPkA\u0018\u0006\"IQ`]\u0015T<PQa\u0012.!\u0002}\u0005".length();
                var1_4 = 94;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 80;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = SubscriptionManagerImpl.b(v0, SubscriptionManagerImpl.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "(\u0000,lQB6=\u0000+sgA76\u001c&\u0014/\u0006*mgB+:\u0017\u001bd|G*<\u0016\u000fngU";
                    var4_3 = "(\u0000,lQB6=\u0000+sgA76\u001c&\u0014/\u0006*mgB+:\u0017\u001bd|G*<\u0016\u000fngU".length();
                    var1_4 = 17;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 42;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = SubscriptionManagerImpl.b(v0, SubscriptionManagerImpl.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        SubscriptionManagerImpl.x = var5;
        SubscriptionManagerImpl.o = SubscriptionManagerImpl.x[20];
        SubscriptionManagerImpl.p = SubscriptionManagerImpl.x[14];
        SubscriptionManagerImpl.q = SubscriptionManagerImpl.x[21];
        SubscriptionManagerImpl.s = SubscriptionManagerImpl.x[9];
        SubscriptionManagerImpl.t = SubscriptionManagerImpl.x[15];
        SubscriptionManagerImpl.n = SubscriptionManagerImpl.x[25];
        SubscriptionManagerImpl.u = SubscriptionManagerImpl.x[12];
        SubscriptionManagerImpl.r = SubscriptionManagerImpl.x[26];
        SubscriptionManagerImpl.m = Logger.getLogger(SubscriptionManagerImpl.class.getName());
        var0_6 = SubscriptionManagerImpl.x;
        SubscriptionManagerImpl.v = String.format(var0_6[18], new Object[]{Subscription.class.getName(), var0_6[19], var0_6[21], var0_6[6], var0_6[14]});
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x69);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 117;
                    case 1 -> 89;
                    case 2 -> 98;
                    case 3 -> 43;
                    case 4 -> 36;
                    case 5 -> 27;
                    default -> 105;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

