{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Add Date, Integer and Logical data \n",
    "Work with date, integer, and logical attribute types."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to MI\n",
    "Get a database and table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "from GRANTA_MIScriptingToolkit import granta as mpy\n",
    "\n",
    "mi = mpy.connect('http://localhost/mi_servicelayer', autologon=True)\n",
    "db = mi.get_db(db_key='MI_Training')\n",
    "db.set_unit_system(unit_system='Metric', absolute_temperatures=False)\n",
    "tab = db.get_table('AM Builds')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a new record and path\n",
    "Define a path in the table from a starting folder (in this case the top level folder) using `path_from()`.\n",
    "If the path does not exist, the required folders will be created.\n",
    "Specify `end_node` to create a new **Record** object at the end of the path with that name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<Record long name:STK Example 13:Tue Mar 23 16:19:23 2021>,\n",
       " <Record long name:Ti-6Al-4V>)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now = datetime.now().strftime(\"%c\")\n",
    "record_name = 'STK Example 13:{}'.format(now)\n",
    "record = tab.path_from(None, tree_path=['Blown Powder (Laser beam)', 'Ti-6Al-4V'], end_node=record_name)\n",
    "record, record.parent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Edit the record's attributes\n",
    "Fetch date, integer and logical attributes for editing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tab.bulk_fetch([record], attributes=['Date of Beam Profile Diagnostic',\n",
    "                                     'Date of Build',\n",
    "                                     'Maximum Number of Layers',\n",
    "                                     'Built in Closed Chamber?'])\n",
    "diagnostic_date = record.attributes['Date of Beam Profile Diagnostic']\n",
    "build_date = record.attributes['Date of Build']\n",
    "number_of_layers = record.attributes['Maximum Number of Layers']\n",
    "closed_chamber = record.attributes['Built in Closed Chamber?']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Date attributes\n",
    "Use the `value` property to set the date value of the attribute with either a **datetime** object or a properly\n",
    "formatted string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "diagnostic_date.value = '2020-07-12'\n",
    "build_date.value = datetime.now()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Integer attributes\n",
    "Use the `value` property to set the integer value of the attribute."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "number_of_layers.value = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Logical attributes\n",
    "Use the `value` property to set the logical value of the attribute."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "closed_chamber.value = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write your changes to MI\n",
    "First, specify the attributes on the record which you want to update on the server. Then write the changes to MI. The list of updated **Record** objects is returned."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "record.set_attributes([diagnostic_date, build_date, number_of_layers, closed_chamber])\n",
    "record = mi.update([record])[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Output the record's attributes\n",
    "Access the attribute values via the same properties you used to assign them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date of Beam Profile Diagnostic: Jul 12 2020\n",
      "Date of Build: Mar 23 2021\n",
      "Maximum Number of Layers: 5\n",
      "Built in Closed Chamber?: Yes\n"
     ]
    }
   ],
   "source": [
    "datetime_format = \"%b %d %Y\"\n",
    "diagnostic_date = record.attributes['Date of Beam Profile Diagnostic']\n",
    "print(\"Date of Beam Profile Diagnostic: {0}\".format(diagnostic_date.value.strftime(datetime_format)))\n",
    "\n",
    "build_date = record.attributes['Date of Build']\n",
    "print(\"Date of Build: {0}\".format(build_date.value.strftime(datetime_format)))\n",
    "\n",
    "number_of_layers = record.attributes['Maximum Number of Layers']\n",
    "print(\"Maximum Number of Layers: {0}\".format(number_of_layers.value))\n",
    "\n",
    "closed_chamber = record.attributes['Built in Closed Chamber?']\n",
    "print(\"Built in Closed Chamber?: {0}\".format('Yes' if closed_chamber.value else 'No'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
