Skip to content

DSL Configuration File Example

SerenaG edited this page Dec 28, 2014 · 1 revision

The code below is written only by way of example. The code fragments assume that there are the appropriate collection and document attributes on an existing MongoDB database. The execution of this code could produce an unexpected behaviour if the requirements aren't satisfied.

Example 1: customer list

Your company want to use MaaP to have a constantly updated list of own customers. In particular, you want two distinct index-page:

  • a page to show the customers over the age of 40 years;
  • a page to show the customers under the age of 40 years.

For each customer, you want to know name, surname, email, age and number of orders placed, assuming having an attribute of the collection of MongoDB called orders containing an array of orders. You want to redirect to the customer's show-page through the email attribute, and you want order the table based on surname, age and number of orders. In the show-page you want to show the customer's address. Finally, you want 20 elements per page.

To achieve this goal, you have to produce a DSL file alike of this:

collection(
	name: "customers", 
	label: "JuniorCustomers", 
	id: "Junior", 
	weight: "0" 
) {
	index( 
		perpage: 20, 
		sortby: "surname", 
		order: "asc", 
		query: {age : { $lt : 40}}
	) {
		column(
			name: "name", 
			label: "Nome", 
			sortable: false, 
			selectable: false
		)
		column(
			name: "surname",
			label: "Cognome",
			sortable: true,
			selectable: false
		)
		column(
			name: "email",
			label: "Email",
			sortable: false,
			selectable: true
		)
		column(
			name: "age",
			label: "Eta",
			sortable: true,
			selectable: false
		)
		column(
			name: "orders",
			label: "# Ordini",
			sortable: true,
			selectable: false,
			transformation: function(val) { 
				return val.length;
			}
		)
	}
	show() {
		row(
			name: "name", 
			label: "Nome"
		)
		row(
			name: "surname",
			label: "Cognome"
		)
		row(
			name: "email",
			label: "Email"
		)
		row(	
			name: "age",
			label: "Eta"
		)
		row(
			name: "address",
			label: "Indirizzo"
		)
		row(
			name: "orders",
			label: "# Ordini",
			transformation: function(val) {
				return val.length;
			}
		)
	}
}

For the configuration of the collection of the users over the age of 40 years, the syntax is very similar, but you must edit the expressions collection and index like:

collection(
	name: "customers", 
	label: "Senior", 
	id: "Senior", 
	weight: "0" 
) {
	index( 
		perpage: 20, 
		sortby: "surname", 
		order: "asc", 
		query: {age : { $gte : 40}}
	) {
		...
		...
	}
	show() {
		...
		...
	}
}

Example 2: product list

Your company provide products and want to have quickly a tool to create custom views of the products produced in the current year. In the index-page, the company want to view the identification code of the product, the name of the model, the available quantity in stock, the selling price in EUR and the production date in US format. Moreover, it wants to know if the product has been shipped or is still in stock. In the show-page of a product, it wants to view all the available fields. Finally, it wants to order the products for production date and available quantity.

To achieve this goal, you have to produce a DSL file alike of this:

collection(
   name: "products",
   label: "Products - " + getCurrentYear(),
) {
   index(
   	query: { 
   		productionDate.year: { $gt : getCurrentYear() } 
   	}
   ) {
   	column(
   		name: "_id",
   		label: "Product id",
   		selectable: true
   	)
   	column(
   		name: "model",
   		label: "Model"
   	)
   	column(
   		name: "amount",
   		label: "Quantity in stock",
   		sortable: true
   	)
   	column(
   		name: "price",
   		label: "Price",
   		transformation: euroFromDollar
   	)
   	column(
   		name: "production_date",
   		label: "Production date",
   		transformation: getAmericanDate
   	)
   	column(
   		name: "ship_date",
   		label: "State",
   		transformation: isShipped
   	)
}

var euroFromDollar = function(val) {
   // convert val from dollars to euro
   return val;
}

var getCurrentYear = function() {
   // return the current year
   return currentYear;
}

var getAmericanDate = function(date) {
   // convert date in US format
   return americanDate;
}

var isShipped = function(date) {
   var shipped = "";
   if (shipped === true) {
   	return "Shipped";
   }
   else {
   	return: "Not shipped";
   }
}

Clone this wiki locally