gikoha’s blog

個人的メモがわり

再生! Framework

なんでeclipseだとこうなるんだ..

% cd workspace
% play new pbook
% cd pbook
% play eclipsify

conf/application.confのdb=の行を編集

db=mysql:USERNAME:PASSWORD@DBNAME
# 末尾に追加;新しい方式(dependencies.yml)ではエラーが出た
# Import the crud module
module.crud=${play.path}/modules/crud

pbook/libにcardme-v0.2.6.jarを入れ、外部JARとして追加
再生!メニューから New Modelを選び、Package name: models, Model Name: Abook とする
Abook.java

package models;
...(中略)...
@Entity
public class Abook extends Model
{
	public String name;
	public String postalnum;
	public String address;
	public String phone;

	public Abook(String name, String postalnum, String address, String phone)
	{
		this.name = name;
		this.postalnum = postalnum;
		this.address = address;
		this.phone = phone;
	}
}

conf/routes

# Catch all
#*       /{controller}/{action}                  {controller}.{action}
GET		/exportvcf								Application.exportVCF
GET		/importForm								Application.importForm
POST	/importvcf								Application.importVCF
*       /										module:crud

controllers/Abooks

package controllers;

import play.*;
import play.mvc.*;

public class Abooks extends CRUD {
}

controllers/Application.java

package controllers;

import info.ineighborhood.cardme.engine.VCardEngine;
....

public class Application extends Controller
{
	public static void index()
	{
		render();
	}

	static String getVCardString(Long id)
	{
		Abook person = Abook.findById(id);
		VCard vcard = new VCardImpl();

		vcard.setBegin(new BeginType());
		vcard.setVersion(new VersionType(VCardVersion.V3_0));
		vcard.setName(new NameType(person.name));
		vcard.setFormattedName(new FormattedNameType(person.name));
		AddressType addr = new AddressType();
		addr.setStreetAddress(person.address);
		addr.setPostalCode(person.postalnum);
		vcard.addAddress(addr);
		vcard.addTelephoneNumber(new TelephoneType(person.phone));
		vcard.setEnd(new EndType());

		VCardWriter writer = new VCardWriter();
		writer.setVCard(vcard);
		String vstring = writer.buildVCardString();
		return vstring;
	}

	public static void exportVCF(Long id)
	{
		String vcard = getVCardString(id);
		Abook person = Abook.findById(id);
		// vcf extension is in mime.types
		renderBinary(new ByteArrayInputStream(vcard.getBytes()), person.name
				+ ".vcf");
	}

	public static void importForm()
	{ // show importForm.html
		render();
	}

	public static void importVCF(File file) throws IOException
	{
		if (file != null)
		{
			VCardEngine vcardParser = new VCardEngine();
			VCard vcard = vcardParser.parse(file);
			String name = vcard.getFormattedName().getFormattedName();
			String phone = vcard.getTelephoneNumbers().next().getTelephone();
			String addr = vcard.getAddresses().next().getStreetAddress();
			String post = vcard.getAddresses().next().getPostalCode();
			// System.out.println("Name:"+name+" Addr:〒 "+post+" "+addr+" Phone:"+phone);
			if(name!=null)
			{
				Abook abookInstance = new Abook(name, post, addr, phone);
				abookInstance.save();
			}
		}

		redirect("/abooks"); // redirect to listing page
	}
}

views/Abooks/list.html

#{extends 'CRUD/layout.html' /} #{set
title:messages.get('crud.list.title', type.name) /}

<div id="crudList" class="${type.name}">

	<h2 id="crudListTitle">&{'crud.list.title', type.name}</h2>

	<div id="crudListSearch">#{crud.search /}</div>

	<div id="crudListTable">
		#{crud.table fields:['name', 'postalnum', 'address', 'phone']}
		#{crud.custom 'name'} <a href="@{Abooks.show(object.id)}">
			${object.name} </a> &nbsp;(<a href="@{Application.exportvcf(object.id)}">export vcf</a>)
		#{/crud.custom} #{/crud.table}
	</div>

	<div id="crudListPagination">#{crud.pagination /}</div>

	<p><a href="@{Application.importForm()}">Import vCard(vcf) file</a></p>

	<p id="crudListAdd">
		<a href="@{blank()}">&{'crud.add', type.modelName}</a>
	</p>

</div>

Abooks/show.html

#{extends 'CRUD/layout.html' /} #{set
title:messages.get('crud.show.title', type.modelName) /}

<div id="crudShow" class="${type.name}">

	<h2 id="crudShowTitle">&{'crud.show.title', type.modelName}</h2>

	<div class="objectForm">
		#{form action:@save(object._key()), enctype:'multipart/form-data'}
		#{crud.form /}
		<p class="crudButtons">
			<input type="submit" name="_save"
				value="&{'crud.save', type.modelName}" /> <input type="submit"
				name="_saveAndContinue"
				value="&{'crud.saveAndContinue', type.modelName}" />
		</p>
		#{/form}
	</div>

	<p>vCard(.vcf) file:
	<a href="@{Application.exportvcf(object.id)}">Export</a>&nbsp;
	<a href="@{Application.importForm()}">Import</a><br /></p>

	#{form @delete(object._key())}
	<p class="crudDelete">
		<input type="submit" value="&{'crud.delete', type.modelName}" />
	</p>
	#{/form}

</div>

Application/importForm.html

#{extends 'main.html' /}
#{set title:'importVCF.html' /}

<p>Specify vcf file to import...</p>

#{form @importVCF(), enctype:'multipart/form-data'}
    <input type="file" id="file" name="file" /><br/>
    <input type="submit" value="Import" />
#{/}


ほとんどRooやGrailsと同等の機能のアプリができましたとさ