diff --git a/src/main/java/org/hbp/mip/controllers/ModelsApi.java b/src/main/java/org/hbp/mip/controllers/ModelsApi.java index 90e6c68e6577516ce31abd5b308b0581ee8c40bc..4cd148e41f25498084dd2e95c816bdfc2bad8bd1 100644 --- a/src/main/java/org/hbp/mip/controllers/ModelsApi.java +++ b/src/main/java/org/hbp/mip/controllers/ModelsApi.java @@ -8,7 +8,6 @@ package org.hbp.mip.controllers; import io.swagger.annotations.*; import org.hbp.mip.MIPApplication; import org.hbp.mip.model.Model; -import org.hbp.mip.model.Query; import org.hbp.mip.model.User; import org.hibernate.Session; import org.springframework.http.HttpStatus; @@ -81,14 +80,6 @@ public class ModelsApi { model.setValid(true); model.setCreatedBy(user); model.setCreatedAt(new Date()); - Query q = new Query(); - Query qIn = model.getQuery(); - q.setRequest(qIn.getRequest()); - //q.setVariables(qIn.getVariables()); - q.setCovariables(qIn.getCovariables()); - q.setGrouping(qIn.getGrouping()); - q.setFilters(qIn.getFilters()); - model.setQuery(q); System.out.println(model); diff --git a/src/main/java/org/hbp/mip/model/Query.java b/src/main/java/org/hbp/mip/model/Query.java index e2bdf430eb07f4784764c7bea60c1bb903ac3703..7a81d3942a3704a8d0bcd486bf8e0abf05422147 100644 --- a/src/main/java/org/hbp/mip/model/Query.java +++ b/src/main/java/org/hbp/mip/model/Query.java @@ -26,10 +26,22 @@ public class Query { private Long id = null; private String request = null; @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @JoinTable(name = "query_variable", joinColumns = { + @JoinColumn(name = "id", nullable = false, updatable = false) }, + inverseJoinColumns = { @JoinColumn(name = "code", + nullable = false, updatable = false) }) private List<Variable> variables = new LinkedList<Variable>(); @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @JoinTable(name = "query_covariable", joinColumns = { + @JoinColumn(name = "id", nullable = false, updatable = false) }, + inverseJoinColumns = { @JoinColumn(name = "code", + nullable = false, updatable = false) }) private List<Variable> covariables = new LinkedList<Variable>(); @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @JoinTable(name = "query_grouping", joinColumns = { + @JoinColumn(name = "id", nullable = false, updatable = false) }, + inverseJoinColumns = { @JoinColumn(name = "code", + nullable = false, updatable = false) }) private List<Variable> grouping = new LinkedList<Variable>(); @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) private List<Filter> filters = new LinkedList<Filter>(); @@ -67,7 +79,7 @@ public class Query { * Covariables **/ @ApiModelProperty(value = "Covariables") - @JsonProperty("covariables") + @JsonProperty("coVariables") public List<Variable> getCovariables() { return covariables; } @@ -80,7 +92,7 @@ public class Query { * Grouping **/ @ApiModelProperty(value = "Grouping") - @JsonProperty("grouping") + @JsonProperty("groupings") public List<Variable> getGrouping() { return grouping; } diff --git a/src/main/java/org/hbp/mip/model/Variable.java b/src/main/java/org/hbp/mip/model/Variable.java index f9d5371b04f0d5ca94958b49a0584cc92fe9b527..1ad7d6e2473d79c2d0d3f6bc51c0667c2fcb9100 100644 --- a/src/main/java/org/hbp/mip/model/Variable.java +++ b/src/main/java/org/hbp/mip/model/Variable.java @@ -18,7 +18,7 @@ import java.util.List; @Table(name = "`variable`") @ApiModel(description = "") @javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringMVCServerCodegen", date = "2016-01-06T09:32:22.266Z") -@JsonIgnoreProperties(value = { "grpPath" }) +@JsonIgnoreProperties(value = { "grpPath", "queries" }) @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class Variable { @@ -40,6 +40,8 @@ public class Variable { private Group group = null; @ManyToMany(fetch = FetchType.EAGER) private List<Value> values = new LinkedList<Value>(); + @ManyToMany(fetch = FetchType.EAGER, mappedBy = "variables") + private List<Query> queries = new LinkedList<>(); public Variable() { } @@ -225,6 +227,14 @@ public class Variable { this.grpPath = grpPath; } + public List<Query> getQueries() { + return queries; + } + + public void setQueries(List<Query> queries) { + this.queries = queries; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/src/main/resources/static/frontend/app/scripts/app/articles/articles.controller.js b/src/main/resources/static/frontend/app/scripts/app/articles/articles.controller.js index 9bcc00db918d7aa84cf4f8df61a226f5713219f5..87f5625f7ade4c4a2085b404b97f8d197c5736ae 100644 --- a/src/main/resources/static/frontend/app/scripts/app/articles/articles.controller.js +++ b/src/main/resources/static/frontend/app/scripts/app/articles/articles.controller.js @@ -168,7 +168,7 @@ angular.module('chuvApp.articles').controller('ArticlesController', ['$scope','A * @returns {boolean} */ $scope.isMine = function(obj) { - return User.hasCurrent() ? obj.createdBy.id == User.current().id : false; + return User.hasCurrent() ? obj.createdBy.username == User.current().username : false; }; $scope.isAuthorized = function(article){ diff --git a/src/main/resources/static/frontend/app/scripts/app/models/model.controller.js b/src/main/resources/static/frontend/app/scripts/app/models/model.controller.js index d1bcbd3ff9461e16ac8dddbb5ca7f3b2bdb7c293..9c90d7317dccea55544eb5ba93175a6c7d9ea451 100644 --- a/src/main/resources/static/frontend/app/scripts/app/models/model.controller.js +++ b/src/main/resources/static/frontend/app/scripts/app/models/model.controller.js @@ -1,235 +1,235 @@ -/** - * Created by Michael DESIGAUD on 12/08/2015. - */ - -'use strict'; -angular.module('chuvApp.models').controller('ModelController',['$scope','$translatePartialLoader','$translate','$rootScope','Model','$stateParams','ChartUtil',"$state",'$log','User','$timeout', - function($scope,$translatePartialLoader,$translate,$rootScope,Model,$stateParams,ChartUtil,$state,$log,User,$timeout){ - - $translatePartialLoader.addPart('model'); - $translate.refresh(); - - $scope.model = {}; - $scope.query = {}; - $scope.dataset = {}; - $scope.chartConfig = { - options: { - chart: { - type: 'column', - zoomType: 'x' - } - }, - size: { - height: 480 - }, - title:{}, - subtitle:{} - }; - - /** - * load model by slug - * @param slug - */ - $scope.load = function (slug) { - Model.get({slug: slug}, function(result) { - $scope.model = result; - $scope.dataset = result.dataset; - if($stateParams.isCopy === "true"){ - $scope.model.title = "Copy of "+$scope.model.title; - } - $scope.chartConfig.title.text = $scope.model.title; - $scope.chartConfig.subtitle.text = $scope.model.description; - $scope.chartConfig.options.chart.type = $scope.model.chart.chartType; - $scope.chartConfig.xAxis = {code:$scope.model.chart.xAxis}; - $scope.chartConfig.series = _.map($scope.model.chart.chartConfigSets, function(o,idx) { - var configSet = {}; - configSet.color = o.color; - configSet.name = o.label; - configSet.code = o.code; - return configSet; - }); - $scope.$emit('event:loadModel',result); - $scope.$emit('event:searchSuccess',ChartUtil.toChartData($scope.chartConfig,result.dataset)); - $scope.executeBtnAnimate(); - $scope.executed = true; - }); - }; - - if ($stateParams.slug !== undefined) { - $scope.load($stateParams.slug); - } - - - /** - * Return true if object has been created by current user - * @param obj - * @returns {boolean} - */ - $scope.isMine = function (obj) { - return obj.id == null || obj.createdBy.id == User.current().id; - }; - - /** - * save or update model - */ - $scope.saveModel = function() { - $scope.model.chart = {chartType: $scope.chartConfig.options.chart.type}; - $scope.model.chart.xAxis = $scope.chartConfig.xAxis.code; - $scope.model.chart.svg = $scope.chartConfig.getHighcharts().getSVG(); - $scope.model.chart.chartConfigSets = _.map($scope.chartConfig.series, function (o) { - var configSet = {}; - configSet.color = o.color; - configSet.label = o.name; - configSet.code = o.code; - return configSet; - }); - - $scope.model.dataset = {code: $scope.dataset.code}; - $scope.model.query = $scope.query; - - $scope.model.title = $scope.chartConfig.title.text; - $scope.model.description = $scope.chartConfig.subtitle.text; - - if ($scope.model.slug == null) { - // save new model - Model.save($scope.model, function (model) { - $state.go('models-edit', {slug: model.slug}); - alert("Save ok"); - },function(){ - alert("Error on save!"); - }); - } else { - // save existing model - Model.update({slug: $scope.model.slug}, $scope.model, function (model) { - $state.go('models-edit', {slug: model.slug}); - alert("Save ok"); - },function(){ - alert("Error on save!"); - }); - } - }; - - /** - * Execute animation - */ - $scope.executeBtnAnimate = function () { - var searchHelpSelector = '.search-help-container'; - var searchResultSelector = '.search-result'; - var tl = new TimelineMax({ paused: true, onComplete: function () { - TweenMax.set($(searchHelpSelector), { position: 'absolute'}); - TweenMax.set($(searchResultSelector), { position: 'relative', left: 0, x: 0, y: 0 }); - } }); - tl.fromTo($(searchHelpSelector), 0.3, { scale: 1 }, { scale: 0.8 }) - .fromTo($(searchHelpSelector), 0.3, { autoAlpha: 1, x: '0%' }, { autoAlpha: 0, x: '40%' }) - .fromTo($(searchResultSelector), 0.3, { scale: 0.8, autoAlpha: 0 }, { scale: 1, autoAlpha: 1 }); - - tl.play(); - }; - - /** - * check if list contains value - * @param list - * @param value - * @returns {boolean} - */ - $scope.contains = function (list, value) { - var findFunction = function (item) { - return angular.equals(item, value) - }; - - return _.find(list, findFunction) !== undefined; - }; - - /** - * Execute a search query - */ - $scope.executeQuery = function () { - var query = angular.copy($scope.query); - //check query - var error = ""; - //The query must have at less a Variable, a Grouping and a Covariable to be sent to the API. - if (query.variables.length < 1) { - error += "The query must have at less a Variable.\n"; - } - if (query.groupings.length < 1) { - error += "The query must have at less a Grouping.\n"; - } - // check if grouping is complete - if ($scope.contains(query.groupings, {code: undefined})) { - error += "A grouping is not complete yet.\n"; - } - - if (query.coVariables.length < 1) { - error += "The query must have at less a Covariable.\n"; - } - // check if coVariables is complete - if ($scope.contains(query.coVariables, {code: undefined})) { - error += "A covariable is not complete yet.\n"; - } - - // check if filter is complete - if ($scope.contains(query.filters, {operator: '', values: []})) { - error += "A filter is not complete yet.\n"; - } - if (error.length > 0) { - alert(error); - return; - } - - // ignore filter without range values - query.filters = _.filter(query.filters, function (filter) { - return filter.values.length > 0; - }); - - $scope.executeBtnAnimate(); - - Model.executeQuery(query).success(function (queryResult) { - $scope.executed = true; - var chartData = ChartUtil.toChartData($scope.chartConfig, queryResult); - $scope.dataset = chartData.dataset; - $log.debug("Generating chart with config:", $scope.chartConfig); - $scope.$emit('event:searchSuccess', chartData); - var chart = $scope.chartConfig.getHighcharts(); - if (chart) { - for (var i = 0; i < chart.series.length; i++) { - chart.series[i].show(); - } - } - }); - - }; - - }]); - -/** - * list models - */ -angular.module('chuvApp.models').controller('ModelsController', ['$scope', '$translatePartialLoader', '$translate', '$rootScope', 'Model', 'backendUrl', '$attrs', 'WidgetService', 'User', - function ($scope, $translatePartialLoader, $translate, $rootScope, Model, backendUrl, $attrs, WidgetService, User) { - - $translatePartialLoader.addPart('model'); - $translate.refresh(); - - var params = {}; - if ($attrs.params !== undefined) { - params = angular.fromJson($attrs.params); - } - - $scope.getSvgUrl = function (model) { - return backendUrl + "/models/" + model.slug + ".svg"; - }; - - /** - * Return true if object has been created by current user - * @param obj - * @returns {boolean} - */ - $scope.isMine = function (obj) { - return obj.createdBy.id == User.current().id; - }; - - Model.getList(params).then(function (response) { - $scope.models = response.data; - WidgetService.make(); - }); - }]); +/** + * Created by Michael DESIGAUD on 12/08/2015. + */ + +'use strict'; +angular.module('chuvApp.models').controller('ModelController',['$scope','$translatePartialLoader','$translate','$rootScope','Model','$stateParams','ChartUtil',"$state",'$log','User','$timeout', + function($scope,$translatePartialLoader,$translate,$rootScope,Model,$stateParams,ChartUtil,$state,$log,User,$timeout){ + + $translatePartialLoader.addPart('model'); + $translate.refresh(); + + $scope.model = {}; + $scope.query = {}; + $scope.dataset = {}; + $scope.chartConfig = { + options: { + chart: { + type: 'column', + zoomType: 'x' + } + }, + size: { + height: 480 + }, + title:{}, + subtitle:{} + }; + + /** + * load model by slug + * @param slug + */ + $scope.load = function (slug) { + Model.get({slug: slug}, function(result) { + $scope.model = result; + $scope.dataset = result.dataset; + if($stateParams.isCopy === "true"){ + $scope.model.title = "Copy of "+$scope.model.title; + } + $scope.chartConfig.title.text = $scope.model.title; + $scope.chartConfig.subtitle.text = $scope.model.description; + $scope.chartConfig.options.chart.type = $scope.model.chart.chartType; + $scope.chartConfig.xAxis = {code:$scope.model.chart.xAxis}; + $scope.chartConfig.series = _.map($scope.model.chart.chartConfigSets, function(o,idx) { + var configSet = {}; + configSet.color = o.color; + configSet.name = o.label; + configSet.code = o.code; + return configSet; + }); + $scope.$emit('event:loadModel',result); + $scope.$emit('event:searchSuccess',ChartUtil.toChartData($scope.chartConfig,result.dataset)); + $scope.executeBtnAnimate(); + $scope.executed = true; + }); + }; + + if ($stateParams.slug !== undefined) { + $scope.load($stateParams.slug); + } + + + /** + * Return true if object has been created by current user + * @param obj + * @returns {boolean} + */ + $scope.isMine = function (obj) { + return obj.username == null || obj.createdBy.username == User.current().username; + }; + + /** + * save or update model + */ + $scope.saveModel = function() { + $scope.model.chart = {chartType: $scope.chartConfig.options.chart.type}; + $scope.model.chart.xAxis = $scope.chartConfig.xAxis.code; + $scope.model.chart.svg = $scope.chartConfig.getHighcharts().getSVG(); + $scope.model.chart.chartConfigSets = _.map($scope.chartConfig.series, function (o) { + var configSet = {}; + configSet.color = o.color; + configSet.label = o.name; + configSet.code = o.code; + return configSet; + }); + + $scope.model.dataset = {code: $scope.dataset.code}; + $scope.model.query = $scope.query; + + $scope.model.title = $scope.chartConfig.title.text; + $scope.model.description = $scope.chartConfig.subtitle.text; + + if ($scope.model.slug == null) { + // save new model + Model.save($scope.model, function (model) { + $state.go('models-edit', {slug: model.slug}); + alert("Save ok"); + },function(){ + alert("Error on save!"); + }); + } else { + // save existing model + Model.update({slug: $scope.model.slug}, $scope.model, function (model) { + $state.go('models-edit', {slug: model.slug}); + alert("Save ok"); + },function(){ + alert("Error on save!"); + }); + } + }; + + /** + * Execute animation + */ + $scope.executeBtnAnimate = function () { + var searchHelpSelector = '.search-help-container'; + var searchResultSelector = '.search-result'; + var tl = new TimelineMax({ paused: true, onComplete: function () { + TweenMax.set($(searchHelpSelector), { position: 'absolute'}); + TweenMax.set($(searchResultSelector), { position: 'relative', left: 0, x: 0, y: 0 }); + } }); + tl.fromTo($(searchHelpSelector), 0.3, { scale: 1 }, { scale: 0.8 }) + .fromTo($(searchHelpSelector), 0.3, { autoAlpha: 1, x: '0%' }, { autoAlpha: 0, x: '40%' }) + .fromTo($(searchResultSelector), 0.3, { scale: 0.8, autoAlpha: 0 }, { scale: 1, autoAlpha: 1 }); + + tl.play(); + }; + + /** + * check if list contains value + * @param list + * @param value + * @returns {boolean} + */ + $scope.contains = function (list, value) { + var findFunction = function (item) { + return angular.equals(item, value) + }; + + return _.find(list, findFunction) !== undefined; + }; + + /** + * Execute a search query + */ + $scope.executeQuery = function () { + var query = angular.copy($scope.query); + //check query + var error = ""; + //The query must have at less a Variable, a Grouping and a Covariable to be sent to the API. + if (query.variables.length < 1) { + error += "The query must have at less a Variable.\n"; + } + if (query.groupings.length < 1) { + error += "The query must have at less a Grouping.\n"; + } + // check if grouping is complete + if ($scope.contains(query.groupings, {code: undefined})) { + error += "A grouping is not complete yet.\n"; + } + + if (query.coVariables.length < 1) { + error += "The query must have at less a Covariable.\n"; + } + // check if coVariables is complete + if ($scope.contains(query.coVariables, {code: undefined})) { + error += "A covariable is not complete yet.\n"; + } + + // check if filter is complete + if ($scope.contains(query.filters, {operator: '', values: []})) { + error += "A filter is not complete yet.\n"; + } + if (error.length > 0) { + alert(error); + return; + } + + // ignore filter without range values + query.filters = _.filter(query.filters, function (filter) { + return filter.values.length > 0; + }); + + $scope.executeBtnAnimate(); + + Model.executeQuery(query).success(function (queryResult) { + $scope.executed = true; + var chartData = ChartUtil.toChartData($scope.chartConfig, queryResult); + $scope.dataset = chartData.dataset; + $log.debug("Generating chart with config:", $scope.chartConfig); + $scope.$emit('event:searchSuccess', chartData); + var chart = $scope.chartConfig.getHighcharts(); + if (chart) { + for (var i = 0; i < chart.series.length; i++) { + chart.series[i].show(); + } + } + }); + + }; + + }]); + +/** + * list models + */ +angular.module('chuvApp.models').controller('ModelsController', ['$scope', '$translatePartialLoader', '$translate', '$rootScope', 'Model', 'backendUrl', '$attrs', 'WidgetService', 'User', + function ($scope, $translatePartialLoader, $translate, $rootScope, Model, backendUrl, $attrs, WidgetService, User) { + + $translatePartialLoader.addPart('model'); + $translate.refresh(); + + var params = {}; + if ($attrs.params !== undefined) { + params = angular.fromJson($attrs.params); + } + + $scope.getSvgUrl = function (model) { + return backendUrl + "/models/" + model.slug + ".svg"; + }; + + /** + * Return true if object has been created by current user + * @param obj + * @returns {boolean} + */ + $scope.isMine = function (obj) { + return obj.createdBy.username == User.current().username; + }; + + Model.getList(params).then(function (response) { + $scope.models = response.data; + WidgetService.make(); + }); + }]); diff --git a/src/main/resources/static/frontend/app/scripts/app/mydata/mydata.controller.js b/src/main/resources/static/frontend/app/scripts/app/mydata/mydata.controller.js index 95f4191214e828f9a3e6210d583dd0551ee72fb5..b6e2161a95916f8ae1f4d6c00ff0e1e9286e5b7c 100644 --- a/src/main/resources/static/frontend/app/scripts/app/mydata/mydata.controller.js +++ b/src/main/resources/static/frontend/app/scripts/app/mydata/mydata.controller.js @@ -1,119 +1,119 @@ -/** - * Created by Michael DESIGAUD on 08/09/2015. - */ -'use strict'; - -angular.module('chuvApp.mydata').controller('MyDataController', ['$scope', '$translatePartialLoader', '$translate','Article','backendUrl','Model','$q','ChartUtil','$timeout','ModalUtil','WidgetService','$stateParams','User', - function ($scope, $translatePartialLoader, $translate,Article,backendUrl,Model,$q,ChartUtil,$timeout,ModalUtil,WidgetService,$stateParams,User) { - $translatePartialLoader.addPart('mydata'); - $translate.refresh(); - - $scope.rows = []; - - $scope.isMyDataScope = $stateParams.scope === "mydata"; - - $scope.init = function(){ - var paramsArticle = {team:$scope.isMyDataScope ? 0 : 1, own:!$scope.isMyDataScope ? 0 : 1}; - if(!$scope.isMyDataScope){ - paramsArticle.status = "published"; - } - - var paramsModel = {team:$scope.isMyDataScope ? 0 : 1, own:!$scope.isMyDataScope ? 0 : 1}; - if(!$scope.isMyDataScope){ - paramsModel.valid = "1"; - } - - $q.all([Article.query(paramsArticle).$promise, - Model.query(paramsModel).$promise]).then(function(data){ - var articles = data[0]; - var models = data[1]; - - if(!models && articles){ - angular.forEach(articles,function(article){ - $scope.rows.push({type: 'A', data: article, size: 2, row: index + 1, col: 2}); - }); - } else { - var promises = []; - angular.forEach(models, function (model) { - promises.push($scope.getModel(model.slug)); - }); - - $q.all(promises).then(function (modelsData) { - angular.forEach(modelsData,function(model,index){ - $scope.rows.push({type: 'M', data: model, size: 1, row: index+1, col: 1}); - if(articles[index]) { - $scope.rows.push({type: 'A', data: articles[index], size: 2, row: index + 1, col: 2}); - } - }); - if(articles.length > modelsData.length){ - angular.forEach(articles.slice(modelsData.length),function(article,index){ - $scope.rows.push({type: 'A', data: article, size: 2, row: index + 1, col: 2}); - }); - } - $scope.refresh(); - }); - } - }); - }; - - $scope.getModel = function(slug){ - var deferred = $q.defer(); - Model.get({slug:slug}).$promise.then(function(model){ - model.chartConfig = { - options: { - chart: { - zoomType: 'x', - height:'280' - } - }, - title:{}, - subtitle:{} - }; - model.chartConfig.title.text = model.title; - model.chartConfig.subtitle.text = model.description; - model.chartConfig.options.chart.type = model.chart.chartType; - model.chartConfig.xAxis = {code:model.chart.xAxis}; - model.chartConfig.series = _.map(model.chart.chartConfigSets, function(o) { - var configSet = {}; - configSet.color = o.color; - configSet.name = o.label; - configSet.code = o.code; - return configSet; - }); - ChartUtil.toChartData(model.chartConfig,model.dataset); - deferred.resolve(model); - }); - return deferred.promise; - }; - - $scope.init(); - - /** - * Return true if object has been created by current user - * @param obj - * @returns {boolean} - */ - $scope.isMine = function(obj) { - return obj.createdBy.id == User.current().id; - }; - - $scope.refresh = function(){ - $timeout(function(){ - $("div.gridster").gridster({ - namespace: 'div.gridster', - widget_selector: "div.grid-item", - widget_margins: [10, 10], - widget_base_dimensions: [340, 360], - max_cols : 1, - max_rows : 1 - }).data('gridster'); - $scope.$broadcast('highchartsng.reflow'); - WidgetService.make(); - },500); - }; - - $scope.showArticleModal = function(article){ - ModalUtil.showModal($scope,article); - }; - }] -); +/** + * Created by Michael DESIGAUD on 08/09/2015. + */ +'use strict'; + +angular.module('chuvApp.mydata').controller('MyDataController', ['$scope', '$translatePartialLoader', '$translate','Article','backendUrl','Model','$q','ChartUtil','$timeout','ModalUtil','WidgetService','$stateParams','User', + function ($scope, $translatePartialLoader, $translate,Article,backendUrl,Model,$q,ChartUtil,$timeout,ModalUtil,WidgetService,$stateParams,User) { + $translatePartialLoader.addPart('mydata'); + $translate.refresh(); + + $scope.rows = []; + + $scope.isMyDataScope = $stateParams.scope === "mydata"; + + $scope.init = function(){ + var paramsArticle = {team:$scope.isMyDataScope ? 0 : 1, own:!$scope.isMyDataScope ? 0 : 1}; + if(!$scope.isMyDataScope){ + paramsArticle.status = "published"; + } + + var paramsModel = {team:$scope.isMyDataScope ? 0 : 1, own:!$scope.isMyDataScope ? 0 : 1}; + if(!$scope.isMyDataScope){ + paramsModel.valid = "1"; + } + + $q.all([Article.query(paramsArticle).$promise, + Model.query(paramsModel).$promise]).then(function(data){ + var articles = data[0]; + var models = data[1]; + + if(!models && articles){ + angular.forEach(articles,function(article){ + $scope.rows.push({type: 'A', data: article, size: 2, row: index + 1, col: 2}); + }); + } else { + var promises = []; + angular.forEach(models, function (model) { + promises.push($scope.getModel(model.slug)); + }); + + $q.all(promises).then(function (modelsData) { + angular.forEach(modelsData,function(model,index){ + $scope.rows.push({type: 'M', data: model, size: 1, row: index+1, col: 1}); + if(articles[index]) { + $scope.rows.push({type: 'A', data: articles[index], size: 2, row: index + 1, col: 2}); + } + }); + if(articles.length > modelsData.length){ + angular.forEach(articles.slice(modelsData.length),function(article,index){ + $scope.rows.push({type: 'A', data: article, size: 2, row: index + 1, col: 2}); + }); + } + $scope.refresh(); + }); + } + }); + }; + + $scope.getModel = function(slug){ + var deferred = $q.defer(); + Model.get({slug:slug}).$promise.then(function(model){ + model.chartConfig = { + options: { + chart: { + zoomType: 'x', + height:'280' + } + }, + title:{}, + subtitle:{} + }; + model.chartConfig.title.text = model.title; + model.chartConfig.subtitle.text = model.description; + model.chartConfig.options.chart.type = model.chart.chartType; + model.chartConfig.xAxis = {code:model.chart.xAxis}; + model.chartConfig.series = _.map(model.chart.chartConfigSets, function(o) { + var configSet = {}; + configSet.color = o.color; + configSet.name = o.label; + configSet.code = o.code; + return configSet; + }); + ChartUtil.toChartData(model.chartConfig,model.dataset); + deferred.resolve(model); + }); + return deferred.promise; + }; + + $scope.init(); + + /** + * Return true if object has been created by current user + * @param obj + * @returns {boolean} + */ + $scope.isMine = function(obj) { + return obj.createdBy.username == User.current().username; + }; + + $scope.refresh = function(){ + $timeout(function(){ + $("div.gridster").gridster({ + namespace: 'div.gridster', + widget_selector: "div.grid-item", + widget_margins: [10, 10], + widget_base_dimensions: [340, 360], + max_cols : 1, + max_rows : 1 + }).data('gridster'); + $scope.$broadcast('highchartsng.reflow'); + WidgetService.make(); + },500); + }; + + $scope.showArticleModal = function(article){ + ModalUtil.showModal($scope,article); + }; + }] +);