@Override public <T, ID extends Serializable> PojoRepository<T, ID> newPojoRepository(Class<T> clazz, Class<ID> idClass) { return new PojoRepositoryImpl<>(this, clazz, idClass); }
@Override public long count(Transaction transaction) { return count((PojoQueryDefinition) null, transaction); }
@Override public void write(T entity, Transaction transaction, String... collections) { if ( entity == null ) return; JacksonDatabindHandle<T> contentHandle = new JacksonDatabindHandle<>(entity); contentHandle.setMapper(objectMapper); DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); metadataHandle = metadataHandle.withCollections(entityClass.getName()); if ( collections != null && collections.length > 0 ) { metadataHandle = metadataHandle.withCollections(collections); } DocumentWriteSet writeSet = docMgr.newWriteSet(); writeSet.add(getDocumentUri(entity), metadataHandle, contentHandle); try { docMgr.write(writeSet, transaction); } catch(MarkLogicIOException e) { checkForEmptyBeans(e); throw e; } }
@Override public void delete(ID[] ids, Transaction transaction) { for ( ID id : ids ) { docMgr.delete(createUri(id), transaction); } }
PojoRepositoryImpl(DatabaseClient client, Class<T> entityClass, Class<ID> idClass) { this(client, entityClass); this.idClass = idClass; findId(); if ( idMethod == null && idProperty == null ) { throw new IllegalArgumentException("Your class " + entityClass.getName() + " does not have a method or field annotated with com.marklogic.client.pojo.annotation.Id"); } }
@Override public void delete(ID... ids) { delete(ids, null); }
@Override public void deleteAll() { deleteAll(null); }
private void checkForEmptyBeans(Throwable e) { Throwable cause = e.getCause(); if ( cause != null ) { if ( cause instanceof JsonMappingException && cause.getMessage() != null && cause.getMessage().contains("SerializationFeature.FAIL_ON_EMPTY_BEANS") ) { throw new MarkLogicBindingException( "Each of your pojo beans and descendent beans must have public fields or paired get/set methods", cause); } else { checkForEmptyBeans(cause); } } }
@Test public void testIssue_93() throws Exception { PojoRepository<Student,Long> students = Common.client.newPojoRepository(Student.class, Long.class); long id=1; Student stud = new Student(); stud.setName("Student1"); stud.setStudId(id); Address adr = new Address(); adr.setCountry("USA"); adr.setState("CA"); adr.setZip(94070); adr.setStreetName("1 tassman"); stud.setAddress(adr); stud.setEmailId("stud@gmail.com"); stud.setPhone(6602345); stud.setNationality("Indian"); stud.setclassStatus(std_status.junior); @SuppressWarnings("rawtypes") ObjectMapper objectMapper = ((PojoRepositoryImpl) students).getObjectMapper(); String value = objectMapper.writeValueAsString(stud); objectMapper.readValue(value, Student.class); students.write(stud, "students"); Student student1 = students.read(id); assertEquals("Student id", student1.getStudId(), stud.getStudId()); assertEquals("Zip code", student1.getAddress().zip, stud.getAddress().zip); assertEquals("class status", student1.getclassStatus(), stud.getclassStatus()); }
@Override public boolean exists(ID id) { return docMgr.exists(createUri(id)) != null; }
@SuppressWarnings("unchecked") public ID getId(T entity) { findId(); if ( idMethod != null ) { try { return (ID) idMethod.invoke(entity); } catch (Exception e) { throw new IllegalStateException("Error invoking " + entityClass.getName() + " method " + idMethod.getName(), e); } } else if ( idProperty != null ) { try { return (ID) idProperty.get(entity); } catch (Exception e) { throw new IllegalStateException("Error retrieving " + entityClass.getName() + " field " + idProperty.getName(), e); } } else { throw new IllegalArgumentException("Your class " + entityClass.getName() + " does not have a method or field annotated with com.marklogic.client.pojo.annotation.Id"); } } }
@Override public long count(String... collections) { return count(collections, null); }
@Override public boolean exists(ID id, Transaction transaction) { return docMgr.exists(createUri(id), transaction) != null; }
@Override public long count() { return count((PojoQueryDefinition) null, null); }
@Override public PojoPage<T> read(ID[] ids, Transaction transaction) { List<String> uris = new ArrayList<>(); for ( ID id : ids ) { uris.add(createUri(id)); } DocumentPage docPage = (DocumentPage) docMgr.read(transaction, uris.toArray(new String[0])); PojoPage<T> pojoPage = new PojoPageImpl<>(docPage, entityClass); return pojoPage; } @Override
@Override public long count(PojoQueryDefinition query) { return count((PojoQueryDefinition) query, null); }
@Override public long count(String[] collections, Transaction transaction) { if ( collections != null && collections.length > 0 ) { if ( collections.length > 1 || collections[0] != null ) { return count(qb.collection(collections), transaction); } } return count((PojoQueryDefinition) null, transaction); }