Initializing the Travel Impressions model with Data

After the model is designed (refer the Spiral 4 of the Travel Impression Model figure) and its code is generated, the model is initialized with some basic data starting with its entries. This is done in the lib/travel/impressions/init.dart file.

initTravelImpressions(var entries) {
  _initCountries(entries);
  _initTravelers(entries);
}

We will start with creating a country and some of its places, together with web links, from the entries parameter.

_initCountries(var entries) {
var countries = entries.countries;
var country = new Country(countries.concept);
  country.code = ‘BA’;
  country.name = ‘Bosnia and Herzegovina’;
  countries.add(country);

In the Country concept of the graphical model, there is no code attribute of the String type. The code attribute is inherited from Dartling. In a concept, you do not need to use the inherited code attribute. However, if you use it, its values must be unique. Note that a new country is added to the countries’ entities. Once the first country is created, its first place may be created.

var place = new Place(country.places.concept);
  place.name = ‘Bascarsija’;
  place.city = ‘Sarajevo’;
  place.description = ‘old town’;
  place.country = country;
  country.places.add(place);

After a new place is created, its country is established, and it is added to the country’s places. Then, web links for the Bascarsija place are created.

  var webLink = new WebLink(place.webLinks.concept);
  webLink.url =
  Uri.parse(‘http://en.wikipedia.org/wiki/Baščaršija’);
  webLink.title = ‘Wikipedia’;
  webLink.description =
      “Sarajevo’s old bazaar and the historical and cultural center.”;
  webLink.place = place;
  place.webLinks.add(webLink);

  webLink = new WebLink(place.webLinks.concept);
  webLink.url =         Uri.parse(‘http://en.wikipedia.org/wiki/File: Baščaršija _2006.jpg’);
  webLink.title = ‘image’;
  webLink.place = place;
  place.webLinks.add(webLink);

Other countries, places, and web links may be created in a similar way. Travelers, with their messages and impressions, are created with their entry into the model.

_initTravelers(var entries) {
  var countries = entries.countries;
  var travelers = entries.travelers;

  var traveler = new Traveler(travelers.concept);
  traveler.email = ‘[email protected]’;
  traveler.password = ‘dzenan’;
  traveler.firstName = ‘Dzenan’;
  traveler.lastName = ‘Ridjanovic’;
  traveler.description = ‘working hard on Dart projects’;
  travelers.add(traveler);

  var message = new Message(traveler.messages.concept);
  message.subject = ‘first day in Sarajevo’;
  message.traveler = traveler;
  traveler.messages.add(message);

  var country = countries.singleWhereCode(‘BA’);
var place = country.places.firstWhereAttribute(‘name’, ‘Bascarsija’);
  var impression =
      new Impression.withId(message.impressions.concept, place, message);
  impression.text = ‘as usual, my first meal is “cevapcici”’;
  message.impressions.add(impression);
  place.impressions.add(impression);

  place = country.places.firstWhereAttribute(‘name’, ‘Bjelasnica’);
  impression =
      new Impression.withId(message.impressions.concept, place, message);
  impression.text = ‘after “cevapcici”, hiking at Bjelasnica is calling’;
  message.impressions.add(impression);
  place.impressions.add(impression);

  place = country.places.firstWhereAttribute(‘name’, ‘Dariva’);
  impression =
    new Impression.withId(message.impressions.concept, place, message);
  impression.text = ‘however, short walk will do’;
  message.impressions.add(impression);
  place.impressions.add(impression);

The Impression concept has two parents: Message and Place. A place is found within a country. A country’s code is unique and the singleWhereCode method is used on the country’s entry to find the country. A place’s name is a part of the identifier (place within country) and the firstWhereAttribute method is used on country.places to find the place. Once some basic data is created, a default web application (web/travel/impressions/travel_impressions.html) may be run to validate the model by navigating from the model’s entries and discovering missing concepts, attributes, and relationships. This default application is an example of generic programming based on the meta model of Dartling. Its code is in the dartling_default_app project that is imported, based on the pub declaration in the generated pubspec.yaml file:

name: travel_impressions
author: Your Name
homepage: http://ondart.me/
version: 0.0.1
description: travel_impressions application that uses dartling for its model.
dependencies:
  browser: any
  dartling:
    git: git://github.com/dzenanr/dartling.git
  dartling_default_app:
    git: git://github.com/dzenanr/dartling_default_app.git

This generates the following code in the web/travel/impressions/travel_impressions_web.dart file:

import “dart:html”;  
import “package:dartling/dartling.dart”; 
import “package:dartling_default_app/dartling_default_app.dart”; 
import “package:travel_impressions/travel_impressions.dart”; 
 
initTravelData(TravelRepo travelRepo) { 
   var travelModels = 
       travelRepo.getDomainModels(TravelRepo.travelDomainCode);  
   var travelImpressionsEntries = travelModels.getModelEntries(TravelRepo.travelImpressionsModelCode); 
   initTravelImpressions(travelImpressionsEntries); 
   travelImpressionsEntries.display(); 
   travelImpressionsEntries.displayJson(); 
} 
 
showTravelData(TravelRepo travelRepo) { 
   var mainView = new View(document, “main”); 
   mainView.repo = travelRepo; 
   
new RepoMainSection(mainView); 
} 
 
void main() { 
var travelRepo = new TravelRepo(); 
  initTravelData(travelRepo); 
  showTravelData(travelRepo); 
} 

In Dartling, a repository may have many domains and a domain may have many models. However, in a default web application, only one domain and one model are used. After the travel repository is created with the Travel domain and the Impressions model, the model is initialized with some data, and they are shown in a web page of the default application. Data are also displayed in the Dart Editor’s console. Note that this is the only code written manually for the time being. The next step is to test the model, and more specific code will be added to the project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.117.191