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

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

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);
                }




            }
        }

        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 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  {
            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;
//        }

    }

    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 - 2025 Weber Informatics LLC | Privacy Policy