OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add viewpoint GET and xml->json mapper
Oliver Günther8e5e4c1433f2
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rbindex ac5f3dba024..434fd22b675 100644--- a/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb+++ b/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb@@ -56,6 +56,8 @@ module Bcf::API::V2_1.new(model: Bcf::Issue,api_name: 'Topics').mount++ mount Bcf::API::V2_1::ViewpointsAPIendendenddiff --git a/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints_api.rbnew file mode 100644index 00000000000..64bb9b666a9--- /dev/null+++ b/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints_api.rb@@ -0,0 +1,52 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Bcf::API::V2_1+ class ViewpointsAPI < ::API::OpenProjectAPI+ resources :viewpoints do+ get &::Bcf::API::V2_1::Endpoints::Index+ .new(model: Bcf::Viewpoint,+ api_name: 'Viewpoints',+ scope: -> { @issue.viewpoints })+ .mount++ route_param :uuid, regexp: /\A[a-f0-9\-]+\z/ do+ after_validation do+ @viewpoint = @issue.viewpoints.find_by_uuid!(params[:uuid])+ end++ get &::Bcf::API::V2_1::Endpoints::Show+ .new(model: Bcf::Viewpoint,+ api_name: 'Viewpoints')+ .mount+ end+ end+ end+enddiff --git a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/single_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/single_representer.rbnew file mode 100644index 00000000000..453ef9dbd53--- /dev/null+++ b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/single_representer.rb@@ -0,0 +1,45 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Bcf::API::V2_1+ class Viewpoints::SingleRepresenter+ attr_reader :represented++ def initialize(represented)+ @represented = represented+ end++ def to_hash+ # Uses mapper to avoid saving JSON for now.+ # We will likely want a migration to move away from XML and render viewpoint JSON directly+ ::OpenProject::Bcf::BcfJson::ViewpointMapper.new(represented).result+ end+ end+enddiff --git a/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rb b/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rbnew file mode 100644index 00000000000..a24fc34d209--- /dev/null+++ b/modules/bcf/lib/open_project/bcf/bcf_json/viewpoint_mapper.rb@@ -0,0 +1,147 @@+module OpenProject::Bcf+ module BcfJson+ class ViewpointMapper+ ROOT_NODE ||= 'VisualizationInfo'.freeze++ attr_reader :resource++ def initialize(viewpoint)+ @resource = viewpoint+ end++ def result+ viewpoint_hash+ end++ def to_json+ viewpoint_hash.to_json+ end++ private++ ##+ # Retrieve the viewpoint hash without root node, if any.+ def viewpoint_hash+ @viewpoint_hash ||= begin+ # Load from XML using activesupport+ hash = Hash.from_xml(resource.viewpoint)+ hash = hash[ROOT_NODE] if hash.key?(ROOT_NODE)++ # Perform destructive transformations+ transformations.each do |m|+ send(m, hash)+ end++ hash+ end+ end++ def transformations+ %i[+ remove_keys+ transform_keys+ transform_perspective_camera+ transform_orthogonal_camera+ transform_lines+ transform_clipping_planes+ transform_selections+ transform_coloring+ transform_visibility+ ]+ end++ def remove_keys(hash)+ hash.delete 'xmlns:xsi'+ hash.delete 'xmlns:xsd'+ end++ def transform_keys(hash)+ # Underscore all keys, will make it easier to reuse more portions of the XML+ hash.deep_transform_keys!(&:underscore)+ end++ ##+ # Transform perspective_camera into json float values+ def transform_orthogonal_camera(hash)+ transform_camera hash, 'orthogonal_camera'+ end++ ##+ # Transform orthogonal_camera into json float values+ def transform_perspective_camera(hash)+ transform_camera hash, 'perspective_camera'+ end++ def transform_camera(hash, key)+ return unless hash.key?(key)++ hash[key].transform_values! do |v|+ if v.is_a?(Hash)+ v.transform_values!(&:to_f)+ else+ v.to_f+ end+ end+ end++ def transform_lines(hash)+ return unless hash.key?('lines')++ hash['lines'] = hash['lines']['line'].map! do |line|+ line.deep_transform_values!(&:to_f)+ end+ end++ def transform_clipping_planes(hash)+ return unless hash.key?('clipping_planes')++ hash['clipping_planes'] = hash['clipping_planes']['clipping_plane'].map! do |plane|+ plane.deep_transform_values!(&:to_f)+ end+ end++ ##+ # Move selections up the tree from the nested XML node+ def transform_selections(hash)+ selections = hash.dig('components', 'selection', 'component')+ return unless selections++ hash['components']['selection'] = selections+ end++ ##+ # Move coloring up the tree from the nested XML node+ def transform_coloring(hash)+ colors = hash.dig('components', 'coloring', 'color')+ return unless colors++ # avoid Array(colors) since that deconstructs the array+ colors = [colors] unless colors.is_a?(Array)++ hash['components']['coloring'] = colors.map do |entry|+ # Prepend hash for hex color+ entry['color'] = "##{entry['color']}"+ entry+ end+ end++ def transform_visibility(hash)+ visibility = hash.dig('components', 'visibility')+ return unless visibility++ visibility['default_visibility'] = visibility['default_visibility'] == 'true'++ # Hoist exceptions components up from the nested XML node+ exceptions = visibility.dig('exceptions', 'component')+ visibility['exceptions'] = Array(exceptions) if exceptions++ # Move view_setup_hints+ view_setup_hints = hash.dig('components', 'view_setup_hints')+ visibility['view_setup_hints'] = view_setup_hints.transform_values { |val| val == 'true' }++ # Remove the old node+ hash['components'].delete('view_setup_hints')+ end+ end+ end+enddiff --git a/modules/bcf/spec/factories/bcf_issue_factory.rb b/modules/bcf/spec/factories/bcf_issue_factory.rbindex d5cac7d2784..bf07fda9bb1 100644--- a/modules/bcf/spec/factories/bcf_issue_factory.rb+++ b/modules/bcf/spec/factories/bcf_issue_factory.rb@@ -115,6 +115,12 @@ FactoryBot.define doMARKUPend+ factory :bcf_issue_with_viewpoint do+ after(:create) do |issue|+ create(:bcf_viewpoint, issue: issue)+ end+ end+factory :bcf_issue_with_comment doafter(:create) do |issue|viewpoint = create(:bcf_viewpoint, issue: issue)diff --git a/modules/bcf/spec/factories/bcf_viewpoint_factory.rb b/modules/bcf/spec/factories/bcf_viewpoint_factory.rbindex 9f850afbea9..81972de6e05 100644--- a/modules/bcf/spec/factories/bcf_viewpoint_factory.rb+++ b/modules/bcf/spec/factories/bcf_viewpoint_factory.rb@@ -38,6 +38,123 @@ FactoryBot.define donew_uuid = SecureRandom.uuiduuid { new_uuid }viewpoint_name { "Viewpoint_#{new_uuid}.bcfv" }+ viewpoint do+ <<~MARKUP+ <?xml version="1.0" encoding="utf-8"?>+ <!--Created with the iabi.BCF library, V1.1.0 at 22.05.2017 09:51. Visit http://iabi.eu to find out more.-->+ <VisualizationInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Guid="8dc86298-9737-40b4-a448-98a9e953293a">+ <Components>+ <ViewSetupHints SpacesVisible="true" SpaceBoundariesVisible="true" OpeningsVisible="true" />+ <Selection>+ <Component IfcGuid="0cSRUx$EX1NRjqiKcYQ$a0" />+ <Component IfcGuid="1jQQiGIAnFzxOUzrdmJYDS" />+ <Component IfcGuid="0fdpeZZEX3FwJ7x0ox5kzF" />+ <Component IfcGuid="23Zwlpd71EyvHlH6OZ77nK" />+ <Component IfcGuid="1OpjQ1Nlv4sQuTxfUC_8zS" />+ </Selection>+ <Visibility DefaultVisibility="true">+ <Exceptions>+ <Component IfcGuid="0Gl71cVurFn8bxAOox6M4X" />+ <Component IfcGuid="23Zwlpd71EyvHlH6OZ77nK" />+ <Component IfcGuid="3DvyPxGIn8qR0KDwbL_9r1" />+ <Component IfcGuid="0fdpeZZEX3FwJ7x0ox5kzF" />+ <Component IfcGuid="1OpjQ1Nlv4sQuTxfUC_8zS" />+ </Exceptions>+ </Visibility>+ <Coloring>+ <Color Color="3498db">+ <Component IfcGuid="0fdpeZZEX3FwJ7x0ox5kzF" />+ <Component IfcGuid="23Zwlpd71EyvHlH6OZ77nK" />+ <Component IfcGuid="1OpjQ1Nlv4sQuTxfUC_8zS" />+ <Component IfcGuid="0cSRUx$EX1NRjqiKcYQ$a0" />+ </Color>+ </Coloring>+ </Components>+ <PerspectiveCamera>+ <CameraViewPoint>+ <X>12.2088897788292</X>+ <Y>52.323145074034</Y>+ <Z>5.24072091171001</Z>+ </CameraViewPoint>+ <CameraDirection>+ <X>-0.381615611200324</X>+ <Y>-0.825232810204882</Y>+ <Z>-0.416365617893758</Z>+ </CameraDirection>+ <CameraUpVector>+ <X>0.05857014928797</X>+ <Y>0.126656300502579</Y>+ <Z>0.990215996212637</Z>+ </CameraUpVector>+ <FieldOfView>60</FieldOfView>+ </PerspectiveCamera>+ <Lines>+ <Line>+ <StartPoint>+ <X>0</X>+ <Y>0</Y>+ <Z>0</Z>+ </StartPoint>+ <EndPoint>+ <X>0</X>+ <Y>0</Y>+ <Z>1</Z>+ </EndPoint>+ </Line>+ <Line>+ <StartPoint>+ <X>0</X>+ <Y>0</Y>+ <Z>1</Z>+ </StartPoint>+ <EndPoint>+ <X>0</X>+ <Y>1</Y>+ <Z>1</Z>+ </EndPoint>+ </Line>+ <Line>+ <StartPoint>+ <X>0</X>+ <Y>1</Y>+ <Z>1</Z>+ </StartPoint>+ <EndPoint>+ <X>1</X>+ <Y>1</Y>+ <Z>1</Z>+ </EndPoint>+ </Line>+ </Lines>+ <ClippingPlanes>+ <ClippingPlane>+ <Location>+ <X>0</X>+ <Y>0</Y>+ <Z>0</Z>+ </Location>+ <Direction>+ <X>0</X>+ <Y>0</Y>+ <Z>1</Z>+ </Direction>+ </ClippingPlane>+ <ClippingPlane>+ <Location>+ <X>0</X>+ <Y>0</Y>+ <Z>0</Z>+ </Location>+ <Direction>+ <X>0</X>+ <Y>1</Y>+ <Z>0</Z>+ </Direction>+ </ClippingPlane>+ </ClippingPlanes>+ </VisualizationInfo>+ MARKUP+ endafter(:create) do |viewpoint|create(:bcf_viewpoint_attachment, container: viewpoint)diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rbnew file mode 100644index 00000000000..033d72b46b1--- /dev/null+++ b/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rb@@ -0,0 +1,106 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2019 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'+require 'rack/test'++require_relative './shared_responses'++describe 'BCF 2.1 viewpoints resource', type: :request, content_type: :json, with_mail: false do+ include Rack::Test::Methods+ include API::V3::Utilities::PathHelper++ shared_let(:project) do+ FactoryBot.create(:project,+ enabled_module_names: [:bcf])+ end++ shared_let(:view_only_user) do+ FactoryBot.create(:user,+ member_in_project: project,+ member_with_permissions: [:view_linked_issues])+ end++ shared_let(:non_member_user) do+ FactoryBot.create(:user)+ end++ shared_let(:work_package) { FactoryBot.create(:work_package, project: project) }+ shared_let(:bcf_issue) { FactoryBot.create(:bcf_issue_with_viewpoint, work_package: work_package) }++ let(:viewpoint) { bcf_issue.viewpoints.first }+ let(:viewpoint_json) { ::OpenProject::Bcf::BcfJson::ViewpointMapper.new(viewpoint).result }+ subject(:response) { last_response }++ describe 'GET /api/bcf/2.1/projects/:project_id/topics/:topic/viewpoints' do+ let(:path) { "/api/bcf/2.1/projects/#{project.id}/topics/#{bcf_issue.uuid}/viewpoints" }+ let(:current_user) { view_only_user }++ before do+ login_as(current_user)+ get path+ end++ it_behaves_like 'bcf api successful response' do+ let(:expected_body) { [viewpoint_json] }+ end++ context 'lacking permission to see project' do+ let(:current_user) { non_member_user }++ it_behaves_like 'bcf api not found response'+ end+ end++ describe 'GET /api/bcf/2.1/projects/:project_id/topics/:uuid/viewpoints/:uuid' do+ let(:path) { "/api/bcf/2.1/projects/#{project.id}/topics/#{bcf_issue.uuid}/viewpoints/#{viewpoint.uuid}" }+ let(:current_user) { view_only_user }++ before do+ login_as(current_user)+ bcf_issue+ get path+ end++ it_behaves_like 'bcf api successful response' do+ let(:expected_body) { viewpoint_json }+ end++ context 'lacking permission to see project' do+ let(:current_user) { non_member_user }++ it_behaves_like 'bcf api not found response'+ end++ context 'invalid uuid' do+ let(:path) { "/api/bcf/2.1/projects/#{project.id}/topics/0/viewpoints" }++ it_behaves_like 'bcf api not found response'+ end+ end+end