Ver Fonte

planned odoo engine for traduce objects

robert2206 há 8 anos atrás
pai
commit
3da621bce5

+ 1 - 0
package.json

@@ -33,6 +33,7 @@
   },
   "devDependencies": {
     "@ionic/app-scripts": "1.0.0",
+    "@types/node": "^7.0.5",
     "typescript": "2.0.9"
   },
   "cordovaPlugins": [

+ 6 - 12
src/models/product.ts

@@ -1,16 +1,10 @@
-function OdooType (type: string) {
-    return function (target: Function) {
-        target.prototype.getRemoteModel = function () {
-            return type;
-        }
+import { OdooModel } from "../odoo-engine/decorators/model";
+import { OdooField } from "../odoo-engine/decorators/field";
+import { FieldTypes } from "../odoo-engine/types/field";
 
-        target.prototype.getLocalModel = function () {
-            return type.replace(/./g, '_');
-        }
-    }
-}
-
-@OdooType('product.template')
+@OdooModel('product.template')
 export class Product {
 
+    @OdooField(FieldTypes.CHAR)
+    name: string;
 }

+ 27 - 0
src/odoo-engine/collections/field-metadata.ts

@@ -0,0 +1,27 @@
+import { ModelMetadataCollection } from "../collections/model-metadata";
+
+export class FieldMetadataCollection<T extends { target?: Function | string, propertyName?: string }> extends ModelMetadataCollection<T> {
+
+    /**
+     *
+     */
+    filterRepeatedMetadatas(existsMetadatas: T[]): this {
+        return this.filter(metadata => {
+            return !existsMetadatas.find(fieldDocument => fieldDocument.propertyName === metadata.propertyName);
+        });
+    }
+
+    /**
+     *
+     */
+    findByProperty(propertyName: string): T {
+        return this.items.find(item => item.propertyName === propertyName);
+    }
+
+    /**
+     *
+     */
+    hasWithProperty(propertyName: string) {
+        return !!this.findByProperty(propertyName);
+    }
+}

+ 74 - 0
src/odoo-engine/collections/model-metadata.ts

@@ -0,0 +1,74 @@
+export class ModelMetadataCollection<T extends { target?: Function | string }> {
+
+    protected items: T[] = [];
+
+    /**
+     *
+     */
+    get length() {
+        return this.items.length;
+    }
+
+    /**
+     *
+     */
+    filter(callbackFunction: (value: T, index: number, array: T[]) => boolean, thisArg?: any): this {
+        const collection = new (<any>this.constructor)();
+        this.items.filter(callbackFunction).forEach(metadata => collection.add(metadata));
+        return collection;
+    }
+
+    /**
+     *
+     */
+    filterByTarget(singleClass?: Function | string) {
+        if (!singleClass) {
+            return new (<any>this.constructor)();
+        }
+
+        return this.filterByTargets([singleClass]);
+    }
+
+    /**
+     *
+     */
+    filterByTargets(classes: Array<Function|string>): this {
+        return this.filter(metadata => { 
+            if (!metadata.target) {
+                return false;
+            }
+
+            return classes.indexOf(metadata.target) !== -1;
+        });
+    }
+
+    /**
+     *
+     */
+    add(metadata: T, checkDuplicateTargets = false): void {
+        if (checkDuplicateTargets) {
+            if (!metadata.target || !(metadata.target instanceof Function)) {
+                throw new Error("No existe éste target");
+            }
+
+            if (this.hasWithTarget(metadata.target)) {
+                throw new Error("Éste target ya existe");
+            }
+        }
+
+        this.items.push(metadata);
+    }
+    /**
+     *
+     */
+    toArray(): Array<T>  {
+        return this.items.map(item => item);
+    }
+
+    /**
+     *
+     */
+    private hasWithTarget(constructor: Function): boolean {
+        return !!this.items.find(metadata => metadata.target === constructor);
+    }
+}

+ 48 - 0
src/odoo-engine/decorators/field.ts

@@ -0,0 +1,48 @@
+import { getMetadataStorage } from "../utils/metadata-storage";
+import { FieldType, FieldTypes } from "../types/field";
+import { OdooFieldMetadata } from "../metadata/field-metadata";
+
+/**
+ *
+ */
+export function OdooField(): Function;
+
+/**
+ *
+ */
+export function OdooField(fieldName: string): Function;
+
+/**
+ *
+ */
+export function OdooField(type: FieldType): Function;
+
+/**
+ *
+ */
+export function OdooField(fieldName: string, type: FieldType);
+
+/**
+ *
+ */
+export function OdooField(fieldName?: string, type?: FieldType): Function {
+    return function (object: Object, propertyName: string) {
+        if (!name) {
+            fieldName = propertyName;
+        }
+
+        if (!type) {
+            const typeMetadata = Reflect && (Reflect as any).getMetadata ? (Reflect as any).getMetadata("design:type", object, propertyName) : undefined;
+            if (typeMetadata) {
+                type = FieldTypes.determineTypeFromFunction(typeMetadata);
+            }
+        }
+
+        const args: OdooFieldMetadata = {
+            target: object.constructor,
+            fieldName: fieldName
+        }
+
+        getMetadataStorage().fields.add(args);
+    }
+}

+ 16 - 0
src/odoo-engine/decorators/model.ts

@@ -0,0 +1,16 @@
+import { getMetadataStorage } from "../utils/metadata-storage";
+import { OdooModelMetadata } from "../metadata/model-metadata";
+
+/**
+ *
+ */
+export function OdooModel(modelName: string) {
+    return function (target: Function) {
+        const args: OdooModelMetadata = {
+            target: target,
+            modelName: modelName
+        }
+
+        getMetadataStorage().models.add(args);
+    }
+};

+ 5 - 0
src/odoo-engine/metadata/field-metadata.ts

@@ -0,0 +1,5 @@
+export interface OdooFieldMetadata {
+
+    readonly target: Function | string;
+    readonly fieldName: string;
+}

+ 10 - 0
src/odoo-engine/metadata/metadata-storage.ts

@@ -0,0 +1,10 @@
+import { ModelMetadataCollection } from "../collections/model-metadata";
+import { FieldMetadataCollection } from "../collections/field-metadata";
+import { OdooModelMetadata } from "./model-metadata";
+import { OdooFieldMetadata } from "./field-metadata";
+
+export class MetadataStorage {
+
+    readonly models = new ModelMetadataCollection<OdooModelMetadata>();
+    readonly fields = new FieldMetadataCollection<OdooFieldMetadata>();
+}

+ 5 - 0
src/odoo-engine/metadata/model-metadata.ts

@@ -0,0 +1,5 @@
+export interface OdooModelMetadata {
+
+    readonly target: Function | string;
+    readonly modelName: string;
+}

+ 47 - 0
src/odoo-engine/types/field.ts

@@ -0,0 +1,47 @@
+export type FieldType = "char"|"text"|"html"|"date"|"datetime"|"integer"|"float"|"boolean"|"binary"|"selection"|"one2many"|"many2one"|"many2many";
+
+export abstract class FieldTypes {
+
+    static CHAR: FieldType = "char";
+    static TEXT: FieldType = "text";
+    static HTML: FieldType = "html";
+    static DATE: FieldType = "date";
+    static DATETIME: FieldType = "datetime";
+    static INTEGER: FieldType = "integer";
+    static FLOAT: FieldType = "float";
+    static BOOLEAN: FieldType = "boolean";
+    static BINARY: FieldType = "binary";
+    static SELECTION: FieldType = "selection";
+    static ONE2MANY: FieldType = "one2many";
+    static MANY2ONE: FieldType = "many2one";
+    static MANY2MANY: FieldType = "many2many";
+
+    /**
+     *
+     */
+    static determineTypeFromFunction(type: Function): FieldType {
+        if (type instanceof Date) {
+            return FieldTypes.DATETIME;
+        } else if (type instanceof Function) {
+            const typeName = (<any>type).name.toLowerCase();
+            switch (typeName) {
+                case "char":
+                    return FieldTypes.CHAR;
+                case "text":
+                    return FieldTypes.TEXT;
+                case "html":
+                    return FieldTypes.HTML;
+                case "integer":
+                    return FieldTypes.INTEGER;
+                case "float":
+                    return FieldTypes.FLOAT;
+                case "boolean":
+                    return FieldTypes.BOOLEAN;
+                case "binary":
+                    return FieldTypes.BINARY;
+            }
+        }
+
+        throw new Error(`El tipo ${type} no puede ser determinado`);
+    }
+}

+ 14 - 0
src/odoo-engine/utils/metadata-storage.ts

@@ -0,0 +1,14 @@
+import { MetadataStorage } from "../metadata/metadata-storage";
+
+/**
+ *
+ */
+export function getMetadataStorage(): MetadataStorage {
+    const globalScope: any = global;
+
+    if (!globalScope.odooMetadataStorage) {
+        globalScope.odooMetadataStorage = new MetadataStorage();
+    }
+
+    return globalScope.odooMetadataStorage;
+}

+ 24 - 24
tsconfig.json

@@ -1,26 +1,26 @@
 {
-  "compilerOptions": {
-    "allowSyntheticDefaultImports": true,
-    "declaration": false,
-    "emitDecoratorMetadata": true,
-    "experimentalDecorators": true,
-    "lib": [
-      "dom",
-      "es2015"
-    ],
-    "module": "es2015",
-    "moduleResolution": "node",
-    "sourceMap": true,
-    "target": "es5"
-  },
-  "include": [
-    "src/**/*.ts"
-  ],
-  "exclude": [
-    "node_modules"
-  ],
-  "compileOnSave": false,
-  "atom": {
-    "rewriteTsconfig": false
-  }
+  	"compilerOptions": {
+    	"allowSyntheticDefaultImports": true,
+    	"declaration": false,
+    	"emitDecoratorMetadata": true,
+    	"experimentalDecorators": true,
+    	"lib": [
+      		"dom",
+      		"es2015"
+    	],
+    	"module": "es2015",
+    	"moduleResolution": "node",
+    	"sourceMap": true,
+    	"target": "es5"
+  	},
+  	"include": [
+    	"src/**/*.ts"
+  	],
+  	"exclude": [
+    	"node_modules"
+  	],
+  	"compileOnSave": false,
+ 	"atom": {
+    	"rewriteTsconfig": false
+  	}
 }