Expand my Community achievements bar.

Upgrade component from AEM 6.2 to 6.5

Avatar

Level 1

Hi there,

 

Could you please help with 6.5 code in sling model for below WCMUsePojo ?

package com.fpt.asr.commons.components.models;

import java.util.ArrayList;
import java.util.List;
import javax.jcr.Node;
import org.apache.sling.api.resource.ValueMap;
import com.adobe.cq.sightly.WCMUsePojo;
import com.fpt.asr.commons.beans.FaqListBean;
import com.fpt.asr.commons.beans.MemberBenefitFAQBean;
import com.fpt.asr.commons.constants.PropertiesConstants;
import com.fpt.asr.commons.utils.JsonUtils;
import com.fpt.asr.commons.utils.MultifieldUtils;

import lombok.Getter;
import lombok.Setter;

@Setter
@getter
public class MemberBenefitFAQModel extends WCMUsePojo {
private String title;
private String[] itemFAQ;
private String item;
private List<Integer> ids;
private List<MemberBenefitFAQBean> memberBenefitFAQBeans;

@Override
public void activate() throws Exception {
ArrayList<Integer> ids = new ArrayList<>();
String path = get("path", String.class);
ValueMap properties = getProperties();
setTitle(properties.get(PropertiesConstants.Component.TITLE, String.class));
String[] itemFAQ = properties.get(PropertiesConstants.Component.ITEM_FAQ, String[].class);
this.setMemberBenefitFAQBeans(JsonUtils.parseAsList(JsonUtils.convertJson(itemFAQ), MemberBenefitFAQBean.class));
List<String> strings = new ArrayList<>();

for (MemberBenefitFAQBean faq : this.getMemberBenefitFAQBeans()) {
List<FaqListBean> faqList = faq.getFaqList();
if (faqList != null) {
for (FaqListBean faqListBean : faqList) {
if (faqListBean.getQuestionId() == 0) {
faqListBean.setQuestionId(MultifieldUtils.randomIdMultifield());
}
ids.add(faqListBean.getQuestionId());
}
}
strings.add(JsonUtils.stringify(faq));
}
this.setItem(JsonUtils.stringify(this.memberBenefitFAQBeans));
this.setIds(ids);
String[] memberBenefitFAQBean = new String[strings.size()];
strings.toArray(memberBenefitFAQBean);

Node rootNode = getRequest().getResourceResolver().resolve(getRequest(), path).adaptTo(Node.class);
if (!this.memberBenefitFAQBeans.isEmpty()) {
MultifieldUtils.convertData(rootNode, "itemFAQ", memberBenefitFAQBean,
JsonUtils.stringify(this.memberBenefitFAQBeans.get(0)));
}
MultifieldUtils.deleteGarbage(ids, rootNode, "faq-{0}");
}
}

 

2 Replies

Avatar

Level 1

Hi @DeepaRani 

 

What issue you are facing? can you elaborate a bit more? 

Avatar

Level 5

Hi @DeepaRani ,

 

WCMUsePojo are replaced by Sling Models, please refer below documents which speaks about how to convert a Pojo to Sling Model and how to write your first Sling Model

 

https://medium.com/@toimrank/aem-sling-models-ea0fb2606625

https://blogs.perficient.com/2017/05/30/how-to-switch-from-wcmusepojo-to-sling-models-in-aem/