Files
mdm/src/main/java/com/midm/model/news/NewsMessage.java
T

307 lines
7.9 KiB
Java

package com.midm.model.news;
import com.google.gson.internal.LinkedTreeMap;
import com.midm.model.device.Device;
import com.midm.model.message.MidmPacket;
import com.midm.model.message.MidmQuery;
import com.midm.model.org.User;
import com.mitac.cell.db.DbObject;
import com.mitac.cell.utils.McDateTimeUtils;
import com.mitac.cell.utils.McEmailUtils;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import org.joda.time.DateTime;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class NewsMessage {
@Id
private UUID uuid;
@DBRef
private User writer;
private String category;
private String forWhat;
private DbObject reference;
private DbObject message;
private Boolean isUnremovable;
private Boolean isEnabled;
private Date sendSchedule;
private Date expiry;
@Transient
private boolean sendNow;
private Date created;
private Date updated;
public NewsMessage(Device device, String category) {
this.init();
this.category = "device_" + category;
this.reference.put("uuid", device.getUuid());
}
public NewsMessage(Device device) {
this.init();
this.reference.put("uuid", device.getUuid());
}
public NewsMessage(MidmPacket packet) {
MidmQuery query = packet.getQuery();
this.setUuid(query.getUuid());
this.created = query.getRegisteredAt();
this.updated = this.created;
String request = query.getRequest();
String command = query.getCommand();
this.category = String.format("%s_%s", request, command);
this.getReference().put("userUuid", query.getSenderUuid());
this.getReference().put("deviceUuid", packet.getRecieverUuid());
this.getReference().put("query", query.getQuery());
}
public NewsMessage() {
this.init();
}
public void init() {
this.uuid = UUID.randomUUID();
this.created = new Date();
this.updated = this.created;
}
public DbObject getMessage() {
if (this.message == null) {
this.message = new DbObject();
}
return this.message;
}
public DbObject getMessage(Locale locale) {
return this.getMessage(locale.toLanguageTag());
}
public DbObject getMessage(String languageTag) {
DbObject message = this.getMessage();
DbObject localedMessage = new DbObject();
if (message.get(languageTag) == null) {
message.put(languageTag, localedMessage);
} else if (message.get(languageTag) instanceof LinkedTreeMap) {
for(Map.Entry entry : ((LinkedTreeMap)message.get(languageTag)).entrySet()) {
localedMessage.append((String)entry.getKey(), entry.getValue());
}
message.put(languageTag, localedMessage);
} else if (message.get(languageTag) instanceof DbObject) {
localedMessage = (DbObject)message.get(languageTag);
message.put(languageTag, localedMessage);
} else if (message.get(languageTag) instanceof org.bson.Document) {
for(Map.Entry entry : ((org.bson.Document)message.get(languageTag)).entrySet()) {
localedMessage.append((String)entry.getKey(), entry.getValue());
}
message.put(languageTag, localedMessage);
}
return localedMessage;
}
public void setMessage(DbObject message) {
this.message = message;
}
public void setMessage(String title, String content) {
this.setTitle(title);
this.setContent(content);
}
public String getTitle() {
return this.getMessage("en-US").getString("title");
}
public String getSummary() {
return this.getMessage().getString("summary");
}
public boolean hasContent() {
return this.getMessage().containsKey("content");
}
public String getContent() {
return this.getMessage("en-US").getString("content");
}
public String getTitle(String locale) {
return this.getMessage(locale).getString("title");
}
public String getSummary(String locale) {
return this.getMessage(locale).getString("summary");
}
public boolean hasContent(String locale) {
return this.getMessage(locale).containsKey("content");
}
public String getContent(String locale) {
return this.getMessage(locale).getString("content");
}
public List getMailsTo() {
return McEmailUtils.parseMails(this.getMessage().getString("to"));
}
public void setMessage(String title, String content, String summary) {
this.setMessage(Locale.US.toLanguageTag(), title, content, summary);
}
public void setMessage(String languageTag, String title, String content, String summary) {
this.setTitle(languageTag, title);
this.setContent(languageTag, content);
this.setSummary(languageTag, summary);
}
public void setTitle(String title) {
this.setTitle(Locale.US.toLanguageTag(), title);
}
public void setContent(String content) {
this.setContent(Locale.US.toLanguageTag(), content);
}
public void setSummary(String summary) {
this.setSummary(Locale.US.toLanguageTag(), summary);
}
public void setTitle(String languageTag, String title) {
DbObject message = this.getMessage(languageTag);
message.put("title", title);
}
public void setContent(String languageTag, String content) {
DbObject message = this.getMessage(languageTag);
message.put("content", content);
}
public DbObject getReference() {
if (this.reference == null) {
this.reference = new DbObject();
}
return this.reference;
}
public void setReference(DbObject reference) {
this.reference = reference;
}
public void setSummary(String languageTag, String summary) {
DbObject message = this.getMessage(languageTag);
message.put("summary", summary);
}
public Boolean isUnremovable() {
return this.isUnremovable;
}
public void setIsUnremovable(Boolean isUnremovable) {
this.isUnremovable = isUnremovable;
}
public String getForWhat() {
return this.forWhat;
}
public void setForWhat(String forWhat) {
this.forWhat = forWhat;
}
public UUID getUuid() {
return this.uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public User getWriter() {
return this.writer;
}
public void setWriter(User writer) {
this.writer = writer;
}
public String getCategory() {
return this.category;
}
public void setCategory(String category) {
this.category = category;
}
public Date getSendSchedule() {
return this.sendSchedule;
}
public void setSendSchedule(Date sendSchedule) {
this.sendSchedule = sendSchedule;
}
public boolean sendNow() {
return this.sendNow || this.sendSchedule == null || McDateTimeUtils.isSameDate(new DateTime(this.sendSchedule), DateTime.now());
}
public Date getExpiry() {
return this.expiry;
}
public void setExpiry(Date expiry) {
this.expiry = expiry;
}
public Date getCreated() {
return this.created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return this.updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
public Boolean getIsUnremovable() {
return this.isUnremovable;
}
public Boolean isEnabled() {
return this.isEnabled;
}
public void setIsEnabled(Boolean isEnabled) {
this.isEnabled = isEnabled;
}
public boolean isSendNow() {
return this.sendNow;
}
public void setSendNow(boolean sendNow) {
this.sendNow = sendNow;
}
public String toDateString(String dateString, String format) {
DateTime dateTime = new DateTime(dateString);
return McDateTimeUtils.toString(dateTime, format);
}
}