public final <T extends RawEntity<K>, K> T create(Class<T> type, Map<String, Object> params) { try { return entityManager.create(type, params); } catch (SQLException e) { throw new ActiveObjectsSqlException(entityManager, e); } }
public final <T extends RawEntity<K>, K> T create(Class<T> type, DBParam... params) { try { return entityManager.create(type, params); } catch (SQLException e) { throw new ActiveObjectsSqlException(entityManager, e); } }
/** * Creates and INSERTs a new entity of the specified type with the given map of parameters. This method merely * delegates to the {@link #create(Class, DBParam...)} method. The idea behind having a separate convenience method * taking a map is in circumstances with large numbers of parameters or for people familiar with the anonymous inner * class constructor syntax who might be more comfortable with creating a map than with passing a number of * objects. * * @param type The type of the entity to INSERT. * @param params A map of parameters to pass to the INSERT. * @return The new entity instance corresponding to the INSERTed row. * @see #create(Class, DBParam...) */ public <T extends RawEntity<K>, K> T create(Class<T> type, Map<String, Object> params) throws SQLException { DBParam[] arrParams = new DBParam[params.size()]; int i = 0; for (String key : params.keySet()) { arrParams[i++] = new DBParam(key, params.get(key)); } return create(type, arrParams); }
/** * Creates and INSERTs a new entity of the specified type with the given map of parameters. This method merely * delegates to the {@link #create(Class, DBParam...)} method. The idea behind having a separate convenience method * taking a map is in circumstances with large numbers of parameters or for people familiar with the anonymous inner * class constructor syntax who might be more comfortable with creating a map than with passing a number of * objects. * * @param type The type of the entity to INSERT. * @param params A map of parameters to pass to the INSERT. * @return The new entity instance corresponding to the INSERTed row. * @see #create(Class, DBParam...) */ public <T extends RawEntity<K>, K> T create(Class<T> type, Map<String, Object> params) throws SQLException { DBParam[] arrParams = new DBParam[params.size()]; int i = 0; for (String key : params.keySet()) { arrParams[i++] = new DBParam(key, params.get(key)); } return create(type, arrParams); }
private Distribution addOnlineDistribution(EntityManager entityManager, Publication publication, String url) throws Exception { OnlineDistribution distribution = entityManager.create(OnlineDistribution.class); distribution.setURL(new URL(url)); distribution.save(); PublicationToDistribution ptd = entityManager.create(PublicationToDistribution.class); ptd.setDistribution(distribution); ptd.setPublication(publication); ptd.save(); return distribution; }
private Photo addPhoto(EntityManager entityManager) throws Exception { final Photo photo = entityManager.create(Photo.class); photo.save(); PhotoData.id = photo.getID(); // save the id return photo; }
private Distribution addPrintDistribution(EntityManager entityManager, Publication publication, int copies) throws Exception { PrintDistribution distribution = entityManager.create(PrintDistribution.class); distribution.setCopies(copies); distribution.save(); PublicationToDistribution ptd = entityManager.create(PublicationToDistribution.class); ptd.setDistribution(distribution); ptd.setPublication(publication); ptd.save(); return distribution; }
private Post addPost(EntityManager entityManager) throws Exception { final Post post = entityManager.create(Post.class); post.setTitle(PostData.TITLE); post.save(); // save the id PostData.id = post.getID(); return post; }
private Author addAuthor(EntityManager entityManager, Publication publication, String title, int index) throws Exception { final Author author = entityManager.create(Author.class); author.setName(title + " " + publication.getID() + ":" + index); author.save(); final Authorship authorship = entityManager.create(Authorship.class); authorship.setPublication(publication); authorship.setAuthor(author); authorship.save(); return author; }
public Nose addNose(EntityManager entityManager, Person person) throws SQLException { final Nose nose = entityManager.create(Nose.class); nose.setPerson(person); nose.setLength(NoseData.LENGTH); nose.save(); // save the id NoseData.id = nose.getID(); return nose; }
private Address[] addAddresses(EntityManager entityManager) throws Exception { final Address[] addresses = new Address[AddressData.getIds().length]; for (int i = 0; i < addresses.length; i++) { final EmailAddress address = entityManager.create(EmailAddress.class); address.setEmail(AddressData.EMAILS[i]); address.save(); AddressData.ids[i] = address.getID(); addresses[i] = address; } return addresses; }
private PersonLegalDefence[] addPersonLegalDefences(EntityManager entityManager, Person person) throws Exception { final PersonLegalDefence[] personLegalDefences = new PersonLegalDefence[PersonLegalDefenceData.ids.length]; for (int i = 0; i < PersonLegalDefenceData.ids.length; i++) { final PersonLegalDefence personLegalDefence = entityManager.create(PersonLegalDefence.class); personLegalDefence.setSeverity(PersonLegalDefenceData.SEVERITIES[i]); personLegalDefence.save(); PersonLegalDefenceData.ids[i] = personLegalDefence.getID(); final PersonSuit personSuit = entityManager.create(PersonSuit.class); personSuit.setPerson(person); personSuit.setPersonLegalDefence(personLegalDefence); personSuit.save(); PersonSuitData.ids[i] = personSuit.getID(); } return personLegalDefences; }
private Company[] addCompanies(EntityManager entityManager) throws Exception { final Company[] companies = new Company[CompanyData.ids.length]; for (int i = 0; i < companies.length; i++) { // the image final InputStream is = new ByteArrayInputStream(CompanyData.IMAGES[i]); final Company company = entityManager.create(Company.class); company.setName(CompanyData.NAMES[i]); company.setCool(CompanyData.COOLS[i]); company.setImage(is); company.save(); is.close(); // for good measure, no need to worry to much about finally etc. here // set the company data CompanyData.ids[i] = company.getCompanyID(); companies[i] = company; } return companies; }
private Chair[] addChairs(EntityManager entityManager, Person person) throws Exception { final Chair[] chairs = new Chair[ChairData.ids.length]; for (int i = 0; i < ChairData.ids.length; i++) { final Chair chair = entityManager.create(Chair.class); chair.setColour(ChairData.COLOURS[i]); chair.save(); ChairData.ids[i] = chair.getID(); final PersonChair personChair = entityManager.create(PersonChair.class); personChair.setPerson(person); personChair.setChair(chair); personChair.save(); PersonChairData.ids[i] = personChair.getID(); chairs[i] = chair; } return chairs; }
private Pen[] addPens(EntityManager entityManager, Person person) throws Exception { final Pen[] pens = new Pen[PenData.ids.length]; for (int i = 0; i < PenData.ids.length; i++) { final Pen pen = entityManager.create(Pen.class); pen.setPerson(person); pen.setWidth(PenData.WIDTHS[i]); pen.save(); PenData.ids[i] = pen.getID(); } return pens; }
private Message addMessage(EntityManager entityManager, Address to, Address from, final String content) throws Exception { final String contents = EntityUtils.getFieldName(entityManager, Message.class, "getContents"); final Message message = entityManager.create(Message.class, new DBParam(contents, content)); message.setTo(to); message.setFrom(from); message.save(); return message; }
private Comment createComment(EntityManager entityManager, Commentable commentable, String title, String text, int index) throws Exception { Comment comment = entityManager.create(Comment.class); comment.setTitle(title); comment.setText(text); comment.setCommentable(commentable); comment.setIndex(index); comment.save(); return comment; }
public Person addPerson(EntityManager entityManager, Company company) throws Exception { final Person person = entityManager.create(Person.class); person.setFirstName(PersonData.FIRST_NAME); person.setLastName(PersonData.LAST_NAME); person.setCompany(company); person.setProfession(PersonData.PROFESSION); person.setImage(PersonData.IMAGE); person.save(); // save the id PersonData.id = person.getID(); return person; }
private Magazine[] addMagazines(EntityManager entityManager) throws Exception { final Magazine[] magazines = new Magazine[MagazineData.ids.length]; for (int i = 0; i < MagazineData.ids.length; i++) { final Magazine magazine = entityManager.create(Magazine.class); magazine.setTitle(MagazineData.TITLES[i]); magazine.save(); MagazineData.ids[i] = magazine.getID(); for (int j = 0; j < MagazineData.AUTHOR_IDS[i].length; j++) { final Author author = addAuthor(entityManager, magazine, "Magazine author", j); MagazineData.AUTHOR_IDS[i][j] = author.getID(); } final Distribution print = addPrintDistribution(entityManager, magazine, 9007); final Distribution online = addOnlineDistribution(entityManager, magazine, "http://amazon.example.com"); MagazineData.DISTRIBUTION_IDS[i][0] = print.getID(); MagazineData.DISTRIBUTION_TYPES[i][0] = print.getEntityType(); MagazineData.DISTRIBUTION_IDS[i][1] = online.getID(); MagazineData.DISTRIBUTION_TYPES[i][1] = online.getEntityType(); magazines[i] = magazine; } return magazines; }
private Book[] addBooks(EntityManager entityManager) throws Exception { final Book[] books = new Book[BookData.ids.length]; for (int i = 0; i < BookData.ids.length; i++) { final Book book = entityManager.create(Book.class); book.setTitle(BookData.TITLES[i]); book.setHardcover(BookData.COVERS[i]); book.save(); BookData.ids[i] = book.getID(); for (int j = 0; j < BookData.AUTHOR_IDS[i].length; j++) { final Author author = addAuthor(entityManager, book, "Book author", j); BookData.AUTHOR_IDS[i][j] = author.getID(); } final Distribution print = addPrintDistribution(entityManager, book, 10003); final Distribution online = addOnlineDistribution(entityManager, book, "http://amazon.example.com"); BookData.DISTRIBUTION_IDS[i][0] = print.getID(); BookData.DISTRIBUTION_TYPES[i][0] = print.getEntityType(); BookData.DISTRIBUTION_IDS[i][1] = online.getID(); BookData.DISTRIBUTION_TYPES[i][1] = online.getEntityType(); books[i] = book; } return books; }