All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Util.DependencyHelper Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Util;

import com.alachisoft.ncache.common.protobuf.DependencyProtocol;
import com.alachisoft.ncache.common.protobuf.DependencyProtocol.Dependency;
import com.alachisoft.ncache.runtime.dependencies.*;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;
import com.alachisoft.ncache.runtime.util.HelperFxn;
import com.alachisoft.ncache.runtime.util.NCDateTime;
import com.google.protobuf.ByteString;
import microsoft.sql.DateTimeOffset;
import tangible.RefObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.*;


public class DependencyHelper {

    public static CacheDependency getCacheDependency(Dependency dependency) throws IllegalArgumentException, OperationFailedException {
        CacheDependency cacheDependency = null;
        RefObject temp_CacheDependency = new RefObject<>(cacheDependency);
        if (dependency == null) {
            return null;
        }
        if (dependency.getKeyDepList().size() > 0) {
            for (int i = 0; i < dependency.getKeyDepList().size(); i++) {
                addToDependency(temp_CacheDependency,new KeyDependency(dependency.getKeyDepList().get(i).getKeysList(), new NCDateTime(dependency.getKeyDepList().get(i).getStartAfter()).getDate(), KeyDependencyType.forValue(dependency.getKeyDepList().get(i).getKeyDependencyType())));
            }
        }



        if (dependency.getFileDepList().size() > 0) {
            for (int i = 0; i < dependency.getFileDepList().size(); i++) {
                 addToDependency(temp_CacheDependency, new com.alachisoft.ncache.runtime.dependencies.FileDependency(dependency.getFileDepList().get(i).getFilePathsList(), new NCDateTime(dependency.getFileDepList().get(i).getStartAfter()).getDate()));
             }
        }

        if (dependency.getOleDbDepList().size() > 0) {
            for (int i = 0; i < dependency.getOleDbDepList().size(); i++) {
                addToDependency(temp_CacheDependency,DBDependencyFactory.createOleDbCacheDependency(dependency.getOleDbDepList().get(i).getConnectionString(), dependency.getOleDbDepList().get(i).getDbCacheKey()));
             }
        }
        if (dependency.getSql7DepList().size() > 0) {
            for (int i = 0; i < dependency.getSql7DepList().size(); i++) {
                addToDependency(temp_CacheDependency,DBDependencyFactory.createSqlCacheDependency(dependency.getSql7DepList().get(i).getConnectionString(), dependency.getSql7DepList().get(i).getDbCacheKey()));
            }
        }
        if (dependency.getOracleDepCount() > 0) {
            for (int i = 0; i < dependency.getOracleDepCount(); i++) {
                HashMap parameters = new HashMap();
                for (int pc = 0; pc < dependency.getOracleDep(i).getParamCount(); pc++) {
                    OracleCmdParams commandParam = new OracleCmdParams();

                    commandParam.setDirection(OracleParameterDirection.getOracleParameterDirection(dependency.getOracleDep(i).getParam(pc).getCmdParam().getDirection()));

                    commandParam.setType(OracleCmdParamsType.getOracleCmdParamsType(dependency.getOracleDep(i).getParam(pc).getCmdParam().getDbType()));
                    commandParam.setValue(dependency.getOracleDep(i).getParam(pc).getCmdParam().getValue());

                    parameters.put(dependency.getOracleDep(i).getParam(pc).getKey(), commandParam);
                }
                OracleCacheDependency oraDep = new OracleCacheDependency(dependency.getOracleDep(i).getConnectionString(),
                        dependency.getOracleDep(i).getQuery(),
                        OracleCommandType.forValue(dependency.getOracleDep(i).getCommandType()),
                        parameters);

                addToDependency(temp_CacheDependency,oraDep);
            }
        }
        if (dependency.getYukonDepCount() > 0)
        {
            for (int i = 0; i < dependency.getYukonDepCount(); i++)
            {
                HashMap parameters = new HashMap();
                for (int pc = 0; pc < dependency.getYukonDep(i).getParamCount(); pc++)
                {
                    DependencyProtocol.YukonCommandParam yukonParam = dependency.getYukonDep(i).getParam(pc).getCmdParam();
                    SqlCmdParams commandParam = new SqlCmdParams();

                    commandParam.setCompareInfo(SqlCmpOptions.getSqlCmpOptions(yukonParam.getCmpOptions()));
                    //commandParam.setParameterDbType(CmdParamsDbType.getCmdParamsDbType(yukonParam.getDbType()));
                    commandParam.setSqlParameterDirection(SqlParamDirection.forValue(yukonParam.getDirection()));
                    commandParam.setIsNullable(yukonParam.getIsNullable());
                    commandParam.setLocaleId(yukonParam.getLocaleId());
                    commandParam.setoffset(yukonParam.getOffset());
                    commandParam.setprecision((byte) yukonParam.getPrecision());
                    commandParam.setScale((byte) yukonParam.getScale());
                    commandParam.setSize(yukonParam.getSize());
                    commandParam.setSourceColumn(yukonParam.getSourceColumn());
                    commandParam.setSourceColumnNullMapping(yukonParam.getSourceColumnNull());
                    commandParam.setSqlValue(yukonParam.getSqlValue());
                    //TODO: needs a setter
                    //commandParam.SrcVersion = (System.Data.DataRowVersion)yukonParam.version;
                    commandParam.setCmdParamsType(CmdParamsType.forValue(yukonParam.getTypeId()));
                    commandParam.setTypeName(yukonParam.getTypeName());
                    commandParam.setUdtTypeName(yukonParam.getUdtTypeName());
                    //commandParam.setValue(yukonParam.getValue());

                    if (!yukonParam.getNullValueProvided())
                    {
                        String val = yukonParam.getValue();
                        if (commandParam.getCmdParamsType() == CmdParamsType.Binary || commandParam.getCmdParamsType() == CmdParamsType.VarBinary || /*
                                 * commandParam.getCmdParamsType() == CmdParamsType.Image ||
                                 */ commandParam.getCmdParamsType() == CmdParamsType.Timestamp)
                        {
                            try
                            {
                                commandParam.setValue(val.getBytes("UTF-8"));
                            }
                            catch (UnsupportedEncodingException ex)
                            {
                            }
                        }
                        else if (commandParam.getCmdParamsType() == CmdParamsType.DateTime || commandParam.getCmdParamsType() == CmdParamsType.DateTime2
                                || commandParam.getCmdParamsType() == CmdParamsType.SmallDateTime)
                        {
                            java.sql.Timestamp timeStamp = new java.sql.Timestamp(Long.valueOf(val));
                            commandParam.setValue(timeStamp);
                        }
                        else if (commandParam.getCmdParamsType() == CmdParamsType.Date)
                        {
                            java.sql.Date date = new java.sql.Date(Long.valueOf(val));
                            commandParam.setValue(date);
                        }
                        else if (commandParam.getCmdParamsType() == CmdParamsType.Time)
                        {
                            java.sql.Time time = new java.sql.Time(Long.valueOf(val));
                            commandParam.setValue(time);
                        }
                        else if (commandParam.getCmdParamsType() == CmdParamsType.DateTimeOffset)
                        {
                            String[] dateOffset = val.split(",");
                            DateTimeOffset offset = DateTimeOffset.valueOf(Timestamp.valueOf(dateOffset[0]), Integer.valueOf(dateOffset[1]));
                            commandParam.setValue(offset);
                        }
                        else
                        {
                            commandParam.setValue(val);
                        }
                    }
                    parameters.put(dependency.getYukonDep(i).getParam(i).getKey(), commandParam);
                }

                SqlCacheDependency yukonDep = new SqlCacheDependency(dependency.getYukonDep(i).getConnectionString(),
                        dependency.getYukonDep(i).getQuery(),
                        SqlCommandType.getSqlCommandType(dependency.getYukonDep(i).getCommandType()),
                        parameters);

                addToDependency(temp_CacheDependency, yukonDep);
            }
        }

        if(dependency.getCustomDepCount() > 0){
            for(int i=0; i < dependency.getCustomDepCount(); i++){
                HashMap parameter = new HashMap ();
                DependencyProtocol.CustomDependency customDependency= dependency.getCustomDep(i);
                for(int pc = 0; pc < customDependency.getParametersCount(); pc++){
                    parameter.put(customDependency.getParameters(pc).getName(), customDependency.getParameters(pc).getValue());
                }

                addToDependency(temp_CacheDependency, new CustomDependency(dependency.getCustomDep(i).getProviderName(), parameter));
            }
        }
        return temp_CacheDependency.argvalue;
    }

    //Basit: tangible.RefObject<~~~> used in place of ref but for now simply replacing the same code path of Dependency Helper from NCClient


    public static DependencyProtocol.Dependency getDependencyProtocol(CacheDependency cacheDependency) throws Exception {

        if (cacheDependency == null) {
            return null;
        }
        DependencyProtocol.Dependency.Builder protoDep = DependencyProtocol.Dependency.newBuilder();

        return getProtoBufDependency(cacheDependency, protoDep).build();
    }

    public static DependencyProtocol.Dependency.Builder getProtoBufDependency(CacheDependency cacheDependency, DependencyProtocol.Dependency.Builder dependency) throws OperationFailedException, UnsupportedEncodingException, IOException, java.lang.IllegalArgumentException {


        if (cacheDependency instanceof FileDependency) {
            FileDependency fileDependency = (FileDependency) cacheDependency;
            dependency.addFileDep(DependencyProtocol.FileDependency.newBuilder().addAllFilePaths(fileDependency.getFileNames()).setStartAfter(fileDependency.getStartAfterTicks()).build());
            return dependency;
        } else if (cacheDependency instanceof KeyDependency) {
            KeyDependency keydependency = (KeyDependency) cacheDependency;
            dependency.addKeyDep(DependencyProtocol.KeyDependency.newBuilder().addAllKeys(keydependency.getCacheKeys()).setStartAfter(keydependency.getStartAfterTicks()).setKeyDependencyType(keydependency.getKeyDependencyType().getValue()).build());

            return dependency;
        } else if (cacheDependency instanceof DBCacheDependency) {
            DBCacheDependency dep = (DBCacheDependency) cacheDependency;
            if (dep.getDBDependencyType() == DBDependencyType.SqlCacheDependency) {
                dependency.addSql7Dep(DependencyProtocol.Sql7Dependency.newBuilder().setConnectionString(dep.getConnectionString()).setDbCacheKey(dep.getPrimaryKey()).build());
            } else if (dep.getDBDependencyType() == DBDependencyType.OleDbCacheDependency) {
                dependency.addOleDbDep(DependencyProtocol.OleDbDependency.newBuilder().setConnectionString(dep.getConnectionString()).setDbCacheKey(dep.getPrimaryKey()).build());
            }

            return dependency;
        }

       else if (cacheDependency instanceof com.alachisoft.ncache.runtime.dependencies.SqlCacheDependency)
        {
            com.alachisoft.ncache.runtime.dependencies.SqlCacheDependency sqlDependency = (com.alachisoft.ncache.runtime.dependencies.SqlCacheDependency) cacheDependency;

            DependencyProtocol.YukonDependency.Builder builder = DependencyProtocol.YukonDependency.newBuilder()
                    .setCommandType(sqlDependency.getCommandType().getValue())
                    .setConnectionString(sqlDependency.getConnectionString())
                    .setQuery(sqlDependency.getCommandText());

            if (sqlDependency.getCommandParams() != null)
            {

                if (sqlDependency.getCommandParams() != null && sqlDependency.getCommandParams().size() > 0)
                {

                    Iterator iterator = sqlDependency.getCommandParams().keySet().iterator();
                    while (iterator.hasNext())
                    {
                        String key = iterator.next().toString();
                        SqlCmdParams cmdParam = (SqlCmdParams) sqlDependency.getCommandParams().get(key);

                        DependencyProtocol.YukonCommandParam.Builder paramBuilder = DependencyProtocol.YukonCommandParam.newBuilder();
                        paramBuilder.setDbType(cmdParam.getCmdParamsType().getValue());
                        paramBuilder.setDirection(cmdParam.getSqlParameterDirection().getValue())
                                .setIsNullable(cmdParam.getIsNullable())
                                .setLocaleId(cmdParam.getLocaleId())
                                .setOffset(cmdParam.getoffset())
                                .setPrecision(cmdParam.getPrecision())
                                .setScale(cmdParam.getScale())
                                .setSize(cmdParam.getSize())
                                .setSourceColumn(cmdParam.getSourceColumn() != null ? cmdParam.getSourceColumn().toString() : "")
                                .setSourceColumnNull(cmdParam.getSourceColumnNullMapping())
                                .setSqlValue(cmdParam.getSqlValue() != null ? cmdParam.getSqlValue().toString() : "")
                                .setVersion(cmdParam.getSourceVersion().getValue())
                                .setTypeName(cmdParam.getTypeName() != null ? cmdParam.getTypeName().toString() : "")
                                .setTypeId(cmdParam.getCmdParamsType().getValue())
                                .setUdtTypeName(cmdParam.getUdtTypeName() != null ? cmdParam.getUdtTypeName() : "")
                                .setNullValueProvided(cmdParam.getValue() == null ? true : false);

                        if (!paramBuilder.getNullValueProvided())
                        {
                            if (cmdParam.getCmdParamsType() == CmdParamsType.Binary || cmdParam.getCmdParamsType() == CmdParamsType.VarBinary || /*
                                     * cmdParam.getCmdParamsType() == CmdParamsType.Image ||
                                     */ cmdParam.getCmdParamsType() == CmdParamsType.Timestamp)
                            {
                                byte[] val = null;
                                try
                                {
                                    val = (byte[]) cmdParam.getValue();
                                    paramBuilder.setValue(new String(val, "UTF-8"));
                                }
                                catch (ClassCastException ex)
                                {
                                    throw new OperationFailedException("Expected byte[] for parameter type '" + cmdParam.getCmdParamsType() + "'");
                                }
                            }
                            else if (cmdParam.getCmdParamsType() == CmdParamsType.DateTime || cmdParam.getCmdParamsType() == CmdParamsType.DateTime2 || cmdParam.getCmdParamsType()
                                    == CmdParamsType.SmallDateTime)
                            {
                                try
                                {
                                    java.sql.Timestamp val = (java.sql.Timestamp) cmdParam.getValue();
                                    paramBuilder.setValue(Long.toString(HelperFxn.getTicks(val)));
                                }
                                catch (ClassCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'java.sql.Timestamp' for parameter type '" + cmdParam.getCmdParamsType() + "'");
                                }
                            }
                            else if (cmdParam.getCmdParamsType() == CmdParamsType.Date)
                            {
                                try
                                {
                                    java.sql.Date val = (java.sql.Date) cmdParam.getValue();
                                    paramBuilder.setValue(Long.toString(HelperFxn.getTicks(val)));
                                }
                                catch (ClassCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'java.sql.Date' for parameter type '" + cmdParam.getCmdParamsType() + "'");
                                }
                            }
                            else if (cmdParam.getCmdParamsType() == CmdParamsType.Time)
                            {
                                try
                                {
                                    java.sql.Time val = (java.sql.Time) cmdParam.getValue();
                                    paramBuilder.setValue(Long.toString(HelperFxn.getTicks(val)));
                                }
                                catch (ClassCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'java.sql.Time' for parameter type '" + cmdParam.getCmdParamsType() + "'");
                                }
                            }
                            else if (cmdParam.getCmdParamsType() == CmdParamsType.DateTimeOffset)
                            {
                                try
                                {
                                    DateTimeOffset val = (DateTimeOffset) cmdParam.getValue();
                                    paramBuilder.setValue(HelperFxn.getTicks(val.getTimestamp()) + "," + val.getMinutesOffset());
                                }
                                catch (ClassCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'microsoft.sql.DateTimeOffset' for parameter type '" + cmdParam.getCmdParamsType() + "'");
                                }
                            }
                            else
                            {
                                paramBuilder.setValue(cmdParam.getValue().toString());
                            }
                        }
                        DependencyProtocol.YukonParam param = DependencyProtocol.YukonParam.newBuilder().setKey(key).setCmdParam(paramBuilder.build()).build();
                        builder = builder.addParam(param);
                    }

                }
            }

            dependency .addYukonDep(builder.build());
            return dependency;
        }
        else if (cacheDependency instanceof OracleCacheDependency) {

            OracleCacheDependency oracleDependency = (OracleCacheDependency) cacheDependency;
            if (oracleDependency.getConnectionString() == null || oracleDependency.getConnectionString().isEmpty()) {
                throw new java.lang.IllegalArgumentException("Connection string cannot be null or empty");
            }
            if (oracleDependency.getCommandText() == null || oracleDependency.getCommandText().isEmpty()) {
                throw new java.lang.IllegalArgumentException("Command Text cannot be null or empty");
            }
            DependencyProtocol.OracleDependency.Builder oraclebuilder =
                    DependencyProtocol.OracleDependency.newBuilder().setCommandType(oracleDependency.getCommandType() != null ? oracleDependency.getCommandType().getValue() : 1).setConnectionString(oracleDependency.getConnectionString()).setQuery(oracleDependency.getCommandText());

            if (oracleDependency.getCommandParams() != null) {
                Set keyEnumeration = oracleDependency.getCommandParams().entrySet();
                Iterator iterator = keyEnumeration.iterator();
                Map.Entry entry;
                while (iterator.hasNext()) {
                    entry = (Map.Entry) iterator.next();
                    String key = (String) entry.getKey();
                    OracleCmdParams cmdParam = (OracleCmdParams) oracleDependency.getCommandParams().get(key);

                    DependencyProtocol.OracleParam param = DependencyProtocol.OracleParam.newBuilder().setKey(key).setCmdParam(DependencyProtocol.OracleCommandParam.newBuilder().setDbType(cmdParam.getType().getValue()).setDirection(cmdParam.getDirection().getValue()).setValue(cmdParam.getValue()
                            != null ? cmdParam.getValue().toString() : "").build()).build();
                    oraclebuilder = oraclebuilder.addParam(param);
                }
            }

            dependency.addOracleDep(oraclebuilder.build());

            return dependency;

        } else if (cacheDependency instanceof com.alachisoft.ncache.runtime.dependencies.ExtensibleDependency) {
            com.alachisoft.ncache.runtime.dependencies.ExtensibleDependency extDependency = (com.alachisoft.ncache.runtime.dependencies.ExtensibleDependency) cacheDependency;


            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(stream);
            out.writeObject(extDependency);
            stream.toByteArray();

            DependencyProtocol.ExtensibleDependency.Builder extensibleBuilder =
                    DependencyProtocol.ExtensibleDependency.newBuilder().setData(ByteString.copyFrom(stream.toByteArray()));

            dependency.addXtDep(extensibleBuilder.build());

            return null;
        }
        else if (cacheDependency instanceof com.alachisoft.ncache.runtime.dependencies.CustomDependency) {
            com.alachisoft.ncache.runtime.dependencies.CustomDependency customDependency = (com.alachisoft.ncache.runtime.dependencies.CustomDependency) cacheDependency;

            DependencyProtocol.CustomDependency.Builder customDependencyBuilder = DependencyProtocol.CustomDependency.newBuilder().setProviderName(customDependency.getProviderName());
            customDependencyBuilder.setProviderName(customDependency.getProviderName());

            for (Map.Entry param : customDependency.getParameters().entrySet()) {
                if (param.getKey() == null || param.getValue() == null)
                    throw new IllegalArgumentException("custom dependencies keys and values must not be null");
                DependencyProtocol.Dictionary.Builder dictionary = DependencyProtocol.Dictionary.newBuilder();
                dictionary.setName(param.getKey().toString());
                dictionary.setValue(param.getValue().toString());
                customDependencyBuilder.addParameters(dictionary);
            }
            dependency.addCustomDep(customDependencyBuilder);
            return dependency;
        }
        else {
            if(cacheDependency instanceof com.alachisoft.ncache.runtime.dependencies.AggregateCacheDependency){
                AggregateCacheDependency aggregateDependency = (AggregateCacheDependency) cacheDependency;
                for (com.alachisoft.ncache.runtime.dependencies.CacheDependency dep : aggregateDependency.getDependencies()) {

                    getProtoBufDependency(dep, dependency);
                }
            }
            return dependency;
        }
    }

    private static void addToDependency(tangible.RefObject cacheDependency, CacheDependency newDependency)
    {
        if (cacheDependency.argvalue != null)
        {
            cacheDependency.argvalue.getDependencies().add(newDependency);
        }
        else
        {
            cacheDependency.argvalue = new CacheDependency(newDependency);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy