Esempio n. 1
0
        private static void DoWork(Options options)
        {
            Uri           serverURL;
            string        appname;
            string        appid;
            bool          openSheets;
            string        virtualProxy;
            QlikSelection mySelection = null;

            ILocation remoteQlikSenseLocation = null;

            try
            {
                if (options.Debug)
                {
                    DEBUG_MODE = true;
                    Print(LogLevel.Debug, "Debug logging enabled.");
                }
                Print(LogLevel.Debug, "setting parameter values in main");
                serverURL    = new Uri(options.Server);
                appname      = options.AppName;
                appid        = options.AppID;
                virtualProxy = !string.IsNullOrEmpty(options.VirtualProxy) ? options.VirtualProxy : "";
                openSheets   = options.FetchObjects;
                if (options.SelectionField != null)
                {
                    mySelection             = new QlikSelection();
                    mySelection.fieldname   = options.SelectionField;
                    mySelection.fieldvalues = options.SelectionValues.Split(',');
                }
                //TODO need to validate the params ideally

                Print(LogLevel.Debug, "setting remoteQlikSenseLocation");;

                ////connect to the server (using windows credentials
                QlikConnection.Timeout = Int32.MaxValue;
                var d = DateTime.Now;

                remoteQlikSenseLocation = Qlik.Engine.Location.FromUri(serverURL);


                Print(LogLevel.Debug, "validating http(s) and virtual proxy");;
                if (virtualProxy.Length > 0)
                {
                    remoteQlikSenseLocation.VirtualProxyPath = virtualProxy;
                }
                bool isHTTPs = false;
                if (serverURL.Scheme == Uri.UriSchemeHttps)
                {
                    isHTTPs = true;
                }
                remoteQlikSenseLocation.AsNtlmUserViaProxy(isHTTPs, null, false);

                Print(LogLevel.Debug, "starting to cache applications");
                ////Start to cache the apps
                IAppIdentifier appIdentifier = null;

                if (appid != null)
                {
                    //Open up and cache one app, based on app ID
                    appIdentifier = remoteQlikSenseLocation.AppWithId(appid);
                    Print(LogLevel.Debug, "got app identifier by ID");
                    LoadCache(remoteQlikSenseLocation, appIdentifier, openSheets, mySelection);
                    Print(LogLevel.Debug, "finished caching by ID");
                }
                else
                {
                    if (appname != null)
                    {
                        //Open up and cache one app
                        appIdentifier = remoteQlikSenseLocation.AppWithNameOrDefault(appname);
                        Print(LogLevel.Debug, "got app identifier by name");
                        LoadCache(remoteQlikSenseLocation, appIdentifier, openSheets, mySelection);
                        Print(LogLevel.Debug, "finished caching by name");
                    }
                    else
                    {
                        //Get all apps, open them up and cache them
                        remoteQlikSenseLocation.GetAppIdentifiers().ToList().ForEach(id => LoadCache(remoteQlikSenseLocation, id, openSheets, null));
                        Print(LogLevel.Debug, "finished caching all applications");
                    }
                }


                ////Wrap it up
                var dt = DateTime.Now - d;
                Print(LogLevel.Info, "Cache initialization complete. Total time: {0}", dt.ToString());
                remoteQlikSenseLocation.Dispose();
                Print(LogLevel.Debug, "done");

                return;
            }
            catch (UriFormatException)
            {
                Print(LogLevel.Info, "Invalid server paramater format. Format must be http[s]://host.domain.tld.");
                return;
            }
            catch (WebSocketException webEx)
            {
                if (remoteQlikSenseLocation != null)
                {
                    Print(LogLevel.Info, "Disposing remoteQlikSenseLocation");
                    remoteQlikSenseLocation.Dispose();
                }

                Print(LogLevel.Info, "Unable to connect to establish WebSocket connection with: " + options.Server);
                Print(LogLevel.Info, "Error: " + webEx.Message);

                return;
            }
            catch (TimeoutException timeoutEx)
            {
                Print(LogLevel.Info, "Timeout Exception - Unable to connect to: " + options.Server);
                Print(LogLevel.Info, "Error: " + timeoutEx.Message);

                return;
            }
            catch (Exception ex)
            {
                if (ex.Message.Trim() == "Websocket closed unexpectedly (EndpointUnavailable):")
                {
                    Print(LogLevel.Info, "Error: licenses exhausted.");
                    return;
                }
                else
                {
                    Print(LogLevel.Info, "Unexpected error.");
                    Print(LogLevel.Info, "Message: " + ex.Message);

                    return;
                }
            }
        }