-
Notifications
You must be signed in to change notification settings - Fork 11
/
template.py
812 lines (704 loc) · 23.4 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# Apex Test Class Template
def template_test_class():
return '''/**
* @author {author}
*/
@isTest
private class {class_name}Test {{
{class_body}
}}
'''
# Apex Test Method Template
def template_test_method():
return '''
/**
* This is a test method for {function_name}
*/
static testMethod void test_{function_name}() {{
// PageReference pageRef = Page.{page_name};
// Test.setCurrentPage(pageRef);
// pageRef.getParameters().put('param1', 'param1');
Test.startTest();
{function_body}
Test.stopTest();
// Check
// System.assert(ApexPages.hasMessages());
// for(ApexPages.Message msg : ApexPages.getMessages()) {{
// System.assertEquals('Upload file is NULL', msg.getSummary());
// System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());
// }}
}}
'''
# Apex Class Template
def template_class():
return '''/**
* @author {author}
*/
public class {class_name}{class_type} {{
public {class_name}{class_type}() {{
{constructor_body}
}}
{class_body}
}}
'''
# Apex Class Without Constructor Template
def template_no_con_class():
return '''/**
* @author {author}
*/
public class {class_name}{class_type} {{
{class_body}
}}
'''
# Apex Class Template
def template_class_with_sharing():
return '''/**
* @author {author}
*/
public with sharing class {class_name}{class_type} {{
public {class_name}{class_type}() {{
{constructor_body}
}}
{class_body}
}}
'''
# constructor method
def template_constructor_fun():
return '''
public {class_name}({args}) {{
{constructor_body}
}}
'''
# VisualForce table td Template
def template_html_table_content():
return '''
<tr>
<th class="mythclass">
<apex:outputPanel rendered="" >
<span class="mod-icon-note">必須</span>
</apex:outputPanel>
<apex:outputText value="{th_body}" />
</th>
<td>
{td_body}
</td>
</tr>
'''
# VisualForce table td Template
def template_html_table_content_with_validate():
return '''
<tr>
<th class="mythclass">
<apex:outputPanel rendered="" >
<span class="mod-icon-note">必須</span>
</apex:outputPanel>
<apex:outputText value="{th_body}" />
</th>
<td>
{td_body}
<apex:outputPanel layout="block" rendered="{{!!ISBlank({td_body2})}}">
<apex:outputText style="color:red;" value="{{!{td_body2}}}"/>
</apex:outputPanel>
</td>
</tr>
'''
def template_list_vf_table_body():
return '''
<tbody>
<tr>
{th_header}
</tr>
<apex:repeat value="{{!{list_dto_instance}}}" var="{dto_instance}" >
<tr>
{table_body}
</tr>
</apex:repeat>
</tbody>
'''
# VisualForce Template
def template_vf_inputform():
return '''<apex:page showHeader="false" standardStylesheets="false" applyHtmlTag="false" applyBodyTag="false" sidebar="false" showChat="false" cache="false" docType="html-5.0" controller="{controller}">
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>{title}</title>
</head>
<style>
.ul-search {{
list-style-type: none;
}}
.li_search {{
display: table;
width: 100%;
margin-bottom: 20px;
}}
.li_title {{
width: 120px
}}
.li_title,
.li_content {{
display: table-cell;
vertical-align: middle
}}
table.type1 {{
border-collapse: separate;
border-spacing: 1px;
text-align: center;
line-height: 1.5;
}}
table.type1 th {{
width: 155px;
padding: 10px;
font-weight: bold;
vertical-align: top;
color: #fff;
background: #036;
}}
table.type1 td {{
width: 155px;
padding: 10px;
vertical-align: top;
border-bottom: 1px solid #ccc;
background: #eee;
}}
</style>
<body>
<apex:form id="mainform">
<!-- START OF Edit Mode -->
<apex:outputText rendered="{{!isEditMode}}">
{search_vf}
<div class="frame-wrapper">
<table class="type1">
{edit_table_body}
</table>
</div>
<div class="btn">
<apex:commandButton value="Return" action="{{!doBack}}" onclick=""/>
<apex:commandButton value="Next" action="{{!doNext}}" onclick=""/>
</div>
</apex:outputText>
<!-- END OF Edit Mode -->
<!-- START OF View Mode -->
<apex:outputText rendered="{{!isViewMode}}">
{search_vf}
<div class="frame-wrapper">
<table class="type1">
{view_table_body}
</table>
</div>
<div class="btn">
<apex:commandButton value="Return" action="{{!doBack}}" onclick=""/>
<apex:commandButton value="Next" action="{{!doNext}}" onclick=""/>
<apex:commandButton value="Save" action="{{!doSave}}" onclick=""/>
</div>
</apex:outputText>
<!-- END OF View Mode -->
<!-- START OF Message Mode -->
<apex:outputText rendered="{{!isMessageMode}}">
<div class="frame-wrapper">
Message Mode
</div>
<div class="btn">
<apex:commandButton value="Return" action="{{!doBack}}" onclick=""/>
<apex:commandButton value="Next" action="{{!doNext}}" onclick=""/>
</div>
</apex:outputText>
<!-- END OF Message Mode -->
</apex:form>
</body>
</html>
</apex:page>
'''
# VisualForce Template
def template_vf_search():
return '''
<div class="search">
<ul class="ul-search">
{search_vf_item}
<li class="li-search">
<div class="li_title">KeyWord</div>
<div class="li_content"><apex:input type="text" value="{{!keywords}}" /></div>
</li>
<li class="li-search">
<div class="li_title"></div>
<div class="li_content"><apex:commandButton value="Search" action="{{!search}}" onclick=""/></div>
</li>
</ul>
</div>
'''
# VisualForce Template
def template_search_snippet(data_type):
vf_code = ""
if data_type == 'picklist':
vf_code = '''
<li class="li-search">
<div class="li_title">{field_label}</div>
<div class="li_content">
<apex:selectList size="1" value="{{!{field_name}}}" >
<apex:selectOptions value="{{!{field_name}List}}" />
</apex:selectList>
</div>
</li>
'''
return vf_code
def template_sfdcxycontroller():
return '''/**
* @author {author}
*/
public virtual class SfdcXyController {{
// Return URL
public String retUrl {{get;set;}}
// Return Current Url
public String currentUrl {{get;set;}}
// Page Mode
public Integer modeIndex {{get;set;}}
public static final Integer MODE_EDIT = 0;
public static final Integer MODE_VIEW = 1;
public static final Integer MODE_MESSAGE = 2;
public Boolean isEditMode {{
get{{
return (modeIndex == MODE_EDIT);
}}
}}
public Boolean isViewMode {{
get{{
return (modeIndex == MODE_VIEW);
}}
}}
public Boolean isMessageMode {{
get{{
return (modeIndex == MODE_MESSAGE);
}}
}}
public SfdcXyController() {{
this.modeIndex = MODE_EDIT;
this.retUrl = ApexPages.currentPage().getParameters().get('retUrl');
this.currentUrl = ApexPages.currentPage().getURL();
this.retUrl = String.isBlank(this.retUrl) ? this.currentUrl : this.retUrl;
}}
/**
* Go Next
*/
public virtual PageReference doNext() {{
Boolean result = doCheck();
setNextMode(result);
return null;
}}
/**
* Go Back
*/
public virtual PageReference doBack() {{
Boolean result = true;
setBackMode(result);
return null;
}}
/**
* Go Cancel
*/
public virtual PageReference doCancel() {{
if (String.isBlank(retUrl)) {{
retUrl = '/';
}}
PageReference nextPage = new PageReference(retUrl);
nextPage.setRedirect(true);
return nextPage;
}}
/**
* do Check
*/
public virtual Boolean doCheck() {{
Boolean result = true;
return result;
}}
/**
* set next mode
*/
public virtual void setNextMode(Boolean flg) {{
if(!flg) return;
if(modeIndex == MODE_EDIT) modeIndex = MODE_VIEW;
else if(modeIndex == MODE_VIEW) modeIndex = MODE_MESSAGE;
else if(modeIndex == MODE_MESSAGE) modeIndex = MODE_MESSAGE;
}}
/**
* set back mode
*/
public virtual void setBackMode(Boolean flg) {{
if(!flg) return;
if(modeIndex == MODE_EDIT) modeIndex = MODE_EDIT;
else if(modeIndex == MODE_VIEW) modeIndex = MODE_EDIT;
else if(modeIndex == MODE_MESSAGE) modeIndex = MODE_VIEW;
}}
}}
'''
def template_controller_class():
return '''/**
* @author {author}
*/
public with sharing class {controller} extends SfdcXyController {{
// DTO Bean
public {dto} {dto_instance} {{get;set;}}
public {controller}() {{
search();
}}
private void search(){{
String id = ApexPages.currentPage().getParameters().get('id');
if(String.isBlank(id)){{
this.{dto_instance} = new {dto}();
}}else{{
this.{dto_instance} = new {dto}({dao}.get{sobj_api}ById(id));
}}
}}
/**
* upsert Dto
*/
public PageReference doSave() {{
Boolean result;
Savepoint sp = Database.setSavepoint();
try {{
upsert {dto_instance}.getSobject();
result = true;
}} catch(DMLException e) {{
Database.rollback(sp);
System.debug('saveDto DMLException:' + e.getMessage());
result = false;
}} catch(Exception e) {{
Database.rollback(sp);
System.debug('saveDto Exception:' + e.getMessage());
result = false;
}}
return null;
}}
/**
* Go Next
*/
public override PageReference doNext() {{
Boolean result = doCheck();
setNextMode(result);
return null;
}}
/**
* Go Back
*/
public override PageReference doBack() {{
Boolean result = true;
setBackMode(result);
return null;
}}
/**
* do Check
*/
public override Boolean doCheck() {{
Boolean result = true;
return result;
}}
}}
'''
def template_list_controller_class():
return '''/**
* @author {author}
*/
public with sharing class {list_controller} extends SfdcXyController {{
// DTO Bean
public {list_dto} {list_dto_instance} {{get;set;}}
// Search DTO Bean
public {dto} searchDto {{get;set;}}
// Search keywords
public String keywords {{get;set;}}
public {list_controller}() {{
this.searchDto = new {dto}();
search();
}}
public void search(){{
this.{list_dto_instance} = new {list_dto}();
for( {sobject__c} {sobj_api_low_cap} : {dao}.get{sobj_api}List(keywords,searchDto)){{
this.{list_dto_instance}.add(new {dto}({sobj_api_low_cap}));
}}
}}
/**
* upsert Dto
*/
public PageReference doSave() {{
Boolean result;
Savepoint sp = Database.setSavepoint();
try {{
List<{sobject__c}> {sobj_api_low_cap}List = new List<{sobject__c}>();
for({dto} dto : this.{list_dto_instance}){{
{sobj_api_low_cap}List.add(dto.getSobject());
}}
update {sobj_api_low_cap}List;
result = true;
}} catch(DMLException e) {{
Database.rollback(sp);
System.debug('saveDto DMLException:' + e.getMessage());
result = false;
}} catch(Exception e) {{
Database.rollback(sp);
System.debug('saveDto Exception:' + e.getMessage());
result = false;
}}
return null;
}}
/**
* Go Next
*/
public override PageReference doNext() {{
Boolean result = doCheck();
setNextMode(result);
return null;
}}
/**
* Go Back
*/
public override PageReference doBack() {{
Boolean result = true;
setBackMode(result);
return null;
}}
/**
* do Check
*/
public override Boolean doCheck() {{
Boolean result = true;
return result;
}}
}}
'''
def template_dao_class():
return '''/**
* @author {author}
*/
public with sharing class {dao} {{
/**
* get soql query string
*/
public static String getQueryField(){{
String query_str = '';
{soql_src}
return query_str;
}}
/**
* get all {sobject__c}
* @return list of {sobject__c}
*/
public static List<{sobject__c}> getAll{sobj_api}List(){{
String query_str = getQueryField();
query_str += ' limit 10000';
List<{sobject__c}> {sobj_api_low_cap}List = Database.query(query_str);
return {sobj_api_low_cap}List;
}}
/**
* get {sobject__c} by Set<id>
* @return list of {sobject__c}
*/
public static List<{sobject__c}> get{sobj_api}List(Set<String> ids){{
String query_str = getQueryField();
query_str += ' WHERE id IN:ids ';
List<{sobject__c}> {sobj_api_low_cap}List = Database.query(query_str);
return {sobj_api_low_cap}List;
}}
/**
* get {sobject__c} by id
* @return one of {sobject__c}
*/
public static {sobject__c} get{sobj_api}ById(String id){{
String query_str = getQueryField();
query_str += ' WHERE id =: id ';
query_str += ' limit 1 ';
List<{sobject__c}> {sobj_api_low_cap}List = Database.query(query_str);
if({sobj_api_low_cap}List.isEmpty())
return null;
else
return {sobj_api_low_cap}List.get(0);
}}
/**
* get {sobject__c} by keywords
* @return list of {sobject__c}
*/
public static List<{sobject__c}> get{sobj_api}List(String keywords,{dto} searchDto){{
String soql = getQueryField();
String query_str = '';
List<String> query_list = new List<String>();
if(String.isNotBlank(keywords)){{
String[] keywordsArr = keywords.replace(' ',' ').split(' ');
List<String> keywordsFilters = new List<String>();
for(String f: keywordsArr){{
if(String.isBlank(f)) continue;
f = f.replace('%', '\\\\%').replace('_','\\\\_');
keywordsFilters.add('%' + f + '%');
}}
{keywords_conditions}
query_list.add(query_str);
}}
{searchDto_conditions}
if(query_list.size() > 0){{
soql += ' WHERE ';
soql += String.join(query_list, ' and ');
}}
soql += ' limit 10000';
List<{sobject__c}> {sobj_api_low_cap}List = Database.query(soql);
return {sobj_api_low_cap}List;
}}
}}
'''
# def template_dao_class():
# return '''/**
# * @author {author}
# */
# public with sharing class {dao} {{
# /**
# * get all {sobject__c}
# * @return list of {sobject__c}
# */
# public static List<{sobject__c}> getAll{sobj_api}List(){{
# List<{sobject__c}> {sobj_api_low_cap}List = [
# {soql_src}
# limit 10000
# ];
# return {sobj_api_low_cap}List;
# }}
# /**
# * get {sobject__c} by Set<id>
# * @return list of {sobject__c}
# */
# public static List<{sobject__c}> get{sobj_api}List(Set<String> ids){{
# List<{sobject__c}> {sobj_api_low_cap}List = [
# {soql_src}
# WHERE id IN:ids
# ];
# return {sobj_api_low_cap}List;
# }}
# /**
# * get {sobject__c} by id
# * @return one of {sobject__c}
# */
# public static {sobject__c} get{sobj_api}ById(String id){{
# List<{sobject__c}> {sobj_api_low_cap}List = [
# {soql_src}
# WHERE id =: id
# limit 1
# ];
# if({sobj_api_low_cap}List.isEmpty())
# return null;
# else
# return {sobj_api_low_cap}List.get(0);
# }}
# /**
# * get {sobject__c} by keywords
# * @return list of {sobject__c}
# */
# public static List<{sobject__c}> get{sobj_api}List(String keywords){{
# if(String.isBlank(keywords)) return getAll{sobj_api}List();
# String[] keywordsArr = keywords.replace(' ',' ').split(' ');
# List<String> keywordsFilters = new List<String>();
# for(String f: keywordsArr){{
# if(String.isBlank(f)) continue;
# f = f.replace('%', '\\\\%').replace('_','\\\\_');
# keywordsFilters.add('%' + f + '%');
# }}
# List<{sobject__c}> {sobj_api_low_cap}List = [
# {soql_src}
# WHERE
# {keywords_conditions}
# ];
# return {sobj_api_low_cap}List;
# }}
# }}
# '''
def template_dto_class():
return '''/**
* @author {author}
*/
public class {dto} {{
{class_body}
public {dto}() {{
init();
}}
public {dto}({sobject__c} sobj) {{
init();
if(sobj != null){{
{constructor_body}
}}
}}
/**
* init method
*/
public void init(){{
{init_body}
}}
/**
* Change the dto to sobject
* get sobject {sobject__c} from dto
* @return {sobject__c}
*/
public {sobject__c} getSobject(){{
{sobject__c} sobj = new {sobject__c}();
{getSobject_body}
return sobj;
}}
}}
'''
def get_vf_edit_snippet(data_type):
vf_code = ""
if data_type == 'id' or data_type == 'ID' or data_type == 'reference':
vf_code = '''<apex:outputText value="{{!{field_name}}}" />'''
elif data_type == 'string':
vf_code = '''<apex:input type="text" value="{{!{field_name}}}" />'''
elif data_type == 'url' :
vf_code = '''<apex:input type="text" value="{{!{field_name}}}" />'''
elif data_type == 'email':
vf_code = '''<apex:input type="email" value="{{!{field_name}}}" />'''
elif data_type == 'phone':
vf_code = '''<apex:input type="text" value="{{!{field_name}}}" />'''
elif data_type == 'textarea':
vf_code = '''<apex:inputTextarea value="{{!{field_name}}}" />'''
elif data_type == 'picklist':
vf_code = '''<apex:selectList size="1" value="{{!{field_name}}}" >
<apex:selectOptions value="{{!{field_name}List}}" />
</apex:selectList>'''
elif data_type == 'multipicklist':
vf_code = '''<apex:selectList size="10" value="{{!{field_name}}}" multiselect="true">
<apex:selectOptions value="{{!{field_name}List}}" />
</apex:selectList>'''
elif data_type == 'int' or data_type == 'percent' or data_type == 'long' or data_type == 'currency' or data_type == 'double':
vf_code = '''<apex:input type="number" value="{{!{field_name}}}" />'''
elif data_type == 'boolean' or data_type == 'combobox':
vf_code = '''<apex:inputCheckbox value="{{!{field_name}}}" />'''
elif data_type == 'datetime' :
vf_code = '''<apex:input type="datetime-local" value="{{!{field_name}}}" />'''
elif data_type == 'date' :
vf_code = '''<apex:input type="date" value="{{!{field_name}}}" />'''
return vf_code
def get_vf_show_snippet(data_type):
if data_type == 'id' \
or data_type == 'ID' \
or data_type == 'reference' \
or data_type == 'string' \
or data_type == 'url' \
or data_type == 'email' \
or data_type == 'phone' \
or data_type == 'picklist' \
or data_type == 'int' or data_type == 'percent' \
or data_type == 'long' or data_type == 'currency' or data_type == 'double' \
or data_type == 'boolean' or data_type == 'combobox':
vf_code = '''<apex:outputText value="{{!{field_name}}}" />'''
elif data_type == 'textarea':
# white-space: pre;
vf_code = '''<div style="white-space: pre-wrap; word-break: break-all; ">
<apex:outputText value="{{!{field_name}}}" />
</div>'''
elif data_type == 'multipicklist':
vf_code = '''<apex:outputText value="{{!{field_name}}}" />'''
# vf_code = '''<apex:selectList size="10" value="{{!{field_name}}}" multiselect="true">
# <apex:selectOptions value="{{!{field_name}List}}" />
# </apex:selectList>'''
elif data_type == 'datetime' :
vf_code = '''<apex:outputText value="{{!{field_name}}}" />'''
# vf_code = '''<apex:input type="datetime-local" value="{{!{field_name}}}" />'''
elif data_type == 'date' :
vf_code = '''<apex:outputText value="{{!{field_name}}}" />'''
# vf_code = '''<apex:input type="date" value="{{!{field_name}}}" />'''
else :
vf_code = '''<apex:outputText value="{{!{field_name}}}" />'''
return vf_code