Developer forum

Forum » Development » Trigger geolocation after user import

Trigger geolocation after user import

Rasmus Andersen
Reply

Hi DW and devs, 

We are running an import of users and products from an ERP system, after the import has completed we would like to update all the users geolocations (just like the manual button from the admin interface). Is it possible to do this automatically or would you advice we write custom code to handle this situation?

Thanks :)


Replies

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Rasmus

That is currently not possible - and could also be a problem to look up geo codes in the Google API in bulks. The button on the user uses the Google JS API and makes the update in the browser. I think you need an app key to bulk look up geo codes.

In 8.7 you make something called TableScript where it is possible to manipulate values on the destination by code:

http://developer.dynamicweb.com/releases/dynamicweb-8-7.aspx#9861

Make a class that inherits Dynamicweb.Data.Integration.TableScript

 

using System.Linq;

using System.Collections.Generic;

namespace Dynamicweb.Examples.CSharp.Notifications.Integration

{

public class TableScriptSample : Dynamicweb.Data.Integration.TableScript

{

public override string ScriptingName

{

get

{

return "Sample input processing script";

}

}

public override void ProcessInputRow(Dynamicweb.Data.Integration.Mapping mapping, Dictionary<string, object> row)

{

//TODO: add you code for processing the input row

//note: the updated value should have the same type as the input value

//for example: set ProductPrice column values to some value

if (mapping.SourceTable != null)

{

Dynamicweb.Data.Integration.Column column = mapping.SourceTable.Columns.FirstOrDefault(c => c != null && c.Name.ToLower() == "ProductPrice".ToLower());

if (column != null && row.ContainsKey(column.Name))

{

row[column.Name] = 10.5;

}

}

}

}

}

Votes for this answer: 1
 
Rasmus Andersen
Reply

Hi Nicolai, 

Thanks for the quick reply, 

The method for bulk import of geolocation is already part of DW (Control Panel ->Modules -> Maps).
Would it make sense to open this method, so we could trigger it after import?

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Rasmus

I think you can - here is the code behind the button:

 

Protected Sub UpdateButton_Click(sender As Object, e As EventArgs) Handles UpdateGeoLocations.ServerClick
   Dim locations As New Generic.List(Of LocationItem)
 
   Dim ids As New Generic.List(Of Integer)
   For Each item As System.Web.UI.WebControls.ListItem In GroupIDs.Items
    If item.Selected Then
     Dim id As Integer
     Integer.TryParse(item.Value, id)
     If id > 0 Then
      ids.Add(id)
     End If
    End If
   Next
 
   Dim includeEmptyAdresses As Boolean = Base.ChkBoolean(Request("includeEmptyAdresses"))
 
   Dim groups As Modules.UserManagement.GroupCollection = Modules.UserManagement.Group.GetGroupsByID(ids)
 
   For Each group As Modules.UserManagement.Group In groups
                Dim status As Dynamicweb.SystemTools.Maps.GeoLocationStatus
                If includeEmptyAdresses Or Not String.IsNullOrEmpty(Dynamicweb.SystemTools.Maps.GetGetLocationAddress(group)) Then
                    status = Dynamicweb.SystemTools.Maps.SetGeoLocationFromGoogleMapsAPI(group)
                    If status = Dynamicweb.SystemTools.Maps.GeoLocationStatus.Updated Then
                        Try
                            group.Save()
                        Catch ex As Exception
                        End Try
                    End If
                    locations.Add(LocationItem.Create(group, status))
                End If
 
    For Each user As Modules.UserManagement.User In group.Users
                    If includeEmptyAdresses Or Not String.IsNullOrEmpty(Dynamicweb.SystemTools.Maps.GetGetLocationAddress(user)) Then
                        status = Dynamicweb.SystemTools.Maps.SetGeoLocationFromGoogleMapsAPI(user)
                        If status = Dynamicweb.SystemTools.Maps.GeoLocationStatus.Updated Then
                            Try
                                ''' User.Save fails if Username is not set
                                user.Save()
                            Catch ex As Exception
                            End Try
                        End If
                        locations.Add(LocationItem.Create(user, status))
                    End If
    Next
   Next
 
   RenderLocations(locations)
  End Sub
 
  Friend Class LocationItem
   Public Property Location As Modules.Maps.Maps.Location
   Public Property FormattedAddress As String
   Public Property Status As String
 
   Public ReadOnly Property Name As String
    Get
     If Not Location Is Nothing AndAlso Not String.IsNullOrEmpty(Location.Name) Then
      Return Location.Name
     End If
     Return "(no name)"
    End Get
   End Property
 
   Public ReadOnly Property Lat As Double
    Get
     If Not Location Is Nothing Then
      Return Location.Lat
     End If
     Return 0
    End Get
   End Property
 
   Public ReadOnly Property Lng As Double
    Get
     If Not Location Is Nothing Then
      Return Location.Lng
     End If
     Return 0
    End Get
   End Property
 
            Public Shared Function Create(group As Modules.UserManagement.Group, status As Dynamicweb.SystemTools.Maps.GeoLocationStatus) As LocationItem
                Return New LocationItem() With {
                  .Location = Modules.Maps.Maps.Location.Create(group),
                  .FormattedAddress = Dynamicweb.SystemTools.Maps.GetGetLocationAddress(group),
                  .Status = status.ToString()
                 }
            End Function
 
            Public Shared Function Create(user As Modules.UserManagement.User, status As Dynamicweb.SystemTools.Maps.GeoLocationStatus) As LocationItem
                Return New LocationItem() With {
                  .Location = Modules.Maps.Maps.Location.Create(user),
                  .FormattedAddress = Dynamicweb.SystemTools.Maps.GetGetLocationAddress(user),
                  .Status = status.ToString()
                 }
            End Function
  End Class
Votes for this answer: 1
 
Rasmus Andersen
Reply

Thank you :)

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Any changes on this functionality? Or any plans on making this standard functionality?

Maybe condition it to the existence of an API key?

Thanks,

Adrian

 
Nicolai Pedersen
Reply

No, no changes, no plans...

 

You must be logged in to post in the forum