Created
September 4, 2011 03:42
-
-
Save roothybrid7/1192210 to your computer and use it in GitHub Desktop.
Slim3 GlobalTransaction(gist: 1192196)を使う実行サンプル(Model: Person, Address)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.rh7.model; | |
import java.io.Serializable; | |
import java.util.Date; | |
import com.google.appengine.api.datastore.Key; | |
import org.slim3.datastore.Attribute; | |
import org.slim3.datastore.CreationDate; | |
import org.slim3.datastore.Model; | |
import org.slim3.datastore.ModelRef; | |
import org.slim3.datastore.ModificationDate; | |
@Model(schemaVersion = 1) | |
public class Address implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Attribute(primaryKey = true) | |
private Key key; | |
@Attribute(version = true) | |
private Long version; | |
// many2one relations(Person[親モデル]関連付け) | |
private ModelRef<Person> personRef = | |
new ModelRef<Person>(Person.class); | |
private String id; | |
private String personId; | |
private String zipCode; | |
private String address; | |
@Attribute(listener = CreationDate.class) | |
private Date createdAt; | |
@Attribute(listener = ModificationDate.class) | |
private Date updatedAt; | |
/** | |
* @return personRef | |
*/ | |
public ModelRef<Person> getPersonRef() { | |
return personRef; | |
} | |
/* [ ... ]: setter/getterは省略 */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.rh7.model; | |
import java.io.Serializable; | |
import java.util.Date; | |
import com.google.appengine.api.datastore.Key; | |
import com.google.appengine.api.datastore.Query.SortDirection; | |
import org.slim3.datastore.Attribute; | |
import org.slim3.datastore.CreationDate; | |
import org.slim3.datastore.InverseModelListRef; | |
import org.slim3.datastore.Model; | |
import org.slim3.datastore.ModificationDate; | |
import org.slim3.datastore.Sort; | |
@Model(schemaVersion = 1) | |
public class Person implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Attribute(primaryKey = true) | |
private Key key; | |
@Attribute(version = true) | |
private Long version; | |
// many2one relations(Person : Address = 1 : nの関連) | |
@Attribute(persistent = false) | |
private InverseModelListRef<Address, Person> addressListRef = | |
new InverseModelListRef<Address, Person>( | |
Address.class, | |
"personRef", | |
this, | |
new Sort("updatedAt", SortDirection.DESCENDING)); | |
private String id; | |
private String name; | |
private Integer viewCount = 0; | |
@Attribute(listener = CreationDate.class) | |
private Date createdAt; | |
@Attribute(listener = ModificationDate.class) | |
private Date updatedAt; | |
private Date addressAt; | |
/** | |
* @return addressListRef | |
*/ | |
public InverseModelListRef<Address, Person> getAddressListRef() { | |
return addressListRef; | |
} | |
/* [ ... ]: setter/getterは省略 */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.rh7.service; | |
import java.util.List; | |
import org.slim3.util.BeanUtil; | |
import com.google.appengine.api.datastore.Key; | |
import com.google.appengine.repackaged.com.google.common.collect.Lists; | |
import jp.rh7.dao.AddressDao; | |
import jp.rh7.dao.PersonDao; | |
import jp.rh7.dao.QueryManager; | |
import jp.rh7.dao.PersistWithTx; | |
import jp.rh7.model.Address; | |
import jp.rh7.model.Person; | |
import jp.rh7.service.info.AddressInfo; | |
import jp.rh7.service.info.PersonInfo; | |
import jp.rh7.utils.DataHandler; | |
import jp.rh7.utils.DateUtils; | |
// 整合性をとる必要のあるデータは | |
// Transactionで実行,成功するまでリトライ | |
// (それ以外のデータはモデルに事前にセット) | |
public class PersonService { | |
private PersonDao personDao = new PersonDao(); | |
private AddressDao addrDao = new AddressDao(); | |
private void setAddrProperties(Person personModel, | |
Address addrModel) throws IllegalArgumentException { | |
String id = | |
String.format( | |
"%s%s%s", | |
personModel.getId(), | |
DateUtils.getCurrentDateMilliTime(), | |
addrDao.allocateId(personModel.getKey()).getId()); | |
Key addrKey = addrDao.createKey(personModel.getKey(), descId); | |
// Slim3のリレーションシップを使うので必要ないけどキーによる関連を設定 | |
addrModel.setKey(addrKey); | |
addrModel.setId(id); | |
addrModel.setPersonId(personModel.getId()); | |
} | |
// 新規登録処理(Person[親モデル], Address[子モデル]の登録) | |
public void createPerson(final Person personModel, | |
final Address addrModel) { | |
// Key,Id採番やModelのrelation等の内部管理データのセット | |
String id = | |
String.format( | |
"%s%s", | |
DateUtils.getCurrentDateMilliTime(), | |
personDao.allocateId().getId()); | |
Key personKey = personDao.createKey(personId); | |
personModel.setKey(personKey); | |
personModel.setId(id); | |
setAddrProperties(personModel, addrModel); | |
addrModel.getPersonRef().setModel(personModel); | |
// 以降Transactionで実行される処理 | |
DataHandler handler = new DataHandler() { | |
public Object[] getModels() { | |
return new Object[] { personModel, addrModel }; | |
} | |
// アドレス追加日時をPersonモデルに設定 | |
public void execute() { | |
personModel.setAddressAt(DateUtils.getDate()); | |
} | |
}; | |
PersistWithTx.save(handler); | |
} | |
// アドレス追加登録(2番目以降のAddress追加) | |
public void addAddress(final Person personModel, | |
final Address addrModel) { | |
setAddrProperties(personModel, addrModel); | |
// 以降Transactionで実行される処理 | |
DataHandler handler = new DataHandler() { | |
Person model = personModel; | |
public Object[] getModels() { | |
return new Object[] { model, addrModel }; | |
} | |
// 最新のPersonモデルをAddressモデルの関連に設定 | |
public void execute() { | |
model = personDao.get(model.getKey()); | |
addrModel.getPersonRef().setModel(model); | |
model.setAddressAt(DateUtils.getDate()); | |
} | |
}; | |
PersistWithTx.save(handler); | |
} | |
// Person参照回数のIncrement | |
public void incViewCount(final Key key) { | |
// 以降Transactionで実行される処理 | |
DataHandler handler = new DataHandler() { | |
Person model = null; | |
public Object[] getModels() { | |
return new Object[] { model }; | |
} | |
// 最新のモデルを取得して参照回数の更新 | |
public void execute() { | |
model = personDao.get(key); | |
model.setViewCount(model.getViewCount() + 1); | |
} | |
}; | |
PersistWithTx.save(handler); | |
} | |
/* [ ... ] */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment