-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectsComboBox.cs
More file actions
176 lines (149 loc) · 7.25 KB
/
ProjectsComboBox.cs
File metadata and controls
176 lines (149 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using Newtonsoft.Json.Linq;
using Npgsql;
namespace LoginTest02
{
/// <summary>
/// Represents the ComboBox
/// </summary>
internal class ProjectsComboBox : ComboBox
{
NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=geomapmaker; " +
"Password=password;Database=geomapmaker;");
private bool _isInitialized;
/// <summary>
/// Combo Box constructor
/// </summary>
public ProjectsComboBox()
{
//UpdateCombo();
DataHelper.UserLoginHandler += onUserLogin;
}
void onUserLogin()
{
_isInitialized = false;
UpdateCombo();
}
/// <summary>
/// Updates the combo box with all the items.
/// </summary>
private void UpdateCombo()
{
if (_isInitialized)
SelectedItem = null; // ItemCollection.FirstOrDefault(); //set the default item in the comboBox
if (!_isInitialized)
{
Clear();
conn.Open();
NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM geomapmaker.projects where id in (select project_id from user_project_links where user_id = " + DataHelper.userID + ") order by name asc", conn);
NpgsqlDataReader dr = command.ExecuteReader();
DataTable dT = new DataTable();
dT.Load(dr);
Add(new ComboBoxItem("<choose>")); //TODO: This is only here because I'm not sure how to have a combobox without an initial selection
foreach (DataRow row in dT.Rows)
{
//Debug.Write("Hi there \n");
//Debug.Write("{0} \n", row["name"].ToString());
Debug.WriteLine(row["name"].ToString());
Add(new ProjectComboBoxItem(row["name"].ToString(), row["notes"].ToString(), row["connection_properties"].ToString()));
}
conn.Close();
}
Enabled = true; //enables the ComboBox
SelectedItem = ItemCollection.FirstOrDefault(); //set the default item in the comboBox
}
/// <summary>
/// The on comboBox selection change event.
/// </summary>
/// <param name="item">The newly selected combo box item</param>
protected override void OnSelectionChange(ComboBoxItem item)
{
//Debug.WriteLine("item type = " + item.GetType());
if (item == null)
return;
if (string.IsNullOrEmpty(item.Text))
return;
// TODO Code behavior when selection changes.
if (item is ProjectComboBoxItem) //TODO: This is only here because I'm not sure how to have a combobox without an initial selection
{
var collProps = JObject.Parse(((ProjectComboBoxItem)item).connectionProperties);
//ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show($"connection properties: " + ((ProjectComboBoxItem)item).connectionProperties);
//ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show($"connection properties: " + collProps["database"]);
//Debug.WriteLine("type of collProps = " + collProps.GetType());
openDatabase(collProps);
}
}
private async Task openDatabase(JObject props)
{
await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
//Get Layers that are NOT Group layers and are unchecked
//var layers = MapView.Active.Map.Layers.ToList();
//MapView.Active.Map.RemoveLayers(layers);
// Opening a Non-Versioned SQL Server instance.
ArcGIS.Core.Data.DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.PostgreSQL)
{
AuthenticationMode = AuthenticationMode.DBMS,
// Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
//Instance = @"127.0.0.1",
Instance = props["instance"].ToString(),
// Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
//Database = "geomapmaker",
Database = props["database"].ToString(),
// Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
//User = "geomapmaker",
User = props["user"].ToString(),
//Password = "password",
Password = props["password"].ToString(),
//Version = "dbo.DEFAULT"
};
using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
{
DataHelper.connectionString = geodatabase.GetConnectionString();
Debug.WriteLine("DataHelper.connectionString set to " + DataHelper.connectionString);
// Use the geodatabase
/*
CIMSqlQueryDataConnection sqldc = new CIMSqlQueryDataConnection()
{
WorkspaceConnectionString = geodatabase.GetConnectionString(),
GeometryType = esriGeometryType.esriGeometryPoint,
OIDFields = "OBJECTID",
Srid = "4326",
//SqlQuery = "select * from geomapmaker2.features where id = " + DataHelper.userID + " and ST_GeometryType(geom)='ST_Point'",
Dataset = "somepoints"
};
FeatureLayer flyr = (FeatureLayer)LayerFactory.Instance.CreateLayer(sqldc, MapView.Active.Map, layerName: DataHelper.userName + "'s points");
*/
//FeatureClass fC = geodatabase.OpenDataset<FeatureClass>("somepoints");
//FeatureLayer flyr = LayerFactory.Instance.CreateFeatureLayer(fC, MapView.Active.Map);
var featureClasses = geodatabase.GetDefinitions<FeatureClassDefinition>();
foreach(FeatureClassDefinition fCD in featureClasses)
{
FeatureClass fC = geodatabase.OpenDataset<FeatureClass>(fCD.GetName());
FeatureLayer flyr = LayerFactory.Instance.CreateFeatureLayer(fC, MapView.Active.Map);
}
}
});
}
}
}