'use strict';
var app = angular.module('com.module.core');

app.service('MetaService', ['$injector', 'CoreService', 'Meta', 'gettextCatalog', function ($injector, CoreService, Meta, gettextCatalog) {

  this.find = function () {
    return Meta.getModels().$promise;
  };

  this.findById = function (modelName) {
    return Meta.getModelById({
      name: modelName
    }).$promise;
  };

  this.getModelInstance = function (modelName) {
    return $injector.get(modelName);
  };

  this.getModelItems = function (modelName) {
    var Model = this.getModelInstance(modelName);
    if (typeof Model.find !== 'function') {
      return false;
    } else {
      return Model.find().$promise;
    }
  };

  this.getModelItem = function (modelName, modelId) {
    var Model = this.getModelInstance(modelName);
    if (typeof Model.find !== 'function') {
      return false;
    } else {
      return Model.findOne({
        filter: {
          where: {
            id: modelId
          }
        }
      }).$promise;
    }
  };

  this.getModelFields = function (model) {
    var result = [];
    angular.forEach(model.properties, function (value, property) {
      if (property !== 'id') {
        var itemField = {
          key: property,
          type: 'input',
          templateOptions: {
            label: property,
            required: value.required !== undefined ? value.required : false,
            description: value.description !== undefined ? value.description : false
          }
        };
        console.log(value);
        result.push(itemField);
      }
    });
    return result;
  };

  this.upsert = function (modelName, item) {
    var Model = this.getModelInstance(modelName);
    return Model.upsert(item).$promise
      .then(function () {
        CoreService.toastSuccess(
          gettextCatalog.getString('Item saved'),
          gettextCatalog.getString('Your item is safe with us!')
        );
      })
      .catch(function (err) {
        CoreService.toastError(
          gettextCatalog.getString('Error saving item '),
          gettextCatalog.getString('This item could no be saved: ' + err)
        );
      }
    );
  };

  this.delete = function (modelName, modelId, successCb, cancelCb) {
    var Model = this.getModelInstance(modelName);

    CoreService.confirm(
      gettextCatalog.getString('Are you sure?'),
      gettextCatalog.getString('Deleting this cannot be undone'),
      function () {
        Model.deleteById({id: modelId}).$promise.then(function () {
          CoreService.toastSuccess(
            gettextCatalog.getString('Item deleted'),
            gettextCatalog.getString('Your item is deleted!'));
          successCb();
        }).catch(function (err) {
          CoreService.toastError(
            gettextCatalog.getString('Error deleting item'),
            gettextCatalog.getString('Your item is not deleted! ') + err);
          cancelCb();
        });
      },
      function () {
        cancelCb();
      }
    );
  };

}]);

@@ -1132,21 +1132,24 @@
 nction (
-value
+property
 , proper
@@ -1150,16 +1150,20 @@
 property
+Name
 ) %7B%0A    
@@ -1176,16 +1176,20 @@
 property
+Name
  !== 'id
@@ -1205,30 +1205,166 @@
     
-var itemField = %7B%0A    
+result.push(getModelField(propertyName, property));%0A      %7D%0A    %7D);%0A    return result;%0A  %7D;%0A%0A  function getModelField (propertyName, property) %7B%0A    return %7B%0A
     
@@ -1374,32 +1374,36 @@
 ey: property
+Name
 ,%0A      
     type: 'i
@@ -1394,31 +1394,43 @@
     
- 
-   
 type: 
-'input',%0A    
+getModelFieldType(property),%0A
     
@@ -1458,20 +1458,16 @@
         
-    
 label: p
@@ -1473,22 +1473,22 @@
 property
-,%0A    
+Name,%0A
         
@@ -1497,21 +1497,24 @@
 quired: 
-value
+property
 .require
@@ -1531,21 +1531,24 @@
 fined ? 
-value
+property
 .require
@@ -1566,20 +1566,16 @@
         
-    
 descript
@@ -1579,21 +1579,24 @@
 iption: 
-value
+property
 .descrip
@@ -1616,21 +1616,24 @@
 fined ? 
-value
+property
 .descrip
@@ -1647,34 +1647,26 @@
 false%0A      
-  
-  %7D%0A    
+%7D%0A
     %7D;%0A     
@@ -1666,81 +1666,202 @@
 ;%0A  
-      console.log(value);
+%7D%0A%0A  function getModelFieldType (property) %7B
 %0A    
-   
+var
  result
-.push(itemField);%0A      %7D
+ = 'input';%0A    if (property.meta !== undefined && property.meta.formType !== undefined) %7B%0A      result = property.meta.formType;
 %0A    %7D
-);
 %0A   
@@ -1875,25 +1875,24 @@
  result;%0A  %7D
-;
 %0A%0A  this.ups

Enhance dynamic form generation