I have a REST endpoint as shown below.
#Path("/consumers")
#Produces("application/x.com.abc.pqr.audit.v2+json")
#Consumes("application/x.com.abc.pqr.audit.v2+json")
public interface ConsumerEndpoint {
#GET
#Path("paged")
Page<Module> getConsumersOfDependencyByPage(#BeanParam ConsumerQueryParams params);
}
As you can see above, I am using #BeanParam to map the query parameters passed from the front end side.
The ConsumerQueryParams class is shown below.
public class ConsumerQueryParams implements Serializable{
private static final long serialVersionUID = 6440255704974023223L;
#QueryParam("pageNum") #DefaultValue("1") private int pageNum;
#QueryParam("pageSize") #DefaultValue("25") private int pageSize;
#QueryParam("groupId") private String groupId;
#QueryParam("artifactId") private String artifactId;
#QueryParam("version") private String version;
#QueryParam("groupIdFilter") private String groupIdFilter;
#QueryParam("artifactIdFilter") private String artifactIdFilter;
#QueryParam("versionFilter") private String versionFilter;
public ConsumerQueryParams() {
}
private ConsumerQueryParams(Builder builder) {
this.pageNum = builder.pageNum;
this.pageSize = builder.pageSize;
this.groupId = builder.groupId;
this.artifactId = builder.artifactId;
this.version = builder.version;
this.groupIdFilter = builder.groupIdFilter;
this.artifactIdFilter = builder.artifactIdFilter;
this.versionFilter = builder.versionFilter;
}
public int getPageNum() {
return pageNum;
}
public int getPageSize() {
return pageSize;
}
public String getGroupId() {
return groupId;
}
public String getArtifactId() {
return artifactId;
}
public String getVersion() {
return version;
}
public String getGroupIdFilter() {
return groupIdFilter;
}
public String getArtifactIdFilter() {
return artifactIdFilter;
}
public String getVersionFilter() {
return versionFilter;
}
#Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(!(obj instanceof ConsumerQueryParams))
return false;
ConsumerQueryParams other = (ConsumerQueryParams) obj;
return Objects.equals(pageNum, other.pageNum) &&
Objects.equals(pageSize, other.pageSize) &&
Objects.equals(groupId, other.groupId) &&
Objects.equals(artifactId, other.artifactId) &&
Objects.equals(version, other.version) &&
Objects.equals(groupIdFilter, other.groupIdFilter) &&
Objects.equals(artifactIdFilter, other.artifactIdFilter) &&
Objects.equals(versionFilter, other.versionFilter);
}
#Override
public int hashCode() {
return Objects.hash(pageNum, pageSize, groupId, artifactId, version, groupIdFilter, artifactIdFilter, versionFilter);
}
public static class Builder {
private int pageNum;
private int pageSize;
private String groupId;
private String artifactId;
private String version;
private String groupIdFilter;
private String artifactIdFilter;
private String versionFilter;
public Builder(int pageNum, int pageSize, String groupId, String artifactId) {
Preconditions.checkArgument(pageNum > 0, "pageNum must be greater than 0.");
Preconditions.checkArgument(pageSize > 0, "pageSize must be greater than 0.");
Preconditions.checkNotNull(groupId, "groupId is null");
Preconditions.checkNotNull(artifactId, "artifactId is null");
this.pageNum = pageNum;
this.pageSize = pageSize;
this.groupId = groupId;
this.artifactId = artifactId;
}
public Builder setVersion(String version) {
this.version = version;
return this;
}
public Builder setGroupIdFilter(String groupIdFilter) {
this.groupIdFilter = groupIdFilter;
return this;
}
public Builder setArtifactIdFilter(String artifactIdFilter) {
this.artifactIdFilter = artifactIdFilter;
return this;
}
public Builder setVersionFilter(String versionFilter) {
this.versionFilter = versionFilter;
return this;
}
public ConsumerQueryParams build() {
return new ConsumerQueryParams(this);
}
}
}
You can see that I am using the Builder pattern to set the variables.
I am using the below url to access the above specified endpoint.
http://localhost:8080/rest/api/consumers/paged?groupId=org.slf4j&artifactId=slf4j-api&groupIdFilter=sdlc
Everything works fine. I am able to get the data on Postman successfully.
Now I have a requirement where I need to rename the groupIdFilter, artifactIdFilter, versionFilter query params in ConsumerQueryParams class to consumerGroupIdFilter, consumerArtifactIdFilter and consumerVersionFilter respectively.
After changing the variable names in ConsumerQueryParams class, it looks like this:
public class ConsumerQueryParams implements Serializable{
private static final long serialVersionUID = 6440255704974023223L;
#QueryParam("pageNum") #DefaultValue("1") private int pageNum;
#QueryParam("pageSize") #DefaultValue("25") private int pageSize;
#QueryParam("groupId") private String groupId;
#QueryParam("artifactId") private String artifactId;
#QueryParam("version") private String version;
#QueryParam("groupIdFilter") private String consumerGroupIdFilter;
#QueryParam("artifactIdFilter") private String consumerArtifactIdFilter;
#QueryParam("versionFilter") private String consumerVersionFilter;
public ConsumerQueryParams() {
}
private ConsumerQueryParams(Builder builder) {
this.pageNum = builder.pageNum;
this.pageSize = builder.pageSize;
this.groupId = builder.groupId;
this.artifactId = builder.artifactId;
this.version = builder.version;
this.consumerGroupIdFilter = builder.consumerGroupIdFilter;
this.consumerArtifactIdFilter = builder.consumerArtifactIdFilter;
this.consumerVersionFilter = builder.consumerVersionFilter;
}
public int getPageNum() {
return pageNum;
}
public int getPageSize() {
return pageSize;
}
public String getGroupId() {
return groupId;
}
public String getArtifactId() {
return artifactId;
}
public String getVersion() {
return version;
}
public String getConsumerGroupIdFilter() {
return consumerGroupIdFilter;
}
public String getConsumerArtifactIdFilter() {
return consumerArtifactIdFilter;
}
public String getConsumerVersionFilter() {
return consumerVersionFilter;
}
#Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if(!(obj instanceof ConsumerQueryParams))
return false;
ConsumerQueryParams other = (ConsumerQueryParams) obj;
return Objects.equals(pageNum, other.pageNum) &&
Objects.equals(pageSize, other.pageSize) &&
Objects.equals(groupId, other.groupId) &&
Objects.equals(artifactId, other.artifactId) &&
Objects.equals(version, other.version) &&
Objects.equals(consumerGroupIdFilter, other.consumerGroupIdFilter) &&
Objects.equals(consumerArtifactIdFilter, other.consumerArtifactIdFilter) &&
Objects.equals(consumerVersionFilter, other.consumerVersionFilter);
}
#Override
public int hashCode() {
return Objects.hash(pageNum, pageSize, groupId, artifactId, version, consumerGroupIdFilter, consumerArtifactIdFilter, consumerVersionFilter);
}
public static class Builder {
private int pageNum;
private int pageSize;
private String groupId;
private String artifactId;
private String version;
private String consumerGroupIdFilter;
private String consumerArtifactIdFilter;
private String consumerVersionFilter;
public Builder(int pageNum, int pageSize, String groupId, String artifactId) {
Preconditions.checkArgument(pageNum > 0, "pageNum must be greater than 0.");
Preconditions.checkArgument(pageSize > 0, "pageSize must be greater than 0.");
Preconditions.checkNotNull(groupId, "groupId is null");
Preconditions.checkNotNull(artifactId, "artifactId is null");
this.pageNum = pageNum;
this.pageSize = pageSize;
this.groupId = groupId;
this.artifactId = artifactId;
}
public Builder setVersion(String version) {
this.version = version;
return this;
}
public Builder setConsumerGroupIdFilter(String consumerGroupIdFilter) {
this.consumerGroupIdFilter = consumerGroupIdFilter;
return this;
}
public Builder setConsumerArtifactIdFilter(String consumerArtifactIdFilter) {
this.consumerArtifactIdFilter = consumerArtifactIdFilter;
return this;
}
public Builder setConsumerVersionFilter(String consumerVersionFilter) {
this.consumerVersionFilter = consumerVersionFilter;
return this;
}
public ConsumerQueryParams build() {
return new ConsumerQueryParams(this);
}
}
}
Now I am trying to access the same endpoint with the url:
http://localhost:8080/rest/api/consumers/paged?groupId=org.slf4j&artifactId=slf4j-api&consumerGroupIdFilter=sdlc
But this is not working. The consumerGroupIdFilter query param in the url is not being mapped to the consumerGroupIdFilter variable of the ConsumerQueryParams object, whereas groupId and artifactId gets mapped.
I am not sure why this is happening. As far as I know, the ConsumerQueryParams class has the correct code. All that I did was to change the variable names and updated the getters and setters in the Builder class.
Can anyone help me here.
The problem is that the url has the new name and the annotation has the old one
#QueryParam("groupIdFilter")
consumerGroupIdFilter
Related
I'm trying to parse XML content with Jackson but I have some difficulties with Boolean value.
This is a part of my XML content :
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body xmlns:ns1="http://somecontent">
<wsResponse xmlns="http://somecontent">
<responseType>SUCCESS</responseType>
<response>
<successfullResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="InterchangeSearchResponse">
<totalResult>1</totalResult>
<returnedResults>1</returnedResults>
<pageIndex>1</pageIndex>
<interchanges>
<interchange>
<depositId>somecontent</depositId>
<interchangeId>somecontent</interchangeId>
<depositDate>2021-03-26T11:45:05.000+01:00</depositDate>
<depositSubject>dépôt WS</depositSubject>
<numADS>number</numADS>
<adsDate>2021-03-26T11:45:05.000+01:00</adsDate>
<alias>contentAsString</alias>
<version xsi:nil="true"/>
<isTest>false</isTest>
<deposantAccount>
<name>someString</name>
</deposantAccount>
<teleProcedure>someString</teleProcedure>
<statesHistory>
This is my XML structure as class :
#JacksonXmlProperty(localName = "Body")
private Body body;
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
}
#JacksonXmlRootElement
public class Body {
#JacksonXmlProperty(localName = "Fault")
private Fault fault;
private WsResponse wsResponse;
public Fault getFault() {
return fault;
}
public void setFault(Fault fault) {
this.fault = fault;
}
public WsResponse getWsResponse() {
return wsResponse;
}
public void setWsResponse(WsResponse wsResponse) {
this.wsResponse = wsResponse;
}
}
#JacksonXmlRootElement(localName = "wsResponse")
public class WsResponse {
private String responseType;
private Response response;
public String getResponseType() {
return responseType;
}
public void setResponseType(String responseType) {
this.responseType = responseType;
}
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
}
#JacksonXmlRootElement
public class Response {
private SuccessfulResponse successfullResponse;
private ErrorResponse errorResponse;
public SuccessfulResponse getSuccessfullResponse() {
return successfullResponse;
}
public void setSuccessfullResponse(SuccessfulResponse successfullResponse) {
this.successfullResponse = successfullResponse;
}
public ErrorResponse getErrorResponse() {
return errorResponse;
}
public void setErrorResponse(ErrorResponse errorResponse) {
this.errorResponse = errorResponse;
}
}
#JacksonXmlRootElement
public class SuccessfulResponse {
/**
* Response when add document success
*/
private String depositId;
/**
* Response when get interchanges by deposit id success
*/
private String type;
private Integer totalResult;
private Integer returnedResults;
private Integer pageIndex;
private Interchanges interchanges;
/**
* Response when get declaration details success
*/
private DeclarationTdfc declarationTdfc;
public SuccessfulResponse() {}
public SuccessfulResponse(String depositId) {
this.depositId = depositId;
}
public SuccessfulResponse(String type, Integer totalResult, Integer returnedResults, Integer pageIndex, Interchanges interchanges) {
this.type = type;
this.totalResult = totalResult;
this.returnedResults = returnedResults;
this.pageIndex = pageIndex;
this.interchanges = interchanges;
}
public SuccessfulResponse(DeclarationTdfc declarationTdfc) {
this.declarationTdfc = declarationTdfc;
}
public SuccessfulResponse(String depositId, String type, Integer totalResult, Integer returnedResults,
Integer pageIndex, Interchanges interchanges, DeclarationTdfc declarationTdfc) {
super();
this.depositId = depositId;
this.type = type;
this.totalResult = totalResult;
this.returnedResults = returnedResults;
this.pageIndex = pageIndex;
this.interchanges = interchanges;
this.declarationTdfc = declarationTdfc;
}
public String getDepositId() {
return depositId;
}
public void setDepositId(String depositId) {
this.depositId = depositId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getTotalResult() {
return totalResult;
}
public void setTotalResult(Integer totalResult) {
this.totalResult = totalResult;
}
public Integer getReturnedResults() {
return returnedResults;
}
public void setReturnedResults(Integer returnedResults) {
this.returnedResults = returnedResults;
}
public Integer getPageIndex() {
return pageIndex;
}
public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}
public Interchanges getInterchanges() {
return interchanges;
}
public void setInterchanges(Interchanges interchanges) {
this.interchanges = interchanges;
}
public DeclarationTdfc getDeclarationTdfc() {
return declarationTdfc;
}
public void setDeclarationTdfc(DeclarationTdfc declarationTdfc) {
this.declarationTdfc = declarationTdfc;
}
}
public class Interchanges {
#JacksonXmlProperty(localName = "interchange")
#JacksonXmlElementWrapper(useWrapping = false)
private List<Interchange> interchange;
public Interchanges() { super(); }
public Interchanges(List<Interchange> interchange) {
super();
this.interchange = interchange;
}
public List<Interchange> getInterchange() {
return interchange;
}
public void setInterchange(List<Interchange> interchange) {
this.interchange = interchange;
}
}
public class Interchange {
private String depositId;
private Integer interchangeId;
//#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = SelfmedConstants.Dates.ENGLISH_DATETIME_PATTERN)
private String depositDate;
private String depositSubject;
private String numADS;
//#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = SelfmedConstants.Dates.ENGLISH_DATETIME_PATTERN)
private String adsDate;
private String alias;
//#JacksonXmlProperty(isAttribute = true)
private String version;
#JsonSerialize(using = BooleanSerializer.class)
#JsonDeserialize(using = BooleanDeserializer.class)
#JacksonXmlProperty(localName = "isTest")
private Boolean isTest;
#JacksonXmlProperty(localName = "name")
#JacksonXmlElementWrapper(localName = "deposantAccount")
private List<String> name;
private String teleProcedure;
private StatesHistory statesHistory;
#JacksonXmlProperty(localName = "declarationId")
#JacksonXmlElementWrapper(localName = "declarationIds")
private List<String> declarationId;
public Interchange() {
}
public Interchange(String depositId, Integer interchangeId, String depositDate, String depositSubject, String numADS,
String adsDate, String alias, String version, Boolean isTest, List<String> name, String teleProcedure,
StatesHistory statesHistory, List<String> declarationId) {
this();
this.depositId = depositId;
this.interchangeId = interchangeId;
this.depositDate = depositDate;
this.depositSubject = depositSubject;
this.numADS = numADS;
this.adsDate = adsDate;
this.alias = alias;
this.version = version;
this.isTest = isTest;
this.name = name;
this.teleProcedure = teleProcedure;
this.statesHistory = statesHistory;
this.declarationId = declarationId;
}
public String getDepositId() {
return depositId;
}
public void setDepositId(String depositId) {
this.depositId = depositId;
}
public Integer getInterchangeId() {
return interchangeId;
}
public void setInterchangeId(Integer interchangeId) {
this.interchangeId = interchangeId;
}
public String getDepositDate() {
return depositDate;
}
public void setDepositDate(String depositDate) {
this.depositDate = depositDate;
}
public String getDepositSubject() {
return depositSubject;
}
public void setDepositSubject(String depositSubject) {
this.depositSubject = depositSubject;
}
public String getNumADS() {
return numADS;
}
public void setNumADS(String numADS) {
this.numADS = numADS;
}
public String getAdsDate() {
return adsDate;
}
public void setAdsDate(String adsDate) {
this.adsDate = adsDate;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Boolean getIsTest() {
return isTest;
}
public void setIsTest(Boolean isTest) {
this.isTest = isTest;
}
public void setIsTest(String isTest) {
this.isTest = Boolean.valueOf(isTest);
}
public List<String> getName() {
return name;
}
public void setName(List<String> name) {
this.name = name;
}
public String getTeleProcedure() {
return teleProcedure;
}
public void setTeleProcedure(String teleProcedure) {
this.teleProcedure = teleProcedure;
}
public StatesHistory getStatesHistory() {
return statesHistory;
}
public void setStatesHistory(StatesHistory statesHistory) {
this.statesHistory = statesHistory;
}
public List<String> getDeclarationId() {
return declarationId;
}
public void setDeclarationId(List<String> declarationId) {
this.declarationId = declarationId;
}
}
As you can see in the Interchange class, I try some stuff but nothing work.
I generate my class like that :
JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(xmlModule);
xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
xmlMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES);
System.out.println(responseAsString);
Envelope envelope = xmlMapper.readValue(responseAsString, new TypeReference<>() {
But when I try to parse my content, I got this exception :
Cannot construct instance of payload.response.Interchange (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('false')
at [Source: (StringReader); line: 20, column: 28] (through reference chain: payload.response.Envelope["Body"]->payload.response.Body["wsResponse"]->payload.response.WsResponse["response"]->payload.response.Response["successfullResponse"]->payload.response.SuccessfulResponse["interchanges"]->payload.response.Interchanges["interchange"]->java.util.ArrayList[1])
I try a lot of things but nothing work so I'm wondering if the problem may be not here...
If you have any solution or leads to explore, please let me know !
Thank.
According to JavaBean spec you have to name getter isTest().
I'm trying to do paging, very similar to the option of #RepositoryRestResource
but only on object that isn't Entity.
the code:
public class FloorplanDevice {
private String FloorplanName;
private Long FloorplanId;
private Long DeviceId;
private String DeviceName;
private String macAddress;
private Long groupId;
private LocatableType deviceType;
public String getFloorplanName() {
return FloorplanName;
}
public void setFloorplanName(String floorplanName) {
FloorplanName = floorplanName;
}
public Long getFloorplanId() {
return FloorplanId;
}
public void setFloorplanId(Long floorplanId) {
FloorplanId = floorplanId;
}
public Long getDeviceId() {
return DeviceId;
}
public void setDeviceId(Long deviceId) {
DeviceId = deviceId;
}
public String getDeviceName() {
return DeviceName;
}
public void setDeviceName(String deviceName) {
DeviceName = deviceName;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
public LocatableType getDeviceType() {
return deviceType;
}
public void setDeviceType(LocatableType deviceType) {
this.deviceType = deviceType;
}
public FloorplanDevice() {
}
public FloorplanDevice(String floorplanName, Long floorplanId, Long deviceId, String deviceName, String macAddress, Long groupId, LocatableType deviceType) {
FloorplanName = floorplanName;
FloorplanId = floorplanId;
DeviceId = deviceId;
DeviceName = deviceName;
this.macAddress = macAddress;
this.groupId = groupId;
this.deviceType = deviceType;
}
}
This object doesn't have Repository but it has controller:
#RequestMapping(
path = arrayOf("/floorplanDevice/{groupId}", "/floorplanDevice/{groupId}/"),
method = arrayOf(RequestMethod.GET))
open fun getFloorplanDevice(#PathVariable("groupId") groupId: Long): ResponseEntity<*>{
var floorplanDevice= floorplanService.getFloorplanDevice(groupId)
return ResponseEntity(floorplanDevice, HttpStatus.OK)
}
So how can I do Paging to this object with page number and size (if it possible sorting also)?
I'm using java Spring
Thank you
Try something like this:
public Page<FloorplanDevice> getFloorplanDevice(#PathVariable("groupId") Long groupId,
#PageableDefault Pageable pageable)
List<FloorplanDevice> list = floorplanService.getFloorplanDevice(groupId);
MutableSortDefinition sort = pageable.getSort() != null ?
StreamSupport.stream(pageable.getSort().spliterator(), false)
.findFirst()
.map(it -> new MutableSortDefinition(it.getProperty(), it.isIgnoreCase(), it.isAscending()))
.orElse(null)
: null;
PagedListHolder<FloorplanDevice> pageHolder = new PagedListHolder<>(list, sort);
pageHolder.setPage(pageable.getPageNumber());
pageHolder.setPageSize(pageable.getPageSize());
pageHolder.resort();
List<FloorplanDevice> content = pageHolder.getPageList();
Page<FloorplanDevice> page = new PageImpl<>(content, pageable, list.size());
return page;
}
Solution:
This is my solution by using PageListHolder.
#RequestMapping(
path = arrayOf("/floorplanDevice/{groupId}/page/{pageNum}/size/{sizeNum}", "/floorplanDevice/{groupId}/page/{pageNum}/size/{sizeNum}/"),
method = arrayOf(RequestMethod.GET))
open fun getFloorplanDevicePage(#PathVariable("groupId") groupId: Long, #PathVariable("pageNum") pageNum: Int,#PathVariable("sizeNum") sizeNum: Int): ResponseEntity<*>{
var floorplanDevice= floorplanService.getFloorplanDevice(groupId)
var paging= PagedListHolder<FloorplanDevice>(floorplanDevice)
var setsize= paging.setPageSize(sizeNum)
if(paging.pageCount>pageNum){
var setpage=paging.setPage(pageNum)}
else{
return throw RuntimeException("page: $pageNum is too big, insert smaller page from 0 to ${paging.pageCount-1}")
}
var pageList= paging.pageList
return ResponseEntity(pageList, HttpStatus.OK)
}
public class AfpProcessSummaryDetail implements Serializable {
private String srNo;
private String fileName;
private String status;
private String location;
private String comments;
private Character convertStatus;
private AfpProcessDetail afpProcessDetail;
public AfpProcessSummaryDetail() {
}
public AfpProcessSummaryDetail(String srNo, String fileName, String status, String location, String comments,
AfpProcessDetail afpProcessDetail) {
this.srNo = srNo;
this.fileName = fileName;
this.status = status;
this.location = location;
this.comments = comments;
this.afpProcessDetail = afpProcessDetail;
}
#ManyToOne
#JoinColumn(name = "PROCESSDETAIL")
public AfpProcessDetail getAfpProcessDetail() {
return afpProcessDetail;
}
AfpProcessDetail
public class AfpProcessDetail implements Serializable {
private String processID;
private String processDate;
private Integer fileCount;
private Integer successCount;
private Integer failureCount;
private Character active;
private Set<AfpProcessSummaryDetail> processSummaryDetails = new HashSet<AfpProcessSummaryDetail>(0);
public AfpProcessDetail() {
}
public AfpProcessDetail(String processID, String processDate, Integer fileCount, Integer successCount,
Integer failureCount) {
this.processID = processID;
this.processDate = processDate;
this.fileCount = fileCount;
this.successCount = successCount;
this.failureCount = failureCount;
}
public AfpProcessDetail(String processID, String processDate, Integer fileCount, Integer successCount,
Integer failureCount, Set<AfpProcessSummaryDetail> processSummaryDetails) {
this.processID = processID;
this.processDate = processDate;
this.fileCount = fileCount;
this.successCount = successCount;
this.failureCount = failureCount;
this.processSummaryDetails = processSummaryDetails;
}
#Column(name = "FAILURECOUNT")
public Integer getFailureCount() {
return failureCount;
}
public void setFailureCount(Integer failureCount) {
this.failureCount = failureCount;
}
#Column(name = "FILECOUNT")
public Integer getFileCount() {
return fileCount;
}
public void setFileCount(Integer fileCount) {
this.fileCount = fileCount;
}
#Column(name = "PROCESSDATE")
public String getProcessDate() {
return processDate;
}
public void setProcessDate(String processDate) {
this.processDate = processDate;
}
#Id
#Column(name = "PROCESSID", unique = true, nullable = false)
public String getProcessID() {
return processID;
}
public void setProcessID(String processID) {
this.processID = processID;
}
#Column(name = "SUCESSCOUNT")
public Integer getSuccessCount() {
return successCount;
}
public void setSuccessCount(Integer successCount) {
this.successCount = successCount;
}
#JoinColumn(name="PROCESSDETAIL")
#OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
public Set<AfpProcessSummaryDetail> getProcessSummaryDetails() {
return processSummaryDetails;
}
public void setProcessSummaryDetails(Set<AfpProcessSummaryDetail> processSummaryDetails) {
this.processSummaryDetails = processSummaryDetails;
}
Code for updating
public String updateSummaryDetails(ViewFile viewFile, String codeID) {
if (viewFile != null && codeID != null) {
HibernateTemplate transactionTemplate = new HibernateTemplate(sessionFactory, true);
Object result = transactionTemplate.execute(new HibernateCallback<Object>() {
#Override
public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
AfpProcessSummaryDetail processSummary =null,newProcessSummary =null;
AfpProcessDetail processDetail = (AfpProcessDetail)session.get(AfpProcessDetail.class,codeID);
List<FileProperty> fileList = viewFile.getFileList();
Set<AfpProcessSummaryDetail> setProcessSummary=new HashSet<AfpProcessSummaryDetail>();
Set<AfpProcessSummaryDetail> modSetProcessSummary=new HashSet<AfpProcessSummaryDetail>();
setProcessSummary =processDetail.getProcessSummaryDetails();
Iterator<AfpProcessSummaryDetail> itrProcessSumm=setProcessSummary.iterator();
int srNo = 0;
while (itrProcessSumm.hasNext()){
processSummary =(AfpProcessSummaryDetail)itrProcessSumm.next();
for (FileProperty fileProperty : fileList) {
newProcessSummary =new AfpProcessSummaryDetail();
newProcessSummary.setSrNo(codeID + "" + srNo);
newProcessSummary.setFileName(fileProperty.getName());
newProcessSummary.setLocation(fileProperty.getPath());
newProcessSummary.setComments(fileProperty.getComment());
newProcessSummary.setStatus(fileProperty.getStatus());
newProcessSummary.setConvertStatus(fileProperty.getConvertStatus());
newProcessSummary.setAfpProcessDetail(processDetail);
modSetProcessSummary.add(newProcessSummary);
/*if (processSummary.getFileName().trim().equals(fileProperty.getName().trim())){
System.out.println("Element removed");
itrProcessSumm.remove();
modSetProcessSummary.add(newProcessSummary);
break;
}*/
srNo++;
}
}
// setProcessSummary.addAll(modSetProcessSummary);
processDetail.setProcessSummaryDetails(modSetProcessSummary);
processDetail.setFailureCount(viewFile.getExceptionNo());
processDetail.setSuccessCount(viewFile.getSuccessNo());
processDetail.setActive(viewFile.getActive());
transactionTemplate.flush();
session.merge(processDetail);
System.out.println("updated successfully");
return codeID;
}
});
Desired result
I want to perform a one to many update -AfpProcessSummaryDetail which is related to AfpProcessDetail via Set. When I try to replace the set values for update it tries to update the primary key to null. If I don't replace the updates don't take place. If I set cascade it gives error -a different object with the same identifier value was already associated with the session:
[com.aurionpro.convertor.dto.AfpProcessSummaryDetail#15a236ffc961];
nested exception is org.hibernate.NonUniqueObjectException: a
different object with the same identifier value was already associated
with the session.
Please suggest
I have a Navigation class where I am dynamically creating the navigation I am having two tables folder(it is directory that contains files) and content(it is like files or pages that will render the content on the public site). I have created a Navigation class in which I am having a wrapper class for merging the fields of content into the folder. I have tried using #OrderBy and #OrderColumn but I came to know that it will only work with collections.
List<Folder> folder = folderRepository.findAllByNavDepthLessThanOrderByNavDepthAsc(3);
here I am sorting it with navDepth(this column belongs to Folder entity) I also want to sort it with navOrder(this column belongs to Content entity)
#Service
public class NavigationService {
#Qualifier("jdbcMySQL")
private JdbcTemplate jdbcTemplate;
private FolderRepository folderRepository;
private FolderService folderService;
#Autowired
public NavigationService(JdbcTemplate jdbcTemplate,
FolderRepository folderRepository,
FolderService folderService) {
this.jdbcTemplate = jdbcTemplate;
this.folderRepository = folderRepository;
this.folderService = folderService;
}
#Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public Map<String, NavigationItem> navigationItems() {
// TODO: // CROSS cutting AOP springs
// TODO: http://docs.spring.io/spring/docs/4.2.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#aop
List<Folder> folder = folderRepository.findAllByNavDepthLessThanOrderByNavDepthAsc(3);
// List<Folder> folder = folderService.navigation();
Map<String, NavigationItem> navItems = new LinkedHashMap<String, NavigationService.NavigationItem>();
for (int i = 0; i < folder.size(); i++) {
NavigationItem ni = new NavigationItem();
ni.setNavDepth((int) (folder.get(i).getNavDepth()));
ni.setFileNamePath(folder.get(i).getDirectoryPath());
ni.setFilepath(folder.get(i).getDirectoryPath());
ni.setChildren(folder.get(i).getContent());
for (int k = 0; k < folder.size(); k++) {
if(folder.get(i).getId() == folder.get(k).getParentId()) {
ni.addSubFolder(folder.get(k));
System.out.println(folder.get(i).getTitle());
System.out.println(folder.get(k));
System.out.println("---!!!!!!________----------!!!!!!!!");
}
}
navItems.put(folder.get(i).getTitle(), ni);
}
return navItems;
}
public class NavigationItem {
private long id;
private long parentId;
private String title;
private String fileName;
private String fileNamePath;
private int navDepth;
private int navOrder;
private String parentFileName;
private String filePath;
private String folderName;
#OrderColumn(name="navOrder ASC")
private List<Content> children = new ArrayList();
private ArrayList<Folder> subFolder = new ArrayList();
public void setSubFolder(ArrayList<Folder> subFolder) {
this.subFolder = subFolder;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public ArrayList<Folder> getSubFolder() {
return subFolder;
}
public void addSubFolder(Folder subFolder) {
this.subFolder.add(subFolder);
}
public void setChildren(List<Content> list) {
this.children = list;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getParentId() {
return parentId;
}
public void setParentId(long parentId) {
this.parentId = parentId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileNamePath() {
return fileNamePath;
}
public void setFileNamePath(String fileNamePath) {
this.fileNamePath = fileNamePath;
}
public long getNavDepth() {
return navDepth;
}
public void setNavDepth(int navDepth) {
this.navDepth = navDepth;
}
public long getNavOrder() {
return navOrder;
}
public void setNavOrder(int navOrder) {
this.navOrder = navOrder;
}
public String getParentFileName() {
return parentFileName;
}
public void setParentFileName(String parentFileName) {
this.parentFileName = parentFileName;
}
public List<Content> getChildren() {
return children;
}
public void addChild(Content child) {
children.add(child);
}
public String getFilepath() {
return filePath;
}
public void setFilepath(String filePath) {
this.filePath = filePath;
}
}
}
Use a Comparator<NavigationItem> and pass that to Collections.sort() or similar methods.
The comparator might look like this:
class NavComparator implements Comparator<NavigationItem> {
int specialValueNoChildren = -1; //assuming nav_order is always 0 or greater
int compare(NavigationItem o1, NavigationItem o2) {
int max1 = getMaxNavOrder( o1 );
int max2 = getMaxNavOrder( o2 );
return Integer.compare( max1, max2 );
}
int getMaxNavOrder( NavigationItem ni ) {
int max = specialValueNoChildren;
for( Content child : ni.getChildren() ) {
max = Math.max(max, child.getNavOrder());
}
return max;
}
}
Here the maximum nav order of all children is selected with -1 being the special case of no children. Then the items are compared by their respective children's maximum nav order.
If you need a different order change that accordingly, e.g. by reversing max1 and max2 or by getting the lowest nav order of the children etc.
I have an object with these attributes:
public final class CaseNote {
private final Long caseNoteId;
private final Long subGroupId;
private final String title;
private final String caseNoteTypeCode;
private final Date contactDate;
private final Date completedDateTime;
private final Long personVisitId;
private final Date createdDateTime;
private final Long createdByWorkerId;
private final Long createdByTeamId;
private final List<CaseNoteDetailsDTO> noteDetails = new ArrayList<CaseNoteDetailsDTO>();
private final List<GroupMemberDetailsDTO> selectedMembers = new ArrayList<GroupMemberDetailsDTO>();
private final ReferenceProvider referenceProvider;
private final Date timeOutDate;
public CaseNote(final CaseNotesDTO caseNoteDto, final List<CaseNoteDetailsDTO> noteDetails,
final List<GroupMemberDetailsDTO> selectedMembers, final ReferenceProvider referenceProvider) {
this.caseNoteId = caseNoteDto.getCaseNoteId();
this.subGroupId = caseNoteDto.getSubGroupId();
this.title = caseNoteDto.getTitle();
this.caseNoteTypeCode = caseNoteDto.getCaseNoteTypeCode();
this.contactDate = caseNoteDto.getContactDateTime();
this.completedDateTime = caseNoteDto.getCompletedDateTime();
this.personVisitId = caseNoteDto.getPersonVisitId();
this.createdDateTime = caseNoteDto.getCreatedDateTime();
this.createdByWorkerId = caseNoteDto.getCreatedByWorkerId();
this.createdByTeamId = caseNoteDto.getCreatedByTeamId();
this.timeOutDate = caseNoteDto.getTimeOutDate();
this.noteDetails.clear();
this.selectedMembers.clear();
this.noteDetails.addAll(noteDetails);
Collections.sort(this.noteDetails, new CaseNoteDetailCreatedDateComparator());
this.selectedMembers.addAll(selectedMembers);
this.referenceProvider = referenceProvider;
}
private class CaseNoteDetailCreatedDateComparator implements Comparator<CaseNoteDetailsDTO> {
#Override
public int compare(final CaseNoteDetailsDTO firstCaseNoteDetail, final CaseNoteDetailsDTO secondCaseNoteDetail) {
return firstCaseNoteDetail.getCreatedDateTime().compareTo(secondCaseNoteDetail.getCreatedDateTime());
}
}
public Long getCaseNoteId() {
return caseNoteId;
}
public Long getSubGroupId() {
return subGroupId;
}
public String getTitle() {
return title;
}
public String getCaseNoteTypeCode() {
return caseNoteTypeCode;
}
public Date getContactDate() {
return contactDate;
}
public Date getCompletedDateTime() {
return completedDateTime;
}
public Long getPersonVisitId() {
return personVisitId;
}
public Date getCreatedDateTime() {
return createdDateTime;
}
public Long getCreatedByWorkerId() {
return createdByWorkerId;
}
public Long getCreatedByTeamId() {
return createdByTeamId;
}
public List<CaseNoteDetailsDTO> getNoteDetails() {
return Collections.unmodifiableList(noteDetails);
}
public List<GroupMemberDetailsDTO> getSelectedMembers() {
return Collections.unmodifiableList(selectedMembers);
}
public boolean isSignificant() {
boolean significantEvent = false;
for (final CaseNoteDetailsDTO detail : this.getNoteDetails()) {
significantEvent = significantEvent || detail.isSignificantEvent();
}
return significantEvent;
}
public String getCaseNoteTypeDescription() {
return referenceProvider.provide(ReferenceDomain.CASENOTE_TYPE, getCaseNoteTypeCode());
}
public CaseNoteDetailsDTO getRootNoteDetails() {
validateCaseNoteDetailsExists();
return getNoteDetails().get(0);
}
public List<CaseNoteDetailsDTO> getAppendments() {
validateCaseNoteDetailsExists();
return getNoteDetails().subList(1, getNoteDetails().size());
}
private void validateCaseNoteDetailsExists() {
if (getNoteDetails() == null || getNoteDetails().isEmpty()) {
throw new IllegalStateException("No case note details found");
}
}
public List<String> getMemberNames() {
final List<String> memberNames = new ArrayList<String>();
final List<GroupMemberDetailsDTO> selectedMembers = getSelectedMembers();
for (final GroupMemberDetailsDTO memberDetails : selectedMembers) {
memberNames.add(memberDetails.getName());
}
return memberNames;
}
public Date getTimeOutDate() {
return timeOutDate;
}
public boolean isTimedOut() {
return completedDateTime == null && new Date().after(this.timeOutDate);
}
}
From a JSP file, I would like to print the attribute 'createdWorkerId', but it's not working. I tried to print the title and it works, but not with the createdWorkerId. The line is the following:
<span class="highlighted"><%=noteClassDescription%>: <c:out value="${casenote.createdByWorkerId}"/> </span>
Should I parse the createdWorkerId to a String before or the problem is other? Any help appreciated.
Thanks.