Skip to content

Commit

Permalink
implemented change layout when dataset is avail
Browse files Browse the repository at this point in the history
  • Loading branch information
SakalAndrej committed Mar 5, 2018
1 parent 330b27c commit bf3a12f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import at.htl.model.Campaign;
import at.htl.model.DataSetDataField;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

@Stateless
public class CampaignFacade {

@PersistenceContext
Expand Down
32 changes: 18 additions & 14 deletions HomeDsBackend/src/main/java/at/htl/utils/LayoutChangerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
import at.htl.facades.CampaignFacade;
import at.htl.facades.DataSetFieldFacade;
import at.htl.model.Campaign;
import at.htl.model.DataSet;
import at.htl.model.DataSetDataField;
import at.htl.model.Display;
import at.htl.xiboClient.DataSetApi;
import at.htl.xiboClient.DisplayApi;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

@Stateless
public class LayoutChangerUtil {
Expand All @@ -31,28 +26,37 @@ public class LayoutChangerUtil {
@Inject
CampaignFacade campaignFacade;

public void changeLayout(long campaignLayoutId,LocalDateTime toDate) throws NoConnectionException {
displayApi.ScheduleLayout(campaignLayoutId, LocalDateTime.now(),toDate);
dataSetApi.collectNowAll();
public void changeLayout(long campaignLayoutId, LocalDateTime toDate) throws NoConnectionException {
displayApi.scheduleLayout(campaignLayoutId, LocalDateTime.now(), toDate);
dataSetApi.collectNowAll();
}

/*
Wenn keine DataSets verfügbar Layout auf normal wechseln (alte kampagne löschen)
Wenn keine DataSets verfügbar Layout auf normal wechseln (alte kampagne löschen falls es eine alte gibt wenn nicht nichts tun)
Wenn DataSets verfügbar sind dataset layout einlegen (neue kampagne anlegen)
*/
public void campaignLogic() {
if (dataSetFieldFacade.getActiveDataSetRows().size()>0) {
if (dataSetFieldFacade.getActiveDataSetRows().size() > 0) {
try {
long id = -1;
if (campaignFacade.getAll().size() <= 0 && (id = displayApi.ScheduleLayout(43,LocalDateTime.now(), LocalDateTime.MAX)) > 0) {
if (campaignFacade.getAll().size() <= 0 && (id = displayApi.scheduleLayout(43, LocalDateTime.now(), LocalDateTime.now().plusMinutes(2))) > 0) {
campaignFacade.save(new Campaign(id));
}
} catch (NoConnectionException e) {
e.printStackTrace();
}
}
else {
campaignFacade.delete(campaignFacade.getAll().get(0).getId());
} else {
if (campaignFacade.getAll() != null && campaignFacade.getAll().size()>0) {
long deleteId = campaignFacade.getAll().get(0).getId();

try {
if (displayApi.deleteEvent(campaignFacade.getAll().get(0).getCampaignId())) {
campaignFacade.delete(deleteId);
}
} catch (NoConnectionException e) {
e.printStackTrace();
}
}
}
}
}
1 change: 1 addition & 0 deletions HomeDsBackend/src/main/java/at/htl/utils/TimeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void doWork() {
System.out.println("Every min scheduler had no job :(!");
}

layoutChangerUtil.campaignLogic();

}

Expand Down
7 changes: 7 additions & 0 deletions HomeDsBackend/src/main/java/at/htl/web/DataSetController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import at.htl.exceptions.NoConnectionException;
import at.htl.facades.DataSetFieldFacade;
import at.htl.model.DataSetDataField;
import at.htl.utils.LayoutChangerUtil;
import at.htl.xiboClient.DataSetApi;

import javax.annotation.PostConstruct;
Expand All @@ -25,6 +26,9 @@ public class DataSetController implements Serializable {
@Inject
DataSetApi dataSetApi;

@Inject
LayoutChangerUtil layoutChangerUtil;

private static List<DataSetDataField> dataSetData;

private DataSetDataField dataSetToAdd;
Expand All @@ -37,6 +41,7 @@ public DataSetController() {
public void init() {
this.updateList();
dataSetToAdd = new DataSetDataField();
//layoutChangerUtil.campaignLogic();
}

public void removeDataSet(DataSetDataField dataSet) {
Expand All @@ -60,6 +65,7 @@ public void removeDataSet(DataSetDataField dataSet) {
} catch (NoConnectionException ex) {
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "", "Error while establishing a connection"));
}
layoutChangerUtil.campaignLogic();
}

public void addDataSet() {
Expand Down Expand Up @@ -87,6 +93,7 @@ public void addDataSetToXibo(DataSetDataField dataFieldToAdd) {
dataFieldToAdd.setDataRowId(id);
dataSetFieldFacade.save(dataFieldToAdd);
this.updateList();
layoutChangerUtil.campaignLogic();

//clear add variable
dataSetToAdd = new DataSetDataField();
Expand Down
32 changes: 23 additions & 9 deletions HomeDsBackend/src/main/java/at/htl/xiboClient/DisplayApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import at.htl.enums.RequestTypeEnum;
import at.htl.exceptions.NoConnectionException;
import at.htl.model.Campaign;
import at.htl.model.Display;
import at.htl.utils.AuthentificationHandler;
import at.htl.utils.RequestHelper;
Expand All @@ -21,7 +20,7 @@
@Stateless
public class DisplayApi {

public LinkedList<Display> GetAllDisplays() throws NoConnectionException {
public LinkedList<Display> getAllDisplays() throws NoConnectionException {
BufferedReader in;
LinkedList<Display> displays = new LinkedList<>();
Display act = new Display();
Expand Down Expand Up @@ -71,19 +70,17 @@ public LinkedList<Display> GetAllDisplays() throws NoConnectionException {
return displays;
}

public long ScheduleLayout(long campaingLayoutId, LocalDateTime fromDate, LocalDateTime toDate) throws NoConnectionException {
public long scheduleLayout(long campaingLayoutId, LocalDateTime fromDate, LocalDateTime toDate) throws NoConnectionException {

Campaign act = new Campaign();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

BufferedReader in;
LinkedList<Display> displays = new LinkedList<>();
try {
//Get all Datasets
HttpURLConnection con = new RequestHelper()
.executeRequest(RequestTypeEnum.POST,
"eventTypeId=1&campaignId=" + campaingLayoutId +
"&displayOrder=0&isPriority=11&displayGroupIds[]="+14+"&fromDt="+fromDate.format(formatter)+"&toDt="+toDate.format(formatter),
"&displayOrder=0&isPriority=11&displayGroupIds[]="+14+"&fromDt="+fromDate.format(formatter)+"&toDt="+toDate.format(formatter)+"&syncTimezone=1",
new RequestHelper().BASE_URL + "api/schedule",
AuthentificationHandler.getTOKEN());

Expand All @@ -95,8 +92,7 @@ public long ScheduleLayout(long campaingLayoutId, LocalDateTime fromDate, LocalD
while ((output = in.readLine()) != null) {
response.append(output);
}

JSONObject jsonObject = new JSONObject(response);
JSONObject jsonObject = new JSONObject(response.toString());
return jsonObject.getLong("campaignId");
}
else {
Expand All @@ -109,4 +105,22 @@ public long ScheduleLayout(long campaingLayoutId, LocalDateTime fromDate, LocalD
}
}

public boolean deleteEvent(long campaignId) throws NoConnectionException {
try {
HttpURLConnection con = new RequestHelper()
.executeRequest(RequestTypeEnum.DELETE, null,
new RequestHelper().BASE_URL + "api/schedule/" + campaignId,
AuthentificationHandler.getTOKEN());
if (con.getResponseCode()==204) {
return true;
}
else {
return false;
}
} catch (NullPointerException ex) {
throw new NoConnectionException("Es ist kein Response vorhanden", ex);
} catch (IOException e) {
throw new NoConnectionException("IO Exception", e);
}
}
}
15 changes: 8 additions & 7 deletions HomeDsBackend/src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<jta-data-source>java:/mysql</jta-data-source>
<properties>

<!--
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="sampleCreate.ddl"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="sampleDrop.ddl"/>
<property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
-->
<!--
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="sampleCreate.ddl"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="sampleDrop.ddl"/>
<property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
-->

</properties>
</persistence-unit>
Expand Down

0 comments on commit bf3a12f

Please sign in to comment.