/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  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.ObjectModelException
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.objectmodel.UpdateException
 *  com.l7tech.policy.Policy
 *  com.l7tech.policy.PolicyVersion
 *  com.l7tech.util.Config
 *  com.l7tech.util.Functions
 *  com.l7tech.util.Functions$Unary
 *  jakarta.persistence.NoResultException
 *  jakarta.persistence.NonUniqueResultException
 *  jakarta.persistence.TypedQuery
 *  org.springframework.dao.DataAccessException
 *  org.springframework.orm.hibernate5.HibernateCallback
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.l7tech.server.policy;

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.ObjectModelException;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.policy.Policy;
import com.l7tech.policy.PolicyVersion;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.event.AdminInfo;
import com.l7tech.server.policy.PolicyVersionManager;
import com.l7tech.server.policy.ae;
import com.l7tech.server.policy.af;
import com.l7tech.server.policy.ag;
import com.l7tech.server.policy.at;
import com.l7tech.server.policy.au;
import com.l7tech.util.Config;
import com.l7tech.util.Functions;
import jakarta.persistence.NoResultException;
import jakarta.persistence.NonUniqueResultException;
import jakarta.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class})
public class PolicyVersionManagerImpl
extends HibernateEntityManager<PolicyVersion, EntityHeader>
implements PolicyVersionManager {
    protected static final Logger logger;
    private final Config m;
    private static final String[] n;

    public PolicyVersionManagerImpl(Config config) {
        this.m = config;
    }

    @Transactional(propagation=Propagation.SUPPORTS)
    public Class<? extends Entity> getImpClass() {
        return PolicyVersion.class;
    }

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public String getTableName() {
        return n[11];
    }

    @Override
    @Transactional(readOnly=true)
    public PolicyVersion findByPrimaryKey(Goid goid, Goid goid2) throws FindException {
        List list;
        block7: {
            HashMap<String, Goid> hashMap = new HashMap<String, Goid>();
            String[] stringArray = n;
            hashMap.put(stringArray[1], goid2);
            hashMap.put(stringArray[7], goid);
            list = this.findMatching(Collections.singletonList(hashMap));
            try {
                try {
                    if (list != null && !list.isEmpty()) break block7;
                }
                catch (FindException findException) {
                    throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
                }
                return null;
            }
            catch (FindException findException) {
                throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
            }
        }
        try {
            if (list.size() > 1) {
                throw new FindException(n[8] + String.valueOf(goid2) + n[9] + String.valueOf(goid));
            }
        }
        catch (FindException findException) {
            throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
        }
        return (PolicyVersion)list.iterator().next();
    }

    @Override
    @Transactional(readOnly=true)
    public List<PolicyVersion> findAllForPolicy(Goid goid) throws FindException {
        HashMap<String, Goid> hashMap = new HashMap<String, Goid>();
        hashMap.put(n[7], goid);
        return this.findMatching(Collections.singletonList(hashMap));
    }

    @Override
    public PolicyVersion checkpointPolicy(Policy policy, boolean bl2, boolean bl3) throws ObjectModelException {
        return this.checkpointPolicy(policy, bl2, null, bl3);
    }

    @Override
    public PolicyVersion checkpointPolicy(Policy policy, boolean bl2, String string, boolean bl3) throws ObjectModelException {
        PolicyVersion policyVersion;
        Goid goid;
        block22: {
            PolicyVersion policyVersion2;
            block23: {
                PolicyVersion policyVersion3;
                block24: {
                    block21: {
                        goid = policy.getGoid();
                        try {
                            if (Goid.isDefault((Goid)goid)) {
                                throw new IllegalArgumentException(n[2]);
                            }
                        }
                        catch (ObjectModelException objectModelException) {
                            throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
                        }
                        AdminInfo adminInfo = AdminInfo.find(false);
                        policyVersion = PolicyVersionManagerImpl.a(policy, adminInfo, bl2, bl3);
                        policyVersion.setName(string);
                        policyVersion3 = this.findActiveVersionForPolicy(goid);
                        policyVersion2 = this.findLatestRevisionForPolicy(goid);
                        try {
                            if (bl3 || policyVersion3 != null) break block21;
                        }
                        catch (ObjectModelException objectModelException) {
                            throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
                        }
                        logger.log(Level.WARNING, n[0] + String.valueOf(goid));
                        policyVersion3 = policyVersion2;
                    }
                    try {
                        try {
                            try {
                                try {
                                    try {
                                        if (policyVersion3 == null) break block22;
                                        if (policyVersion3.getXml() == null) break block23;
                                    }
                                    catch (ObjectModelException objectModelException) {
                                        throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
                                    }
                                    if (!policyVersion3.getXml().equals(policy.getXml())) break block23;
                                }
                                catch (ObjectModelException objectModelException) {
                                    throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
                                }
                                if (!bl2) break block24;
                            }
                            catch (ObjectModelException objectModelException) {
                                throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
                            }
                            if (policyVersion3.isActive()) break block24;
                        }
                        catch (ObjectModelException objectModelException) {
                            throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
                        }
                        policyVersion3.setActive(true);
                        this.update(policyVersion3);
                        this.deactivateVersions(goid, policyVersion3.getGoid());
                    }
                    catch (ObjectModelException objectModelException) {
                        throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
                    }
                }
                return policyVersion3;
            }
            try {
                if (policyVersion.getOrdinal() <= policyVersion2.getOrdinal()) {
                    policyVersion.setOrdinal(policyVersion2.getOrdinal() + 1L);
                }
            }
            catch (ObjectModelException objectModelException) {
                throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
            }
        }
        Goid goid2 = this.save(policyVersion);
        try {
            policyVersion.setGoid(goid2);
            if (bl2) {
                this.deactivateVersions(goid, goid2);
            }
        }
        catch (ObjectModelException objectModelException) {
            throw PolicyVersionManagerImpl.a((Exception)((Object)objectModelException));
        }
        this.a(goid, policyVersion);
        return policyVersion;
    }

    @Override
    public PolicyVersion findLatestRevisionForPolicy(Goid goid) {
        return (PolicyVersion)this.getHibernateTemplate().execute((HibernateCallback)new at(this, goid));
    }

    private void a(Goid goid, PolicyVersion policyVersion) throws FindException, DeleteException {
        int n2;
        List<Object> list;
        int n3;
        Goid goid2;
        block12: {
            goid2 = policyVersion.getGoid();
            n3 = this.m.getIntProperty(n[6], 20);
            list = new ArrayList<PolicyVersion>(this.findAllForPolicy(goid));
            list = Functions.grep(list, (Functions.Unary)new ae(this, goid2, goid));
            Collections.sort(list, new af(this));
            n2 = list.size();
            try {
                try {
                    if (policyVersion.isActive() || !PolicyVersionManagerImpl.a(policyVersion)) break block12;
                }
                catch (FindException findException) {
                    throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
                }
                ++n2;
            }
            catch (FindException findException) {
                throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
            }
        }
        if (n2 >= n3) {
            for (PolicyVersion policyVersion2 : list) {
                try {
                    try {
                        try {
                            if (!Goid.equals((Goid)policyVersion2.getPolicyGoid(), (Goid)goid) || Goid.equals((Goid)policyVersion2.getGoid(), (Goid)goid2)) continue;
                        }
                        catch (FindException findException) {
                            throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
                        }
                        this.delete(policyVersion2);
                        if (--n2 > n3) continue;
                        break;
                    }
                    catch (FindException findException) {
                        throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
                    }
                }
                catch (FindException findException) {
                    throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
                }
            }
        }
    }

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

    private static boolean a(PolicyVersion policyVersion) {
        boolean bl2;
        block5: {
            block4: {
                try {
                    try {
                        if (policyVersion.getName() != null && policyVersion.getName().length() >= 1) break block4;
                    }
                    catch (IllegalArgumentException illegalArgumentException) {
                        throw PolicyVersionManagerImpl.a(illegalArgumentException);
                    }
                    bl2 = true;
                    break block5;
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw PolicyVersionManagerImpl.a(illegalArgumentException);
                }
            }
            bl2 = false;
        }
        return bl2;
    }

    @Override
    public void deactivateVersions(Goid goid, Goid goid2) throws UpdateException {
        try {
            this.getHibernateTemplate().execute((HibernateCallback)new ag(this, goid, goid2));
        }
        catch (DataAccessException dataAccessException) {
            throw new UpdateException((Throwable)dataAccessException);
        }
    }

    @Override
    @Transactional(readOnly=true)
    public PolicyVersion findActiveVersionForPolicy(Goid goid) throws FindException {
        try {
            return (PolicyVersion)this.getHibernateTemplate().execute((HibernateCallback)new au(this, goid));
        }
        catch (NoResultException noResultException) {
            return null;
        }
        catch (NonUniqueResultException nonUniqueResultException) {
            throw new FindException(n[3] + String.valueOf(goid));
        }
    }

    @Override
    public PolicyVersion findPolicyVersionForPolicy(Goid goid, long l2) throws FindException {
        List list;
        String[] stringArray;
        block5: {
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            stringArray = n;
            hashMap.put(stringArray[12], goid);
            hashMap.put(stringArray[5], l2);
            list = this.findMatching(Collections.singletonList(hashMap));
            try {
                try {
                    if (list != null && !list.isEmpty()) break block5;
                }
                catch (FindException findException) {
                    throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
                }
                throw new FindException();
            }
            catch (FindException findException) {
                throw PolicyVersionManagerImpl.a((Exception)((Object)findException));
            }
        }
        if (list.size() > 1) {
            long l3 = l2;
            String string = String.valueOf(goid);
            stringArray = n;
            throw new FindException(stringArray[4] + string + stringArray[10] + l3);
        }
        return (PolicyVersion)list.iterator().next();
    }

    private static PolicyVersion a(Policy policy, AdminInfo adminInfo, boolean bl2, boolean bl3) {
        Goid goid = policy.getGoid();
        PolicyVersion policyVersion = new PolicyVersion();
        policyVersion.setPolicyGoid(goid);
        policyVersion.setActive(bl2);
        long l2 = policy.getVersion() + 1;
        if (!bl3) {
            ++l2;
        }
        policyVersion.setOrdinal(l2);
        policyVersion.setTime(System.currentTimeMillis());
        policyVersion.setUserLogin(adminInfo.login);
        policyVersion.setUserProviderGoid(adminInfo.identityProviderOid);
        policyVersion.setXml(policy.getXml());
        return policyVersion;
    }

    static /* synthetic */ PersistentEntity access$000(PolicyVersionManagerImpl policyVersionManagerImpl, TypedQuery typedQuery) {
        return policyVersionManagerImpl.getSingleResult(typedQuery);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[13];
                var3_1 = 0;
                var2_2 = "oiZ\u0014\u0004\u001faNh\u001b\u0010\u0001\u0014%\u001afX\u0002\u0001\f$\u001aq^\u0004\u001b\u0013.T']\u0019\u001aZ1UkR\u0015\u0011Z6SsSV\u000f\u0015(^'\u0004]hR\u0012/oiZ\u0014\u0004\u001faNh\u001b\u0015\u0000\u001f\"QwT\u001f\u0006\u000eaJhW\u001f\u000b\u0003aMnO\u001e\u0007\u000f5\u001af\u001b\u0000\t\u0016(^'t?,:|hN\u0018\fZ,Uu^V\u001c\u0012 T'T\u0018\rZ YsR\u0000\rZ\u0011UkR\u0015\u0011,$HtR\u0019\u0006Z6SsSV\u0018\u0015-SdB)\u000f\u0015(^:3|hN\u0018\fZ,Uu^V\u001c\u0012 T'T\u0018\rZ\u0011UkR\u0015\u0011,$HtR\u0019\u0006Z6SsSV\u0018\u0015-SdB)\u000f\u0015(^:\u0007Uu_\u001f\u0006\u001b-\u001cJhW\u001f\u000b\u0003\u0017_uH\u001f\u0007\u0014(T`v\u0017\u0010($LnH\u001f\u0007\u00142\nJhW\u001f\u000b\u0003\u0006Un_,|hN\u0018\fZ,Uu^V\u001c\u0012 T'T\u0018\rZ\u0011UkR\u0015\u0011,$HtR\u0019\u0006Z6SsSV\u000f\u0015(^:\u0011\u001afU\u0012H\n.VnX\u000f7\u001d.Sc\u0006\r\u001afU\u0012H\u00153^nU\u0017\u0004G";
                var4_3 = "oiZ\u0014\u0004\u001faNh\u001b\u0010\u0001\u0014%\u001afX\u0002\u0001\f$\u001aq^\u0004\u001b\u0013.T']\u0019\u001aZ1UkR\u0015\u0011Z6SsSV\u000f\u0015(^'\u0004]hR\u0012/oiZ\u0014\u0004\u001faNh\u001b\u0015\u0000\u001f\"QwT\u001f\u0006\u000eaJhW\u001f\u000b\u0003aMnO\u001e\u0007\u000f5\u001af\u001b\u0000\t\u0016(^'t?,:|hN\u0018\fZ,Uu^V\u001c\u0012 T'T\u0018\rZ YsR\u0000\rZ\u0011UkR\u0015\u0011,$HtR\u0019\u0006Z6SsSV\u0018\u0015-SdB)\u000f\u0015(^:3|hN\u0018\fZ,Uu^V\u001c\u0012 T'T\u0018\rZ\u0011UkR\u0015\u0011,$HtR\u0019\u0006Z6SsSV\u0018\u0015-SdB)\u000f\u0015(^:\u0007Uu_\u001f\u0006\u001b-\u001cJhW\u001f\u000b\u0003\u0017_uH\u001f\u0007\u0014(T`v\u0017\u0010($LnH\u001f\u0007\u00142\nJhW\u001f\u000b\u0003\u0006Un_,|hN\u0018\fZ,Uu^V\u001c\u0012 T'T\u0018\rZ\u0011UkR\u0015\u0011,$HtR\u0019\u0006Z6SsSV\u000f\u0015(^:\u0011\u001afU\u0012H\n.VnX\u000f7\u001d.Sc\u0006\r\u001afU\u0012H\u00153^nU\u0017\u0004G".length();
                var1_4 = 51;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = PolicyVersionManagerImpl.b(v0, PolicyVersionManagerImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "B`_\u0017\u0003\u000b\u0016DjA\r\t\u001d'\nB`_\u0017\u0003\u000b\u000e]fW";
                    var4_3 = "B`_\u0017\u0003\u000b\u0016DjA\r\t\u001d'\nB`_\u0017\u0003\u000b\u000e]fW".length();
                    var1_4 = 14;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 109;
                        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++] = PolicyVersionManagerImpl.b(v0, PolicyVersionManagerImpl.a(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;
            }
        }
        PolicyVersionManagerImpl.n = var5;
        PolicyVersionManagerImpl.logger = Logger.getLogger(PolicyVersionManagerImpl.class.getName());
    }

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

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x24);
        }
        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 -> 95;
                    case 1 -> 98;
                    case 2 -> 94;
                    case 3 -> 19;
                    case 4 -> 13;
                    case 5 -> 31;
                    default -> 36;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

