Package cronapi.database
Class DataSource
- java.lang.Object
-
- cronapi.database.DataSource
-
- All Implemented Interfaces:
com.fasterxml.jackson.databind.JsonSerializable
public class DataSource extends Object implements com.fasterxml.jackson.databind.JsonSerializable
Class database manipulation, responsible for querying, inserting, updating and deleting database data procedurally, allowing paged navigation and setting page size.- Since:
- 2017-04-26
- Version:
- 1.0
- Author:
- robson.ataide
-
-
Constructor Summary
Constructors Constructor Description DataSource(com.google.gson.JsonObject query)DataSource(String entity)Init a datasource with a page size equals 100DataSource(String entity, int pageSize)Init a datasource setting a page sizeDataSource(String entity, javax.persistence.EntityManager entityManager)Init a datasource with a page size equals 100, and custom entity manager
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidalowFetchNext(boolean alowFetchNext)voidcheckRESTSecurity(String method)voidcheckRESTSecurity(String relationId, String method)voidclear()Clean Datasource and to free up allocated memorylongcount()voiddelete()Removes the object in the current indexvoiddelete(Var[] primaryKeys)voiddeleteRelation(String refId, Var[] primaryKeys, Var[] relationKeys)voiddisableMultiTenant()voidenableMultiTenant()Varexecute(String query, Var... params)Execute QueryObject[]fetch()Retrieve objects from database using repository when filter is null or empty, if filter not null or is not empty, this method uses entityManager and create a jpql instruction.Object[]fetch(boolean isCount)voidfilter(Var data, Var[] extraParams)voidfilter(String filter, Var... params)Fetch objects from database by a filtervoidfilter(String filter, org.springframework.data.domain.PageRequest pageRequest, Var... params)voidfilterByPk(Var[] params)voidfilterByRelation(String refId, org.springframework.data.domain.PageRequest pageRequest, Var... primaryKeys)voidflush()intgetCurrent()ClassgetDomainClass()StringgetEntity()javax.persistence.EntityManagergetEntityManager(Class domainClass)StringgetFilter()VargetId()VargetIds()EntityMetadatagetMetadata()ObjectgetObject()Return object in current indexObjectgetObject(String fieldName)Return field passed from object in current indexObjectgetObjectWithId(Var[] ids)org.springframework.data.domain.PagegetPage()Gets a Pageable object retrieved from repositoryStringgetRelationEntity(String relationId)StringgetSimpleEntity()VargetTotalElements()booleanhasData()booleanhasNext()Verify if can moves the index for next position, in pageable case, looking for next page and so onvoidinsert()Create a new instance of entity and add a results and set current (index) for his positionvoidinsert(Object value)ObjectinsertRelation(String refId, Map<?,?> data, Var... primaryKeys)booleanisPlainData()voidnext()Moves the index for next position, in pageable case, looking for next page and so onvoidnextOnPage()Moves the index for next position, in pageable case, looking for next page and so onbooleanprevious()Moves the index for previous position, in pageable case, looking for next page and so onvoidresolveRelation(String refId)Objectsave()Objectsave(boolean returnCursorAfterInsert)Objectsave(boolean returnCursorAfterInsert, boolean returnRefreshed)Saves the object in the current index or a new object when has insertedElementObjectsaveAfterCommit(org.eclipse.persistence.internal.sessions.AbstractSession session)voidserialize(com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider serializers)voidserializeWithType(com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider serializers, com.fasterxml.jackson.databind.jsontype.TypeSerializer typeSer)voidsetCurrent(int current)voidsetDataSourceFilter(DataSourceFilter dsFilter)voidsetPageSize(int pageSize)Create a new page request with size passedvoidsetPlainData(boolean plainData)voidsetUseOdataRequest(boolean useOdataRequest)voidsetUseOffset(boolean useOffset)voidsetUseUrlParams(boolean useUrlParams)ObjecttoObject(Map<?,?> values)StringtoString()voidupdate(Var data)voidupdateField(String fieldName, Object fieldValue)Update a field from object in the current indexvoidupdateFields(Var... fields)Update fields from object in the current indexbooleanuseUrlParams()voidvalidate(String jpql)
-
-
-
Constructor Detail
-
DataSource
public DataSource(String entity)
Init a datasource with a page size equals 100- Parameters:
entity- - full name of entitiy class like String
-
DataSource
public DataSource(com.google.gson.JsonObject query)
-
DataSource
public DataSource(String entity, javax.persistence.EntityManager entityManager)
Init a datasource with a page size equals 100, and custom entity manager- Parameters:
entity- - full name of entitiy class like StringentityManager- - custom entity manager
-
DataSource
public DataSource(String entity, int pageSize)
Init a datasource setting a page size- Parameters:
entity- - full name of entitiy class like StringpageSize- - page size of a Pageable object retrieved from repository
-
-
Method Detail
-
getEntityManager
public javax.persistence.EntityManager getEntityManager(Class domainClass)
-
getDomainClass
public Class getDomainClass()
-
getSimpleEntity
public String getSimpleEntity()
-
getEntity
public String getEntity()
-
count
public long count()
-
fetch
public Object[] fetch()
Retrieve objects from database using repository when filter is null or empty, if filter not null or is not empty, this method uses entityManager and create a jpql instruction.- Returns:
- a array of Object
-
fetch
public Object[] fetch(boolean isCount)
-
getMetadata
public EntityMetadata getMetadata()
-
insert
public void insert()
Create a new instance of entity and add a results and set current (index) for his position
-
insert
public void insert(Object value)
-
save
public Object save()
-
save
public Object save(boolean returnCursorAfterInsert)
-
save
public Object save(boolean returnCursorAfterInsert, boolean returnRefreshed)
Saves the object in the current index or a new object when has insertedElement
-
saveAfterCommit
public Object saveAfterCommit(org.eclipse.persistence.internal.sessions.AbstractSession session)
-
delete
public void delete(Var[] primaryKeys)
-
delete
public void delete()
Removes the object in the current index
-
updateField
public void updateField(String fieldName, Object fieldValue)
Update a field from object in the current index- Parameters:
fieldName- - attributte name in entityfieldValue- - value that replaced or inserted in field name passed
-
updateFields
public void updateFields(Var... fields)
Update fields from object in the current index- Parameters:
fields- - bidimensional array like fields sample: { {"name", "Paul"}, {"age", "21"} }
-
filterByPk
public void filterByPk(Var[] params)
-
update
public void update(Var data)
-
getObject
public Object getObject()
Return object in current index- Returns:
- Object from database in current position
-
getObject
public Object getObject(String fieldName)
Return field passed from object in current index- Returns:
- Object value of field passed
-
next
public void next()
Moves the index for next position, in pageable case, looking for next page and so on
-
nextOnPage
public void nextOnPage()
Moves the index for next position, in pageable case, looking for next page and so on
-
hasNext
public boolean hasNext()
Verify if can moves the index for next position, in pageable case, looking for next page and so on- Returns:
- boolean true if has next, false else
-
hasData
public boolean hasData()
-
previous
public boolean previous()
Moves the index for previous position, in pageable case, looking for next page and so on- Returns:
- boolean true if has previous, false else
-
setCurrent
public void setCurrent(int current)
-
getCurrent
public int getCurrent()
-
getPage
public org.springframework.data.domain.Page getPage()
Gets a Pageable object retrieved from repository- Returns:
- pageable from repository, returns null when fetched by filter
-
setPageSize
public void setPageSize(int pageSize)
Create a new page request with size passed- Parameters:
pageSize- size of page request
-
filter
public void filter(String filter, Var... params)
Fetch objects from database by a filter- Parameters:
filter- jpql instruction like a namedQueryparams- parameters used in jpql instruction
-
setDataSourceFilter
public void setDataSourceFilter(DataSourceFilter dsFilter)
-
filter
public void filter(String filter, org.springframework.data.domain.PageRequest pageRequest, Var... params)
-
alowFetchNext
public void alowFetchNext(boolean alowFetchNext)
-
resolveRelation
public void resolveRelation(String refId)
-
filterByRelation
public void filterByRelation(String refId, org.springframework.data.domain.PageRequest pageRequest, Var... primaryKeys)
-
clear
public void clear()
Clean Datasource and to free up allocated memory
-
execute
public Var execute(String query, Var... params)
Execute Query- Parameters:
query- - JPQL instruction for filter objects to removeparams- - Bidimentional array with params name and params value
-
getTotalElements
public Var getTotalElements()
-
serialize
public void serialize(com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider serializers) throws IOException- Specified by:
serializein interfacecom.fasterxml.jackson.databind.JsonSerializable- Throws:
IOException
-
serializeWithType
public void serializeWithType(com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider serializers, com.fasterxml.jackson.databind.jsontype.TypeSerializer typeSer) throws IOException- Specified by:
serializeWithTypein interfacecom.fasterxml.jackson.databind.JsonSerializable- Throws:
IOException
-
checkRESTSecurity
public void checkRESTSecurity(String relationId, String method) throws Exception
- Throws:
Exception
-
getRelationEntity
public String getRelationEntity(String relationId) throws Exception
- Throws:
Exception
-
disableMultiTenant
public void disableMultiTenant()
-
enableMultiTenant
public void enableMultiTenant()
-
getFilter
public String getFilter()
-
getIds
public Var getIds()
-
getId
public Var getId()
-
validate
public void validate(String jpql)
-
isPlainData
public boolean isPlainData()
-
setPlainData
public void setPlainData(boolean plainData)
-
useUrlParams
public boolean useUrlParams()
-
setUseUrlParams
public void setUseUrlParams(boolean useUrlParams)
-
flush
public void flush()
-
setUseOdataRequest
public void setUseOdataRequest(boolean useOdataRequest)
-
setUseOffset
public void setUseOffset(boolean useOffset)
-
-