     
     modelDf.promise
 
+  getOrCreateTypologyByName: (name, args) ->
+    typePromiseMap = args.typePromiseMap ? {}
+    typePromise = typePromiseMap[name]
+    return typePromise if typePromise
+    Logger.debug('Creating type', name)
+    typeDf = Q.defer()
+    typePromiseMap[name] = typeDf.promise
+
+    projectId = args.projectId
+    unless projectId then return Q.reject('No projectId provided')
+
+    usedColors = args.usedColors ? []
+    
+    type = Typologies.findByName(name, projectId)
+    if type
+      typeDf.resolve(type._id)
+    else
+      fillColor = Typologies.getNextAvailableColor(projectId, {exclude: usedColors})
+      usedColors.push(fillColor)
+      typologyDoc =
+        name: name
+        project: projectId
+      SchemaUtils.setParameterValue(typologyDoc, 'style.fill_color', fillColor)
+      typologyDoc = @_mapTypology(typologyDoc) ? typologyDoc
+      Typologies.insert typologyDoc, (err, typeId) ->
+        if err
+          typeDf.reject(err)
+        else
+          typeDf.resolve(typeId)
+    typeDf.promise
+
   _mapEntity: (doc) -> doc
   _mapTypology: (doc) -> doc
   _mapGeometry: (geom) -> geom

                modelDf.promise.then(incrementC3mlCount)

                if typeName
                  getOrCreateTypologyByName(typeName).then Meteor.bindEnvironment (typeId) ->
                    createEntityArgs.typeId = typeId
                    createEntity()
                else
                  createEntity()
              

      # A map of parent IDs to a map of children names to their IDs.
      childrenNameMap = {}

      Logger.info('Inserting ' + c3mls.length + ' c3mls...')

      insertedCount = 0
