Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import eu.dnetlib.uoaadmintools.dao.*;
4
import eu.dnetlib.uoaadmintools.entities.*;
5

    
6
import eu.dnetlib.uoaadmintools.entities.statistics.Statistics;
7
import org.apache.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Value;
9
import org.springframework.web.bind.annotation.*;
10
import org.springframework.beans.factory.annotation.Autowired;
11

    
12
import java.util.*;
13

    
14
@RestController
15
@CrossOrigin(origins = "*")
16
public class CommunityController {
17
    private final Logger log = Logger.getLogger(this.getClass());
18

    
19
    @Autowired
20
    private CommunityDAO communityDAO;
21

    
22
    @Autowired
23
    private LayoutDAO layoutDAO;
24

    
25
    @Autowired
26
    private PageDAO pageDAO;
27

    
28
    @Autowired
29
    private EntityDAO entityDAO;
30

    
31
    @Autowired
32
    private DivIdDAO divIdDAO;
33

    
34
    @Autowired
35
    private PageHelpContentController pageHelpContentController;
36

    
37
    @Autowired
38
    private DivHelpContentController divHelpContentController;
39

    
40
    @Autowired
41
    private HtmlPageContentController htmlPageContentController;
42

    
43
    @Autowired
44
    private DivIdController divIdController;
45

    
46
    @Autowired
47
    private StatisticsDAO statisticsDAO;
48
    @Autowired
49
    private CommunitySubscribersDAO  communitySubscribersDAO;
50

    
51
    @Value( "${admintool.defaultColor}" )
52
    private String defaultColor;
53

    
54
    @RequestMapping(value = "/community", method = RequestMethod.GET)
55
    public List<Community> getAllCommunities() {
56
        List<Community> communities = communityDAO.findAll();
57

    
58
        return communities;
59
    }
60

    
61
    @RequestMapping(value = "/communityFull", method = RequestMethod.GET)
62
    public List<CommunityResponse> getAllCommunitiesFull() {
63
        List<Community> communities = communityDAO.findAll();
64
        List<CommunityResponse> communitiesResponse = new ArrayList<>();
65
        for(Community community : communities) {
66
            CommunityResponse communityResponse = new CommunityResponse(community);
67

    
68
            List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
69
            log.debug("PAGES number="+pages.size());
70
            Iterator<CommunityPage> iteratorPages = pages.iterator();
71
            while(iteratorPages.hasNext()) {
72
                CommunityPage page = iteratorPages.next();
73
                if(!page.getIsEnabled()) {
74
                    iteratorPages.remove();
75
                }
76
            }
77
            communityResponse.setPages(pages);
78
            log.debug("PAGES set");
79

    
80
            List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
81
            log.debug("ENTITIES number="+entities.size());
82
            Iterator<CommunityEntity> iteratorEntities = entities.iterator();
83
            while(iteratorEntities.hasNext()) {
84
                CommunityEntity entity = iteratorEntities.next();
85
                if(!entity.getIsEnabled()) {
86
                    iteratorEntities.remove();
87
                }
88
            }
89
            communityResponse.setEntities(entities);
90
            Layout layout = layoutDAO.findById(community.getLayout());
91
            communityResponse.setLayout(layout);
92
            communitiesResponse.add(communityResponse);
93
        }
94
        return communitiesResponse;
95
    }
96

    
97
    @RequestMapping(value = "/communityFull/{pid}", method = RequestMethod.GET)
98
    public CommunityResponse getCommunityFull(@PathVariable(value = "pid") String pid) {
99
        Community community = communityDAO.findByPid(pid);
100
        CommunityResponse communityResponse = new CommunityResponse(community);
101

    
102
        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getPid(), null, null, null);
103
        Iterator<CommunityPage> iteratorPages = pages.iterator();
104
        while(iteratorPages.hasNext()) {
105
            CommunityPage page = iteratorPages.next();
106
            if(!page.getIsEnabled()) {
107
                iteratorPages.remove();
108
            }
109
        }
110
        communityResponse.setPages(pages);
111

    
112
        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getPid(), null);
113
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
114
        while(iteratorEntities.hasNext()) {
115
            CommunityEntity entity = iteratorEntities.next();
116
            if(!entity.getIsEnabled()) {
117
                iteratorEntities.remove();
118
            }
119
        }
120
        communityResponse.setEntities(entities);
121
        Layout layout = layoutDAO.findById(community.getLayout());
122
        communityResponse.setLayout(layout);
123
//        communityResponse.setPages(this.getPagesForCommunityByType(community.getId(), null));
124
//        communityResponse.setEntities(this.getEntitiesForCommunity(community.getId()));
125

    
126
        return communityResponse;
127
    }
128
/*
129

    
130
    @RequestMapping(value = "/communityFullByName/{name}", method = RequestMethod.GET)
131
    public CommunityResponse getCommunityFullByName(@PathVariable(value = "name") String name) {
132
        Community community = communityDAO.findByName(name);
133
        CommunityResponse communityResponse = new CommunityResponse(community);
134

    
135
        List<CommunityPage> pages = this.getPagesForCommunityByType(community.getId(), null, null);
136
        Iterator<CommunityPage> iteratorPages = pages.iterator();
137
        while(iteratorPages.hasNext()) {
138
            CommunityPage page = iteratorPages.next();
139
            if(!page.getIsEnabled()) {
140
                iteratorPages.remove();
141
            }
142
        }
143
        communityResponse.setPages(pages);
144

    
145
        List<CommunityEntity> entities = this.getEntitiesForCommunity(community.getId(), null);
146
        Iterator<CommunityEntity> iteratorEntities = entities.iterator();
147
        while(iteratorEntities.hasNext()) {
148
            CommunityEntity entity = iteratorEntities.next();
149
            if(!entity.getIsEnabled()) {
150
                iteratorEntities.remove();
151
            }
152
        }
153
        communityResponse.setEntities(entities);
154

    
155
        return communityResponse;
156
    }
157
*/
158

    
159
    @RequestMapping(value = "/community/update", method = RequestMethod.POST)
160
    public CommunityResponse updateCommunity(@RequestBody Community community) {
161
        Community com = communityDAO.findById(community.getId());
162
        com.setName(community.getName());
163
        com.setPid(community.getPid());
164
        // = this.getCommunityByCommunityResponse(communityResponse);
165
        communityDAO.save(com);
166
        CommunityResponse communityResponse = this.getCommunityFull(community.getPid());
167

    
168
        return communityResponse;
169
    }
170

    
171
    @RequestMapping(value = "/community/save", method = RequestMethod.POST)
172
    public CommunityResponse insertCommunity(@RequestBody Community community) {
173
        //Community community = this.getCommunityByCommunityResponse(communityResponse);
174

    
175
        List<CommunityEntity> communityEntities = new ArrayList<>();
176
        List<CommunityPage> communityPages = new ArrayList<>();
177
        Map<String, Boolean> entities = new HashMap<>();
178
        Map<String, Boolean> pages = new HashMap<>();
179

    
180
        for(Entity entity : entityDAO.findAll()) {
181
            entities.put(entity.getId(), true);
182

    
183
            CommunityEntity communityEntity = new CommunityEntity(entity);
184
            communityEntity.setIsEnabled(true);
185
            communityEntities.add(communityEntity);
186
        }
187

    
188
        for(Page page : pageDAO.findAll()) {
189
            pages.put(page.getId(), true);
190

    
191
            CommunityPage communityPage = new CommunityPage(page);
192
            communityPage.setIsEnabled(true);
193

    
194
            communityPages.add(communityPage);
195
        }
196

    
197
        community.setEntities(entities);
198
        community.setPages(pages);
199
        Layout layout = new Layout();
200
        layout.setColor(defaultColor);
201
        layout = layoutDAO.save(layout);
202
        community.setLayout(layout.getId());
203
        Statistics statistics =  new Statistics(community.getPid());
204
        statisticsDAO.save(statistics);
205
        CommunitySubscribers communitySubscribers =  new CommunitySubscribers(community.getPid());
206
        communitySubscribersDAO.save(communitySubscribers);
207
        Community savedCommunity = communityDAO.save(community);
208
        CommunityResponse communityResponse = this.getCommunityFull(savedCommunity.getPid());
209

    
210
        log.debug("pid of saved community: "+savedCommunity.getPid());
211

    
212
        String id = savedCommunity.getId();
213

    
214
        String link_context_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Select a research community and/or a category and search for a community concept, or browse to the community tree through the categories</div> </div>";
215
        String link_project_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Search for projects using project name or grant id. Limit results filtering by funder.</div> </div>";
216
        String link_result_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span></div> Search for research results in OpenAIRE information space, Datacite, CrossRef or ORCID. <div class=\"uk-text-small\">Use keywords, DOI (more than one - space separated), author&#39;s ORCID</div> </div>";
217
        String link_result_bulk_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Upload a csv file containing a list of DOIs. For each DOI found in the file, metadata will be fetched from CrossRef or Datacite and will be added to your selected research results.</div> <div class=\"uk-margin-top uk-text-small\"><span class=\"uk-text-bold\">CSV format:</span> <ul class=\"uk-list\"> <li>The format of CSV file should be &quot;DOI&quot;,&quot;ACCESS_MODE&quot;,&quot;DATE&quot;.</li> <li>The value &quot;DOI&quot; is required</li> <li>Access mode column should have values: &quot;OPEN&quot;,&quot;CLOSED&quot; or &quot;EMBARGO&quot;.</li> <li>Date column valid format is YYYY-MM-DD and is required when access mode has value EMBARGO.</li> <li>In case access mode is not available default value is &quot;OPEN&quot;.</li> </ul> </div> </div>";
218
        String link_metadata_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Manage access mode &amp; type of selected research results. For OpenAIRE this functionality isn&#39;t available.</div> </div>";
219

    
220
        DivId div = null;
221
        div=divIdDAO.findByName("link-context-form");
222
        if(div != null) {
223
            String link_context_form = div.getId();
224
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_context_form, id, link_context_form_content, false));
225
        }
226

    
227
        div=divIdDAO.findByName("link-project-form");
228
        if(div != null) {
229
            String link_project_form = div.getId();
230
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_project_form, id, link_project_form_content, false));
231
        }
232

    
233
        div=divIdDAO.findByName("link-result-form");
234
        if(div != null) {
235
            String link_result_form = div.getId();
236
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_result_form, id, link_result_form_content, true));
237
        }
238

    
239
        div=divIdDAO.findByName("link-result-bulk");
240
        if(div != null) {
241
            String link_result_bulk = div.getId();
242
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_result_bulk, id, link_result_bulk_content, true));
243
        }
244

    
245
        div=divIdDAO.findByName("link-metadata");
246
        if(div != null) {
247
            String link_metadata = div.getId();
248
            divHelpContentController.insertOrUpdateDivHelpContent(new DivHelpContent(link_metadata, id, link_metadata_content, false));
249
        }
250

    
251
        Page page = null;
252
        page = pageDAO.findByRoute("/about" );
253
        if(page != null) {
254
            String htmlContent = "<div><div class=\"uk-article-title custom-article-title\"> About the community </div> <p> This is an introductory text. To be updated... </p> </div>";
255
            HtmlPageContent htmlPageContent = new HtmlPageContent(page.getId(), id, htmlContent);
256
            htmlPageContentController.updateHtmlPageContent(htmlPageContent);
257
        }
258

    
259
        page = pageDAO.findByRoute("/organizations");
260
        if(page != null) {
261
            String htmlContent = "<div><div class=\"uk-article-title custom-article-title\"> Organizations related to the community </div> <p> This is an introductory text. Here follows the list of organizations... </p> <div class=\"uk-child-width-1-3@m uk-text-center uk-grid-match \" uk-grid > <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"https://upload.wikimedia.org/wikipedia/el/2/2b/Logo_uoa_blue.png\" alt=\"\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"https://www.uoa.gr/\">University of Athens</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"https://pbs.twimg.com/profile_images/631127495933165569/ElbqhHK0_400x400.jpg\" alt=\"\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"https://www.athena-innovation.gr/en\">Athena Research & Innovation center</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 1\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 1</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 2\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 2</a> </h3> </div> </div> <div class=\"uk-card uk-card-default uk-margin-bottom uk-padding-remove\"> <div class=\"uk-card-media-top\"> <img src=\"\" alt=\"Logo 3\" class=\"uk-height-small uk-responsive-height \"> </div> <div class=\"uk-card-body\"> <h3 class=\"uk-card-title\"> <a class=\"wk-link-reset\" href=\"\">Organization 3</a> </h3> </div> </div> </div></div>";
262
            HtmlPageContent htmlPageContent = new HtmlPageContent(page.getId(), id, htmlContent);
263
            htmlPageContentController.updateHtmlPageContent(htmlPageContent);
264
        }
265

    
266
        return communityResponse;
267
    }
268

    
269
    private Community getCommunityByCommunityResponse(CommunityResponse communityResponse) {
270
        Community community = new Community();
271
        community.setId(communityResponse.getId());
272
        community.setName(communityResponse.getName());
273

    
274
        List<CommunityEntity> fullEntities = communityResponse.getEntities();
275
        Map<String, Boolean> entities = new HashMap<String, Boolean>();
276
        for(CommunityEntity entity : fullEntities) {
277
            entities.put(entity.getId(), true);
278
        }
279
        for(Entity entity : entityDAO.findAll()) {
280
            if(!entities.containsKey(entity.getId())) {
281
                entities.put(entity.getId(), false);
282
            }
283
        }
284
        community.setEntities(entities);
285

    
286
        List<CommunityPage> fullPages = communityResponse.getPages();
287
        Map<String, Boolean> pages = new HashMap<String, Boolean>();
288
        for(CommunityPage page : fullPages) {
289
            pages.put(page.getId(), true);
290
        }
291
        for(Page page : pageDAO.findAll()) {
292
            if(!pages.containsKey(page.getId())) {
293
                pages.put(page.getId(), false);
294
            }
295
        }
296
        community.setPages(pages);
297
        Layout layout = communityResponse.getLayout();
298
        community.setLayout(layout.getId());
299

    
300
        return community;
301
    }
302

    
303
    @RequestMapping(value = "/community/delete", method = RequestMethod.POST)
304
    public Boolean deleteCommunities(@RequestBody List<String> communities) throws Exception {
305
        for (String id: communities) {
306
            Community community = communityDAO.findById(id);
307
            String pid = community.getPid();
308

    
309
            // delete div contents related to this community
310
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(pid, null, null, null);
311
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
312
                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
313
            }
314

    
315
            // delete page contents related to this community
316
            List<PageHelpContentResponse> pageHelpContentResponses = pageHelpContentController.getPageHelpContents(pid, null, null, null, null);
317
            for(PageHelpContentResponse pageHelpContentResponse : pageHelpContentResponses) {
318
                pageHelpContentController.deletePageHelpContent(pageHelpContentResponse.getId());
319
            }
320

    
321
            List<HtmlPageContent> htmlPageContents = htmlPageContentController.getHtmlPageContents(pid, null);
322
            for(HtmlPageContent htmlPageContent : htmlPageContents) {
323
                htmlPageContentController.deleteHtmlPageContent(htmlPageContent.getId());
324
            }
325

    
326
            Statistics stats = statisticsDAO.findByPid(pid);
327
            if(stats != null) {
328
                statisticsDAO.delete(stats.getId());
329
            }
330

    
331
            CommunitySubscribers communitySubscribers = communitySubscribersDAO.findByPid(pid);
332
            if(communitySubscribers != null) {
333
                communitySubscribersDAO.delete(communitySubscribers.getId());
334
            }
335

    
336
            Layout layout = layoutDAO.findById(community.getLayout());
337
            if(layout != null) {
338
                layoutDAO.delete(layout.getId());
339
            }
340

    
341
            communityDAO.delete(id);
342
        }
343

    
344
        return true;
345
    }
346

    
347
//    @RequestMapping(value = "/community", method = RequestMethod.DELETE)
348
//    public void deleteAllCommunities() {
349
//        communityDAO.deleteAll();
350
//    }
351

    
352
    @RequestMapping(value = "/community", method = RequestMethod.POST)
353
    public Community insertOrUpdateCommunity(@RequestBody Community community) {
354
        return communityDAO.save(community);
355
    }
356

    
357
    @RequestMapping(value = "/community/{pid}", method = RequestMethod.GET)
358
    public Community getCommunity(@PathVariable(value = "pid") String pid) {
359
        log.debug("PID: "+ pid);
360
        return communityDAO.findByPid(pid);
361
    }
362

    
363
    @RequestMapping(value = "/community/{id}", method = RequestMethod.DELETE)
364
    public void deleteCommunity(@PathVariable(value = "id") String id) {
365
        communityDAO.delete(id);
366
    }
367

    
368
    @RequestMapping(value = "/community/{pid}/pages", method = RequestMethod.GET)
369
    public List<CommunityPage> getPagesForCommunityByType(@PathVariable(value = "pid") String pid,
370
                                                          @RequestParam(value="page_type", required=false) String page_type,
371
                                                          @RequestParam(value="page_route", required=false) String page_route,
372
                                                          @RequestParam(value="div", required = false) String div) {
373
        List<CommunityPage> return_pages = new ArrayList<CommunityPage>();
374
        Map<String, Boolean> pages = communityDAO.findByPid(pid).getPages();
375

    
376
        if(pages != null) {
377
            for (Map.Entry<String, Boolean> page : pages.entrySet()) {
378
                if(div != null && div.equals("true")) {
379
                    Community community = communityDAO.findByPid(pid);
380
                    List<DivId> divIds = divIdDAO.findByPagesContaining(page.getKey());
381
                    if(divIds.isEmpty()) {
382
                        continue;
383
                    }
384
                }
385

    
386
                Page p = pageDAO.findById(page.getKey());
387

    
388
                if((pid.equals("openaire") && p.getOpenaire()) ||(!pid.equals("openaire") && p.getConnect())) {
389
                    if ((page_type == null && page_route == null) || (page_route == null && p.getType().equals(page_type))
390
                            || p.getRoute().equals(page_route)) {
391
                        CommunityPage communityPage = new CommunityPage(p);
392

    
393
                        List<Entity> entities = new ArrayList<>();
394
                        for (String entity : p.getEntities()) {
395
                            entities.add(entityDAO.findById(entity));
396
                        }
397
                        communityPage.setEntities(entities);
398
                        communityPage.setIsEnabled(page.getValue());
399

    
400
                        return_pages.add(communityPage);
401

    
402
                        if (page_route != null) {
403
                            break;
404
                        }
405
                    }
406
                }
407
            }
408
        }
409
        return_pages.sort(Comparator.comparing(CommunityPage::getName));
410
        return return_pages;
411
    }
412

    
413
    @RequestMapping(value = "/community/{id}/page", method = RequestMethod.POST)
414
    public Community insertOrUpdatePage(@PathVariable(value = "id") String id, @RequestBody CommunityPage page) {
415
        Community community = communityDAO.findById(id);
416
        Map<String, Boolean> pages = community.getPages();
417

    
418
        String name = page.getName();
419
        boolean isEnabled = page.getIsEnabled();
420

    
421
        pages.put(name, isEnabled);
422
        community.setPages(pages);
423

    
424
        return communityDAO.save(community);
425
    }
426

    
427
    @RequestMapping(value = "community/{pid}/page/toggle", method = RequestMethod.POST)
428
    public Community togglePage(@PathVariable(value = "pid") String pid, @RequestBody List<String> pageIds, @RequestParam String status) throws Exception {
429
        Community community = communityDAO.findByPid(pid);
430
        Map<String, Boolean> pages = community.getPages();
431

    
432
        for (String pageId: pageIds) {
433
            log.debug("Toggle community page: " + pageId + " of community: " + pid + " to " + status);
434
            pages.put(pageId, Boolean.parseBoolean(status));
435
        }
436

    
437
        community.setPages(pages);
438
        return communityDAO.save(community);
439
    }
440

    
441
    @RequestMapping(value = "community/{pid}/entity/toggle", method = RequestMethod.POST)
442
    public Community toggleEntity(@PathVariable(value = "pid") String pid, @RequestBody List<String> entityIds, @RequestParam String status) throws Exception {
443
        Community community = communityDAO.findByPid(pid);
444
        Map<String, Boolean> entities = community.getEntities();
445
        Map<String, Boolean> pages = community.getPages();
446

    
447
        for (String entityId: entityIds) {
448
            log.debug("Toggle community entity: " + entityId + " of community: " + pid + " to " + status);
449

    
450
            entities.put(entityId, Boolean.parseBoolean(status));
451

    
452
            if(pages != null) {
453
                for (Map.Entry<String, Boolean> pageEntry : pages.entrySet()) {
454
                    Page page = pageDAO.findById(pageEntry.getKey());
455
                    if (page.getEntities().contains(entityId) && page.getType().equals("search")) {
456
                        pages.put(pageEntry.getKey(), Boolean.parseBoolean(status));
457
                    }
458
                }
459
            }
460
        }
461

    
462
        community.setEntities(entities);
463
        return communityDAO.save(community);
464
    }
465

    
466
    @RequestMapping(value = "/community/{pid}/entities", method = RequestMethod.GET)
467
    public List<CommunityEntity> getEntitiesForCommunity(@PathVariable(value = "pid") String pid, @RequestParam(value="entity", required=false) String entity) {
468
        List<CommunityEntity> return_entities = new ArrayList<CommunityEntity>();
469
        Map<String, Boolean> entities = communityDAO.findByPid(pid).getEntities();
470

    
471
        log.debug("/community/"+pid+"/entities -- entity: "+entity);
472
        if (entity != null) {
473
            String entityId = entityDAO.findByPid(entity).getId();
474
            CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(entityId));
475
            communityEntity.setIsEnabled(entities.get(entityId));
476
            return_entities.add(communityEntity);
477
        } else {
478
            if(entities != null) {
479
                for (Map.Entry<String, Boolean> _entity : entities.entrySet()) {
480
                    CommunityEntity communityEntity = new CommunityEntity(entityDAO.findById(_entity.getKey()));
481
                    communityEntity.setIsEnabled(_entity.getValue());
482
                    return_entities.add(communityEntity);
483
                }
484
            }
485
        }
486
        return return_entities;
487
    }
488

    
489
    @RequestMapping(value = "/community/{pid}/layout", method = RequestMethod.GET)
490
    public Layout getLayoutForCommunity(@PathVariable(value = "pid") String pid) {
491
        Community community = communityDAO.findByPid(pid);
492
        return layoutDAO.findById(community.getLayout());
493
    }
494

    
495
    @RequestMapping(value = "/community/{pid}/updateLayout", method = RequestMethod.POST)
496
    public Layout updateLayoutForCommunity(@PathVariable(value = "pid") String pid, @RequestBody Layout layout) {
497
        Community community = communityDAO.findByPid(pid);
498
        if(community.getLayout() != null) {
499
            layout.setId(community.getLayout());
500
            layout = layoutDAO.save(layout);
501
        } else {
502
            layout = layoutDAO.save(layout);
503
            community.setLayout(layout.getId());
504
            communityDAO.save(community);
505
        }
506
        return layout;
507
    }
508

    
509
    @RequestMapping(value = "/community/{pid}/resetLayout", method = RequestMethod.POST)
510
    public Layout resetLayoutForCommunity(@PathVariable(value = "pid") String pid) {
511
        Layout layout;
512
        Community community = communityDAO.findByPid(pid);
513
        if(community.getLayout() != null) {
514
            layout = layoutDAO.findById(community.getLayout());
515
            layout.setColor(defaultColor);
516
            layout = layoutDAO.save(layout);
517
        } else {
518
            layout = new Layout();
519
            layout.setColor(defaultColor);
520
            layout = layoutDAO.save(layout);
521
            community.setLayout(layout.getId());
522
            communityDAO.save(community);
523
        }
524
        return layout;
525
    }
526
}
527

    
(1-1/15)