/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.cluster.ClusterProperty
 *  com.l7tech.gateway.common.security.rbac.Role
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.gateway.common.transport.InterfaceTag
 *  com.l7tech.identity.Group
 *  com.l7tech.identity.Identity
 *  com.l7tech.identity.IdentityProvider
 *  com.l7tech.identity.User
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.Entity
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.ObjectModelException
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.objectmodel.folder.Folder
 *  com.l7tech.policy.Policy
 *  com.l7tech.util.Either
 *  com.l7tech.util.Functions
 *  com.l7tech.util.Functions$Unary
 *  org.springframework.dao.DataIntegrityViolationException
 *  org.springframework.transaction.TransactionStatus
 *  org.springframework.transaction.support.TransactionCallback
 */
package com.l7tech.server.bundling;

import com.l7tech.gateway.common.cluster.ClusterProperty;
import com.l7tech.gateway.common.security.rbac.Role;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.gateway.common.transport.InterfaceTag;
import com.l7tech.identity.Group;
import com.l7tech.identity.Identity;
import com.l7tech.identity.IdentityProvider;
import com.l7tech.identity.User;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.Entity;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.folder.Folder;
import com.l7tech.policy.Policy;
import com.l7tech.server.EntityHeaderUtils;
import com.l7tech.server.bundling.EntityBundleImporterImpl;
import com.l7tech.server.bundling.EntityMappingInstructions;
import com.l7tech.server.bundling.exceptions.IncorrectMappingInstructionsException;
import com.l7tech.server.bundling.o;
import com.l7tech.server.bundling.p;
import com.l7tech.server.bundling.q;
import com.l7tech.util.Either;
import com.l7tech.util.Functions;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

class n
implements TransactionCallback<Either<Either<DeleteException, IncorrectMappingInstructionsException>, EntityHeader>> {
    final Entity val$entity;
    final Map val$cachedPrivateKeyOperations;
    final EntityMappingInstructions val$mapping;
    final Set val$deletedPolicyIds;
    final EntityBundleImporterImpl this$0;
    private static final String[] a;

    /*
     * Ignored method signature, as it can't be verified against descriptor
     */
    n(EntityBundleImporterImpl entityBundleImporterImpl, Entity entity, Map map, EntityMappingInstructions entityMappingInstructions, Set set) {
        this.this$0 = entityBundleImporterImpl;
        this.val$entity = entity;
        this.val$cachedPrivateKeyOperations = map;
        this.val$mapping = entityMappingInstructions;
        this.val$deletedPolicyIds = set;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public Either<Either<DeleteException, IncorrectMappingInstructionsException>, EntityHeader> doInTransaction(TransactionStatus transactionStatus) {
        try {
            try {
                block45: {
                    block44: {
                        if (EntityType.SSG_KEY_ENTRY == EntityType.findTypeByEntity((Class)this.val$entity.getClass())) {
                            int n2 = this.val$entity.getId().indexOf(":");
                            if (n2 < 0) {
                                String[] stringArray = a;
                                Either either = Either.left((Object)Either.left((Object)((Object)new DeleteException(stringArray[2] + this.val$entity.getId() + stringArray[4]))));
                                return either;
                            }
                            String string = this.val$entity.getId().substring(0, n2);
                            String string2 = this.val$entity.getId().substring(n2 + 1);
                            this.val$cachedPrivateKeyOperations.put(this.val$mapping.getSourceEntityHeader(), new o(this, string, string2));
                            return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                        }
                        if (this.val$entity instanceof Identity) {
                            IdentityProvider identityProvider;
                            Goid goid = ((Identity)this.val$entity).getProviderId();
                            try {
                                identityProvider = this.this$0.identityProviderFactory.getProvider(goid);
                            }
                            catch (FindException findException) {
                                String[] stringArray = a;
                                Either either = Either.left((Object)Either.left((Object)((Object)new DeleteException(stringArray[6] + String.valueOf(goid), (Throwable)findException))));
                                transactionStatus.flush();
                                return either;
                            }
                            try {
                                try {
                                    if (this.val$entity instanceof Group) {
                                        identityProvider.getGroupManager().delete((Group)this.val$entity);
                                        return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                                    }
                                }
                                catch (FindException findException) {
                                    throw n.a(findException);
                                }
                                try {
                                    if (!(this.val$entity instanceof User)) return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                                    identityProvider.getUserManager().delete((User)this.val$entity);
                                    return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                                }
                                catch (FindException findException) {
                                    throw n.a(findException);
                                }
                            }
                            catch (DeleteException deleteException) {
                                Either either = Either.left((Object)Either.left((Object)((Object)deleteException)));
                                transactionStatus.flush();
                                return either;
                            }
                        }
                        if (this.val$entity instanceof InterfaceTag) {
                            try {
                                ClusterProperty clusterProperty = (ClusterProperty)this.this$0.clusterPropertyManager.findByUniqueName(InterfaceTag.PROPERTY_NAME);
                                HashSet hashSet = new HashSet();
                                if (clusterProperty == null) return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                                hashSet.addAll(InterfaceTag.parseMultiple((String)clusterProperty.getValue()));
                                HashSet hashSet2 = new HashSet(Functions.grep(hashSet, (Functions.Unary)new p(this)));
                                this.this$0.clusterPropertyManager.putProperty(InterfaceTag.PROPERTY_NAME, InterfaceTag.toString(hashSet2));
                                return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                            }
                            catch (ObjectModelException | ParseException throwable) {
                                String[] stringArray = a;
                                Either either = Either.left((Object)Either.left((Object)((Object)new DeleteException(stringArray[9] + ((InterfaceTag)this.val$entity).getName(), throwable))));
                                transactionStatus.flush();
                                return either;
                            }
                        }
                        try {
                            if (!(this.val$entity instanceof Role) || ((Role)this.val$entity).isUserCreated().booleanValue()) break block44;
                        }
                        catch (FindException findException) {
                            throw n.a(findException);
                        }
                        String[] stringArray = a;
                        Either either = Either.left((Object)Either.right((Object)new IncorrectMappingInstructionsException(this.val$mapping, stringArray[10] + ((Role)this.val$entity).getName() + stringArray[8] + this.val$entity.getId())));
                        return either;
                    }
                    try {
                        if (!(this.val$entity instanceof Folder) || !Goid.equals((Goid)Folder.ROOT_FOLDER_ID, (Goid)((Folder)this.val$entity).getGoid())) break block45;
                    }
                    catch (FindException findException) {
                        throw n.a(findException);
                    }
                    String[] stringArray = a;
                    Either either = Either.left((Object)Either.right((Object)new IncorrectMappingInstructionsException(this.val$mapping, stringArray[7])));
                    return either;
                }
                if (this.val$entity instanceof Policy) {
                    Set<Policy> set = this.this$0.policyCache.findUsages(((Policy)this.val$entity).getGoid());
                    Policy policy = (Policy)Functions.grepFirst(set, (Functions.Unary)new q(this));
                    if (policy != null) {
                        String[] stringArray = a;
                        Either either = Either.left((Object)Either.right((Object)new IncorrectMappingInstructionsException(this.val$mapping, stringArray[11] + EntityHeaderUtils.fromEntity((Entity)policy).toStringVerbose())));
                        return either;
                    }
                    try {
                        this.this$0.policyManager.deleteWithoutValidation((Policy)this.val$entity);
                        this.val$deletedPolicyIds.add(((Policy)this.val$entity).getGoid());
                        return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                    }
                    catch (DeleteException deleteException) {
                        String[] stringArray = a;
                        Either either = Either.left((Object)Either.right((Object)new IncorrectMappingInstructionsException(this.val$mapping, stringArray[5], deleteException)));
                        transactionStatus.flush();
                        return either;
                    }
                }
                try {
                    if (this.val$entity instanceof PublishedService) {
                        try {
                            this.this$0.serviceManager.delete((PersistentEntity)((PublishedService)this.val$entity));
                            this.val$deletedPolicyIds.add(((PublishedService)this.val$entity).getPolicy().getGoid());
                            return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                        }
                        catch (DeleteException deleteException) {
                            String[] stringArray = a;
                            Either either = Either.left((Object)Either.right((Object)new IncorrectMappingInstructionsException(this.val$mapping, stringArray[0], deleteException)));
                            transactionStatus.flush();
                            return either;
                        }
                    }
                }
                catch (FindException findException) {
                    throw n.a(findException);
                }
                try {
                    this.this$0.entityCrud.delete(this.val$entity);
                    return Either.right((Object)EntityHeaderUtils.fromEntity(this.val$entity));
                }
                catch (DeleteException deleteException) {
                    String[] stringArray = a;
                    Either either = Either.left((Object)Either.right((Object)new IncorrectMappingInstructionsException(this.val$mapping, stringArray[3], deleteException)));
                    transactionStatus.flush();
                    return either;
                }
            }
            finally {
                transactionStatus.flush();
            }
        }
        catch (DataIntegrityViolationException dataIntegrityViolationException) {
            return Either.left((Object)Either.right((Object)new IncorrectMappingInstructionsException(this.val$mapping, a[1], dataIntegrityViolationException)));
        }
    }

    private static FindException a(FindException findException) {
        return findException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[12];
                var3_1 = 0;
                var2_2 = "v\nFq\u0014s.Z\u0011\u0013y\u0015?%A\u0000\u0013n\u0015!6\\\u0006V9v\nFq\u0014s.Z\u0011\u0013y\u0015?%A\u0000\u0013x\u001e')A\u001c\u001d==20E\f]z\u0003s!G\u0000\u0013q\u00198%Y\u001c\u0013r\u0005'`Z\u0003\u0013r\u00027%G+v\u0004]s\u001f'`Q\u0000_x\u00046`E\u0017Zk\u0011'%\u0015\u000eVd^s\t[\u0013Rq\u00197`^\u0000J=\u00197z\u0015\u0017v\nFq\u0014s.Z\u0011\u0013y\u0015?%A\u0000\u0013x\u001e')A\u001c/\u001bEve\u00006#A\u0000W=\u00197`B\fGuP5/G\bRiJs|^\u0000Jn\u0004<2P,W#Jo!Y\fRnN\u0017v\nFq\u0014s.Z\u0011\u0013y\u0015?%A\u0000\u0013m\u001f?)V\u001c'v\u0004]s\u001f'`S\f]yP:$P\u000bGt\u0004*`E\u0017\\k\u00197%GEDt\u0004;`\\\u0001\t=\u001dv\u0004]s\u001f'`Q\u0000_x\u00046`A\rV=\u0002</AEUr\u001c7%G\n\u0012EDt\u0004;`|\u0001\u0013\u001bv\u0004]s\u001f'`S\f]yP:.A\u0000A{\u00110%\u0015\u0011RzJs";
                var4_3 = "v\nFq\u0014s.Z\u0011\u0013y\u0015?%A\u0000\u0013n\u0015!6\\\u0006V9v\nFq\u0014s.Z\u0011\u0013y\u0015?%A\u0000\u0013x\u001e')A\u001c\u001d==20E\f]z\u0003s!G\u0000\u0013q\u00198%Y\u001c\u0013r\u0005'`Z\u0003\u0013r\u00027%G+v\u0004]s\u001f'`Q\u0000_x\u00046`E\u0017Zk\u0011'%\u0015\u000eVd^s\t[\u0013Rq\u00197`^\u0000J=\u00197z\u0015\u0017v\nFq\u0014s.Z\u0011\u0013y\u0015?%A\u0000\u0013x\u001e')A\u001c/\u001bEve\u00006#A\u0000W=\u00197`B\fGuP5/G\bRiJs|^\u0000Jn\u0004<2P,W#Jo!Y\fRnN\u0017v\nFq\u0014s.Z\u0011\u0013y\u0015?%A\u0000\u0013m\u001f?)V\u001c'v\u0004]s\u001f'`S\f]yP:$P\u000bGt\u0004*`E\u0017\\k\u00197%GEDt\u0004;`\\\u0001\t=\u001dv\u0004]s\u001f'`Q\u0000_x\u00046`A\rV=\u0002</AEUr\u001c7%G\n\u0012EDt\u0004;`|\u0001\u0013\u001bv\u0004]s\u001f'`S\f]yP:.A\u0000A{\u00110%\u0015\u0011RzJs".length();
                var1_4 = 24;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = n.a(v0, n.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0002p)\u0007kS\u0014%t+\fpB\u00142h4\u001daJ\u00143~+\f$\u0000.\u0002~2\u0005`\u0007Z.eg\raKQ5tg\u0019kK]\"hkImS\u0014(bg\u000baNZ&12\u001aaC\u0014#h}I";
                    var4_3 = "\u0002p)\u0007kS\u0014%t+\fpB\u00142h4\u001daJ\u00143~+\f$\u0000.\u0002~2\u0005`\u0007Z.eg\raKQ5tg\u0019kK]\"hkImS\u0014(bg\u000baNZ&12\u001aaC\u0014#h}I".length();
                    var1_4 = 27;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 9;
                        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++] = n.a(v0, n.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;
            }
        }
        n.a = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x3D);
        }
        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 -> 72;
                    case 1 -> 24;
                    case 2 -> 78;
                    case 3 -> 96;
                    case 4 -> 13;
                    case 5 -> 46;
                    default -> 61;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

