<?php
namespace App\Service\Kernel;
use App\Entity\Kernel\DocumentTemplate;
use App\Entity\Kernel\DocumentTemplateGroup;
use App\Entity\Kernel\TemplateType;
use App\Entity\Kernel\User;
use App\Entity\Kernel\Organization;
use App\Entity\Kernel\ConfigurableList;
use App\Entity\Kernel\ListItem;
use Cassandra\Set;
use Doctrine\Persistence\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
use App\Entity\Kernel\SecurityAction;
use App\Entity\Kernel\UserLog;
use App\Entity\Kernel\Setting;
use Symfony\Component\HttpKernel\KernelInterface;
use App\Entity\Kernel\BatchJob;
use App\Entity\Kernel\Widget;
use DateInterval;
class KernelService {
private $entityManager;
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
public function getApplicationInfoValue($key, $organization = null) {
try {
$setting = $this->entityManager->getRepository(Setting::class)->findOneBy(array('identifier' => $key, 'organization' => $organization));
if (!$setting) {
$setting = $this->entityManager->getRepository(Setting::class)->findOneByIdentifier($key);
if (!$setting) {
return "";
}
}
return $setting->getValue();
} catch (Exception $e) {
return "";
}
}
public function getApplicationInfoFile($key) {
$setting = $this->entityManager->getRepository(Setting::class)->findOneByIdentifier($key);
return $setting->getFileValue();
}
/**
* @param $list
* @param $organization
* @return ConfigurableList|null
*/
public function getList($list, $organization = null): ?ConfigurableList {
if ($organization == null)
{
$weblist = $this->entityManager->getRepository(ConfigurableList::class)->findOneBy(array('identifier' => $list, 'state' => 0));
}
else
{
$query = $this->entityManager
->createQuery("SELECT s FROM App\Entity\Kernel\ConfigurableList s
WHERE (((s.state = 0) AND (s.identifier = ?1)) AND ((s.organization = ?2) or (s.organization is null)))");
$query = $query->setParameter(1, $list);
$query = $query->setParameter(2, $organization->getId());
$weblists = $query->getResult();
if ($weblists) {
$weblist = $weblists[0];
}
else
{
$weblist = null;
}
}
if (!$weblist) {
return null;
} else {
return $weblist;
}
}
/**
* @param string $list
* @param string $item
* @param $organization
* @return ListItem|null
*/
public function getListItem(string $list, string $item, $organization = null) {
$weblist = $this->getList($list, $organization);
if (!$weblist) {
return null;
} else {
return $this->entityManager->getRepository(ListItem::class)
->findOneBy(array('configurablelist' => $weblist, 'name' => $item, 'state' => 0));
}
}
public function getSetting(string $settindIdentifier) : Setting{
return $this->entityManager->getRepository(Setting::class)
->findOneBy(array('identifier' => $settindIdentifier, 'state' => 0));
}
/**
* @param string $name
* @param string $identifier
* @param $integer1
* @param $string1
* @param $string2
* @param $authorizationtype
* @param int $state
* @return ConfigurableList|mixed
*/
public function createList(
string $name,
string $identifier,
$integer1,
$string1,
$string2,
$authorizationtype,
int $state
) {
$list = $this->entityManager->getRepository(ConfigurableList::class)->findOneByIdentifier($identifier);
if (!$list) {
$list = new ConfigurableList();
$list->setName($name);
$list->setIdentifier($identifier);
$list->setInteger1($integer1);
$list->setString1($string1);
$list->setString2($string2);
$list->setAuthorizationtype($authorizationtype);
$list->setState($state);
}
return $list;
}
/**
* @param $object
* @return void
*/
public function persistAndFlush($object)
{
$this->entityManager->persist($object);
$this->entityManager->flush();
}
/**
* @param ConfigurableList $list
* @param string $name
* @return ListItem
*/
public function addToConfigurableList(ConfigurableList $list, string $name) : ListItem
{
$listitem = $this->entityManager->getRepository(ListItem::class)->findOneBy(
array('name' => $name, 'configurablelist' => $list, 'state' => 0)
);
if (!$listitem) {
$listitem = new ListItem();
$listitem->setConfigurablelist($list);
$listitem->setName($name);
$listitem->setState(0);
}
return $listitem;
}
/**
* @param ConfigurableList $list
* @param string $name
* @return ListItem
*/
public function addValueTypeToList(ConfigurableList $list, string $name) : ListItem
{
$listitem = $this->addToConfigurableList($list, $name);
$this->persistAndFlush($listitem);
return $listitem;
}
/**
* @param string $name
* @param string $indentifier
* @param bool $organizationSpecific
* @param int $state
* @param string $type
* @param $authorizationtype
* @param $fileValue
* @return Setting
*/
public function createSetting(
string $name,
string $indentifier,
bool $organizationSpecific,
int $state,
string $type,
$authorizationtype,
$fileValue
) {
$setting = $this->entityManager->getRepository(Setting::class)->findOneByIdentifier($indentifier);
if (!$setting) {
$setting = new Setting();
$setting->setName($name);
$setting->setIdentifier($indentifier);
$setting->setOrganizationSpecific($organizationSpecific);
$setting->setState($state);
$setting->setType($type);
$setting->setFileValue($fileValue);
$setting->setAuthorizationtype($authorizationtype);
}
return $setting;
}
public function createTemplateType(string $name,string $functionalArea,string $description,bool $parentNeeded,bool $isParent,string $stringPart1 ,string $part1){
$templateType = $this->entityManager->getRepository(TemplateType::class)->findOneByName(
$name
);
if (!$templateType) {
$templateType = new TemplateType();
$templateType->setName($name);
$templateType->setFunctionalArea($functionalArea);
$templateType->setParameterDescription($description);
$templateType->setParentNeeded($parentNeeded);
$templateType->setIsParent($isParent);
$templateType->setStringPart1($stringPart1);
$templateType->setPart1($part1);
$templateType->setState(0);
$this->persistAndFlush($templateType);
}
return $templateType;
}
public function createDocumentTemplate($documentTemplateGroup,TemplateType $type,string $stringPart1,string $part1){
$documentTemplate = $this->entityManager->getRepository(DocumentTemplate::class)->findOneBy(
array('documenttemplategroup' => $documentTemplateGroup, 'type' => $type)
);
if (!$documentTemplate) {
$documentTemplate = new DocumentTemplate();
}
$documentTemplate->setDocumenttemplategroup($documentTemplateGroup);
$documentTemplate->setType($type);
$documentTemplate->setState(0);
$documentTemplate->setStringPart1(
$stringPart1
);
$documentTemplate->setPart1(
$part1
);
$this->persistAndFlush($documentTemplate);
return $documentTemplate;
}
public function createDocumentTemplateGroup(string $name){
$documentTemplateGroup = $this->entityManager->getRepository(DocumentTemplateGroup::class)->findOneByName($name);
if (!$documentTemplateGroup) {
$documentTemplateGroup = new DocumentTemplateGroup();
$documentTemplateGroup->setName($name);
$documentTemplateGroup->setState(0);
$this->persistAndFlush($documentTemplateGroup);
}
return $documentTemplateGroup;
}
public function CreateBatchjob($type, $enabled, $minute, $hour, $dayOfMonth, $month, $dayOfWeek, $authorizationtype, $organization)
{
$batchjobType = $this->entityManager->getRepository(ListItem::class)->findOneByName($type);
$minuteList = $this->getListItem("Kernel.Minute", $minute);
$hourList = $this->getListItem("Kernel.Hour", $hour);
$dayOfMonthList = $this->getListItem("Kernel.DayOfMonth", $dayOfMonth);
$monthList = $this->getListItem("Kernel.Month", $month);
$dayOfWeekList = $this->getListItem("Kernel.DayOfWeek", $dayOfWeek);
$batchjob = $this->entityManager->getRepository(BatchJob::class)->findOneBy(
array('type' => $batchjobType, 'organization' => $organization));
if(!$batchjob){
$batchjob = new BatchJob();
}
$batchjob->setType( $batchjobType );
$batchjob->setEnabled($enabled);
$batchjob->addMinute( $minuteList );
$batchjob->addHour($hourList);
$batchjob->addDayOfMonth($dayOfMonthList);
$batchjob->addMonth($monthList);
$batchjob->addDayOfWeek($dayOfWeekList);
$batchjob->setAuthorizationtype($authorizationtype);
$batchjob->setOrganization($organization);
$batchjob->setState(0);
$this->entityManager->persist($batchjob);
$this->entityManager->flush();
return $batchjob;
}
public function getBatchjob($type)
{
return $this->entityManager->getRepository(BatchJob::class)->findOneBy(['type' => $type]);
}
public function createConfigurableList($identifier, $name, $string1, $string2, $boolean1, $boolean2, $integer1, $integer2, $float1, $float2, $text1, $text2, $list1, $list2, $list1List, $list2List, $authorizationtype, $organization ) {
$configurableList = $this->entityManager->getRepository(ConfigurableList::class)->findOneBy(array('identifier' => $identifier, 'state' => 0));
if (!$configurableList) {
$configurableList = new ConfigurableList();
}
$configurableList->setIdentifier($identifier);
$configurableList->setName($name);
$configurableList->setString1($string1);
$configurableList->setString2($string2);
$configurableList->setBoolean1($boolean1);
$configurableList->setBoolean2($boolean2);
$configurableList->setInteger1($integer1);
$configurableList->setInteger2($integer2);
$configurableList->setFloat1($float1);
$configurableList->setFloat2($float2);
$configurableList->setText1($text1);
$configurableList->setText2($text2);
$configurableList->setList1($list1);
$configurableList->setList2($list2);
$configurableList->setList1List($list1List);
$configurableList->setList2List($list2List);
$configurableList->setState(0);
$configurableList->setOrganization($organization);
$configurableList->setAuthorizationtype($authorizationtype);
$this->entityManager->persist($configurableList);
$this->entityManager->flush();
return $configurableList;
}
public function CreateWidget(
$TypeName,
$name,
$string1,
$boolean1,
$boolean2,
$boolean3,
$list1,
$list1list
){
$ConfigurableList = $this->entityManager->getRepository(ConfigurableList::class)->findOneBy(['identifier' => "Kernel.ScreenConfigurationType"]);
$type = $this->entityManager->getRepository(ListItem::class)->findOneBy(
array('name' => $TypeName, 'configurablelist' => $ConfigurableList, 'state' => 0)
);
$widget = $this->entityManager->getRepository(Widget::class)->findOneBy(
array('name' => $name, 'type' => $type, 'state' => 0)
);
if (!$widget) {
$widget = new Widget();
}
$widget->setState(0);
$widget->setName($name);
$widget->setType($type);
$widget->setString1($string1);
$widget->setBoolean1($boolean1);
$widget->setBoolean2($boolean2);
$widget->setBoolean3($boolean3);
$widget->setList1($list1);
$widget->setList1List($list1list);
$this->entityManager->persist($widget);
$this->entityManager->flush();
return $widget;
}
public function archiveListItems(ConfigurableList $list){
foreach ($list->getListitems() as $item){
$item->setState(1);
}
$this->persistAndFlush($list);
return $list;
}
public function getWidget($name){
return $this->entityManager->getRepository(Widget::class)->findOneBy(
array('name' => $name, 'state' => 0)
);
}
/**
* @throws Exception
*/
public function stringToDateTimeInterval(string $durationString):\DateInterval
{
$vals = explode(':', $durationString);
if(sizeof($vals) > 1 && strlen($vals[1]) < 2 ){
$vals[1] = (int)$vals[1]*10;
}
if(count($vals) == 2){
$result = 'PT' . $vals[0] . 'H' . $vals[1] . 'M';
}else{
$result = 'PT' . $vals[0] . 'H';
}
return new DateInterval($result);
}
}